Class ProducerSessionPool

java.lang.Object
io.micronaut.jms.pool.ProducerSessionPool
All Implemented Interfaces:
AutoCloseable

public final class ProducerSessionPool extends Object implements AutoCloseable
Reuses the sessions of producers across sends, instead of opening a connection and a session for each message. The sessions are created on one connection from the connection factory of a JMSConnectionPool, which this pool opens on the first send and closes with itself. A session is used by one send at a time, and a session that failed is closed rather than reused. At most maxActive sessions are borrowed at once, of all session modes: when all of them are borrowed, a send waits up to maxWait for one to be released.

A session released while sends are waiting is kept for them, so a burst of sends reuses at most maxActive sessions of each session mode instead of closing and opening sessions. Once no send waits, a released session is closed if maxIdle sessions of its mode are idle already, and a periodic check closes the oldest idle sessions above maxIdle that remain, for example sessions kept for a send that took one of another mode.

When a session cannot be created, or the provider reports a failure of the connection to its ExceptionListener, the connection is replaced. Its idle sessions are closed, its borrowed sessions are closed when they are released, and the connection is closed once the last of its sessions is.

Since:
5.2.0
  • Field Details

    • DEFAULT_MAX_ACTIVE

      public static final int DEFAULT_MAX_ACTIVE
      The default maximum number of sessions that are borrowed at once.
      See Also:
    • DEFAULT_MAX_WAIT

      public static final Duration DEFAULT_MAX_WAIT
      The default time that a send waits for a session when the maximum number of sessions are borrowed.
  • Constructor Details

    • ProducerSessionPool

      public ProducerSessionPool(JMSConnectionPool connectionPool, int maxIdle)
      Parameters:
      connectionPool - The pool of the connection that the sessions are created on
      maxIdle - The maximum number of idle sessions kept for each session mode
    • ProducerSessionPool

      public ProducerSessionPool(JMSConnectionPool connectionPool, int maxIdle, int maxActive, Duration maxWait)
      Parameters:
      connectionPool - The pool of the connection that the sessions are created on
      maxIdle - The maximum number of idle sessions kept for each session mode once no send waits
      maxActive - The maximum number of sessions borrowed at once, of all session modes
      maxWait - How long a send waits for a session when maxActive sessions are borrowed
  • Method Details

    • borrow

      public jakarta.jms.Session borrow(boolean transacted, int acknowledgeMode) throws jakarta.jms.JMSException
      Borrows a session, which must be returned with release(Session, boolean). When the maximum number of sessions are borrowed, waits for one to be released.
      Parameters:
      transacted - Whether the session is transacted
      acknowledgeMode - The acknowledge mode of the session
      Returns:
      The session
      Throws:
      jakarta.jms.JMSException - If the session cannot be created, or no session was released in time
    • release

      public void release(jakarta.jms.Session session, boolean broken)
      Returns a borrowed session.
      Parameters:
      session - The session
      broken - Whether a send failed on the session, so it is closed instead of reused
    • getIdleCount

      public int getIdleCount()
      Returns:
      The number of sessions that are currently idle
    • getBorrowedCount

      public int getBorrowedCount()
      Returns:
      The number of sessions that are currently borrowed
    • close

      public void close()
      Closes the idle sessions and the connections, which also closes the sessions that are still borrowed.
      Specified by:
      close in interface AutoCloseable