MyServerSessionPool.java
This class implements the javax.jms.ServerSessionPool interface, creating and controlling access to a pool of ServerSessions.
In this implementation, the pool consists of a static array of ServerSession objects that are created during the construction of the pool. The following four parameters are passed into the constructor:
- javax.jms.Connection connection
The connection used to create JMS sessions.
- int capacity
The size of the array of MyServerSession objects.
- int ackMode
The required acknowledge mode of the JMS sessions.
- MessageListenerFactory mlf
The MesssageListenerFactory that creates the message listener that is supplied to the JMS sessions. See MessageListenerFactory.java.
The pool's constructor uses these parameters to create an array of MyServerSession objects. The supplied connection is used to create JMS sessions of the given acknowledge mode and correct domain (QueueSessions for point-to-point and TopicSessions for publish/subscribe). The sessions are supplied with a message listener. Finally, the ServerSession objects, based on the JMS sessions, are created.
This sample implementation is a static model. That is, all the ServerSessions in the pool are created when the pool is created, and after this the pool cannot grow or shrink. This approach is just for simplicity. It is possible for a ServerSessionPool to use a sophisticated algorithm to create ServerSessions dynamically, as needed.
MyServerSessionPool keeps a record of which ServerSessions are currently in use by maintaining an array of boolean values called inUse. These booleans are all initialized to false. When the getServerSession method is invoked and requests a ServerSession from the pool, the inUse array is searched for the first false value. When one is found, the boolean is set to true and the corresponding ServerSession is returned. If there are no false values in the inUse array, the getServerSession method must wait() until notification occurs.
Notification occurs in either of the following circumstances:
- The pool's close() method is called, indicating that the pool must be shut down.
- A ServerSession that is currently in use completes its workload and calls the serverSessionFinished method. The serverSessionFinished method returns the ServerSession to the pool, and sets the corresponding inUse flag to false. The ServerSession then becomes eligible for reuse.
uj25660_