public class JmsPoolConnectionFactory extends Object implements javax.jms.ConnectionFactory, javax.jms.QueueConnectionFactory, javax.jms.TopicConnectionFactory
setConnectionCheckInterval(long)
method. By default the value is -1 which means no connection check thread will be run. Set to a
non-negative value to configure the connection check thread to run.Modifier and Type | Field and Description |
---|---|
protected Object |
connectionFactory |
static int |
DEFAULT_MAX_CONNECTIONS |
protected boolean |
jmsContextSupported |
protected AtomicBoolean |
stopped |
Constructor and Description |
---|
JmsPoolConnectionFactory() |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears all connections from the pool.
|
javax.jms.Connection |
createConnection() |
javax.jms.Connection |
createConnection(String userName,
String password) |
javax.jms.JMSContext |
createContext() |
javax.jms.JMSContext |
createContext(int sessionMode) |
javax.jms.JMSContext |
createContext(String username,
String password) |
javax.jms.JMSContext |
createContext(String username,
String password,
int sessionMode) |
protected PooledConnection |
createPooledConnection(javax.jms.Connection connection)
Delegate that creates each instance of an ConnectionPool object.
|
protected javax.jms.Connection |
createProviderConnection(PooledConnectionKey key)
Given a
PooledConnectionKey create a JMS Connection using the
configuration from the key and the assigned JMS ConnectionFactory instance. |
protected javax.jms.JMSContext |
createProviderContext(String username,
String password,
int sessionMode)
Create a new
JMSContext using the provided credentials and Session mode |
javax.jms.QueueConnection |
createQueueConnection() |
javax.jms.QueueConnection |
createQueueConnection(String userName,
String password) |
javax.jms.TopicConnection |
createTopicConnection() |
javax.jms.TopicConnection |
createTopicConnection(String userName,
String password) |
long |
getBlockIfSessionPoolIsFullTimeout()
Returns the timeout to use for blocking creating new sessions
|
long |
getConnectionCheckInterval() |
Object |
getConnectionFactory() |
int |
getConnectionIdleTimeout()
Gets the idle timeout value applied to Connection's that are created by this pool but are
not currently in use.
|
protected org.apache.commons.pool2.impl.GenericKeyedObjectPool<PooledConnectionKey,PooledConnection> |
getConnectionsPool()
Gets the Pool of ConnectionPool instances which are keyed by different ConnectionKeys.
|
int |
getExplicitProducerCacheSize()
Returns the currently configured producer cache size that will be used in a pooled
Session when the pooled Session is not configured to use a single anonymous producer.
|
int |
getMaxConnections()
Returns the maximum number to pooled Connections that this factory will allow before it
begins to return existing connections from the pool on calls to (
createConnection() . |
int |
getMaxSessionsPerConnection()
Returns the currently configured maximum number of sessions a pooled Connection will
create before it either blocks or throws an exception when a new session is requested,
depending on configuration.
|
int |
getNumConnections() |
void |
initConnectionsPool() |
boolean |
isBlockIfSessionPoolIsFull()
Returns whether a pooled Connection will enter a blocked state or will throw an Exception
once the maximum number of sessions has been borrowed from the the Session Pool.
|
boolean |
isUseAnonymousProducers()
Should Sessions use one anonymous producer for all producer requests or should a new
MessageProducer be created for each request to create a producer object, default is true.
|
boolean |
isUseProviderJMSContext() |
protected JmsPoolJMSContext |
newPooledConnectionContext(JmsPoolConnection connection,
int sessionMode)
Allows subclasses to create an appropriate JmsPoolJMSContext wrapper for the newly
create JMSContext such as one that provides support for XA Transactions.
|
protected JmsPoolConnection |
newPooledConnectionWrapper(PooledConnection connection)
Allows subclasses to create an appropriate JmsPoolConnection wrapper for the newly
create connection such as one that provides support for XA Transactions.
|
protected void |
populateProperties(Properties props)
Called by any superclass that implements a JNDI Referenceable or similar that needs to collect
the properties of this class for storage etc.
|
void |
setBlockIfSessionPoolIsFull(boolean block)
Controls the behavior of the internal session pool.
|
void |
setBlockIfSessionPoolIsFullTimeout(long blockIfSessionPoolIsFullTimeout)
Controls the behavior of the internal
Session pool. |
void |
setConnectionCheckInterval(long connectionCheckInterval)
Sets the number of milliseconds to sleep between runs of the Connection check thread.
|
void |
setConnectionFactory(Object factory)
Sets the ConnectionFactory used to create new pooled Connections.
|
void |
setConnectionIdleTimeout(int connectionIdleTimeout)
Sets the idle timeout value for Connection's that are created by this pool but not in use in
Milliseconds (defaults to 30 seconds).
|
void |
setExplicitProducerCacheSize(int cacheSize)
Sets whether a pooled Session uses a cache for MessageProducer instances that are
created against an explicit destination instead of creating new MessageProducer on each
call to Session.createProducer(javax.jms.Destination).
|
void |
setMaxConnections(int maxConnections)
Sets the maximum number of pooled Connections (defaults to one).
|
void |
setMaxSessionsPerConnection(int maxSessionsPerConnection)
Sets the maximum number of pooled sessions per connection in the pool.
|
void |
setUseAnonymousProducers(boolean value)
Sets whether a pooled Session uses only one anonymous MessageProducer instance or creates
a new MessageProducer for each call the create a MessageProducer.
|
void |
setUseProviderJMSContext(boolean useProviderJMSContext)
Controls the behavior of the
createContext() methods. |
void |
start()
Starts the Connection pool.
|
void |
stop()
Stops the pool from providing any new connections and closes all pooled Connections.
|
public static final int DEFAULT_MAX_CONNECTIONS
protected final AtomicBoolean stopped
protected Object connectionFactory
protected boolean jmsContextSupported
public void initConnectionsPool()
public Object getConnectionFactory()
public void setConnectionFactory(Object factory)
Updates to this value do not affect Connections that were previously created and placed
into the pool. In order to allocate new Connections based off this new ConnectionFactory
it is first necessary to clear()
the pooled Connections.
factory
- The factory to use to create pooled Connections.public javax.jms.QueueConnection createQueueConnection() throws javax.jms.JMSException
createQueueConnection
in interface javax.jms.QueueConnectionFactory
javax.jms.JMSException
public javax.jms.QueueConnection createQueueConnection(String userName, String password) throws javax.jms.JMSException
createQueueConnection
in interface javax.jms.QueueConnectionFactory
javax.jms.JMSException
public javax.jms.TopicConnection createTopicConnection() throws javax.jms.JMSException
createTopicConnection
in interface javax.jms.TopicConnectionFactory
javax.jms.JMSException
public javax.jms.TopicConnection createTopicConnection(String userName, String password) throws javax.jms.JMSException
createTopicConnection
in interface javax.jms.TopicConnectionFactory
javax.jms.JMSException
public javax.jms.Connection createConnection() throws javax.jms.JMSException
createConnection
in interface javax.jms.ConnectionFactory
javax.jms.JMSException
public javax.jms.Connection createConnection(String userName, String password) throws javax.jms.JMSException
createConnection
in interface javax.jms.ConnectionFactory
javax.jms.JMSException
public javax.jms.JMSContext createContext()
createContext
in interface javax.jms.ConnectionFactory
public javax.jms.JMSContext createContext(int sessionMode)
createContext
in interface javax.jms.ConnectionFactory
public javax.jms.JMSContext createContext(String username, String password)
createContext
in interface javax.jms.ConnectionFactory
public javax.jms.JMSContext createContext(String username, String password, int sessionMode)
createContext
in interface javax.jms.ConnectionFactory
public void start()
If configured to do so this method will attempt to create an initial Connection to place
into the pool using the default ConnectionFactory.createConnection()
from the configured
provider ConnectionFactory
.
public void stop()
This method stops services from the JMS Connection Pool closing down any Connections in the pool regardless of them being loaned out at the time. The pool cannot be restarted after a call to stop.
public void clear()
createConnection()
if the pool has not been stopped. Care should be taken when
using this method as Connections that are in use by the client will be closed.public int getMaxSessionsPerConnection()
public void setMaxSessionsPerConnection(int maxSessionsPerConnection)
A Connection that is created from this JMS Connection pool can limit the number
of Sessions that are created and loaned out. When a limit is in place the client
application must be prepared to respond to failures or hangs of the various
Connection.createSession()
methods.
Because Connections can be borrowed and returned at will the available Sessions for a Connection in the pool can change dynamically so even on fresh checkout from this pool a Connection may not have any available Session instances to loan out if a limit is configured.
maxSessionsPerConnection
- The maximum number of pooled sessions per connection in the pool.public void setBlockIfSessionPoolIsFull(boolean block)
Connection.createSession()
will block if the session pool is full. If the
block options is set to false, it will change the default behavior and instead the
call to create a Session
will throw a JMSException.
The size of the session pool is controlled by the getMaxSessionsPerConnection()
configuration property.
block
- if true, the call to Connection.createSession()
blocks if the session pool is full
until a session is available. defaults to true.public boolean isBlockIfSessionPoolIsFull()
setBlockIfSessionPoolIsFull(boolean)
public int getMaxConnections()
createConnection()
.public void setMaxConnections(int maxConnections)
createConnection()
will result in a new Connection being created up to the max
connections value, once the maximum Connections have been created Connections are served
in a last in first out ordering.maxConnections
- the maximum Connections to pool for a given user / password combination.public int getConnectionIdleTimeout()
public void setConnectionIdleTimeout(int connectionIdleTimeout)
For a Connection that is in the pool but has no current users the idle timeout determines how
long the Connection can live before it is eligible for removal from the pool. Normally the
connections are tested when an attempt to check one out occurs so a Connection instance can sit
in the pool much longer than its idle timeout if connections are used infrequently. To evict idle
connections in a more timely manner the setConnectionCheckInterval(long)
can be configured
to a non-zero value and the pool will actively check for idle connections that have exceeded their
idle timeout value.
connectionIdleTimeout
- The maximum time a pooled Connection can sit unused before it is eligible for removal.public boolean isUseAnonymousProducers()
When enabled the session only needs to allocate one MessageProducer for all requests and the MessageProducer#send(destination, message) method can be used. Normally this is the right thing to do however it does result in the Broker not showing the producers per destination.
public void setUseAnonymousProducers(boolean value)
value
- Boolean value that configures whether anonymous producers are used.public int getExplicitProducerCacheSize()
public void setExplicitProducerCacheSize(int cacheSize)
When caching explicit producers the cache will hold up to the configured number of producers and if more producers are created than the configured cache size the oldest or lest recently used producers are evicted from the cache and will be closed when all references to that producer are explicitly closed or when the pooled session instance is closed. By default this value is set to zero and no caching is done for explicit producers created by the pooled session.
This caching would only be done when the setUseAnonymousProducers(boolean)
configuration
option is disabled.
cacheSize
- The number of explicit producers to cache in the pooled Sessionpublic void setConnectionCheckInterval(long connectionCheckInterval)
By default this value is set to -1 and a connection check thread is not started.
connectionCheckInterval
- The time to wait between runs of the Connection check thread.public long getConnectionCheckInterval()
public int getNumConnections()
public long getBlockIfSessionPoolIsFullTimeout()
setBlockIfSessionPoolIsFull(boolean)
public void setBlockIfSessionPoolIsFullTimeout(long blockIfSessionPoolIsFullTimeout)
Session
pool. By default the call to
Connection.getSession() will block if the Session
pool is full. This setting
will affect how long it blocks and throws an exception after the timeout.
The size of the session pool is controlled by the setMaxSessionsPerConnection(int)
value that has been configured. Whether or not the call to create session blocks is controlled
by the setBlockIfSessionPoolIsFull(boolean)
property.
By default the timeout defaults to -1 and a blocked call to create a Session will
wait indefinitely for a new Session
blockIfSessionPoolIsFullTimeout
- if blockIfSessionPoolIsFullTimeout is true then use this setting
to configure how long to block before an error is thrown.public boolean isUseProviderJMSContext()
public void setUseProviderJMSContext(boolean useProviderJMSContext)
createContext()
methods.
By default this value is set to false and the JMS Pool will use n pooled version of a JMSContext to wrap Connections from the pool. These pooled JMSContext objects have certain limitations which may not be desirable in some cases. To use the JMSContext implementation from the underlying JMS provider this option can be set to true however in that case no pooling will be applied to the JMSContext's create or their underlying connections.
useProviderJMSContext
- Boolean value indicating whether the pool should include JMSContext in the pooling.protected org.apache.commons.pool2.impl.GenericKeyedObjectPool<PooledConnectionKey,PooledConnection> getConnectionsPool()
protected PooledConnection createPooledConnection(javax.jms.Connection connection)
connection
- The connection that is being added into the pool.protected JmsPoolConnection newPooledConnectionWrapper(PooledConnection connection)
connection
- The PooledConnection
to wrap.JmsPoolConnection
that wraps the given PooledConnection
protected JmsPoolJMSContext newPooledConnectionContext(JmsPoolConnection connection, int sessionMode)
connection
- The JmsPoolConnection
to use in the JMSContext wrapper.sessionMode
- The JMS Session acknowledgement mode to use in the JMSContext
JmsPoolJMSContext
that wraps the given JmsPoolConnection
protected javax.jms.Connection createProviderConnection(PooledConnectionKey key) throws javax.jms.JMSException
PooledConnectionKey
create a JMS Connection
using the
configuration from the key and the assigned JMS ConnectionFactory
instance.key
- The PooledSessionKey
to use as configuration for the new JMS Connection.javax.jms.JMSException
- if an error occurs while creating the new JMS Connection.protected javax.jms.JMSContext createProviderContext(String username, String password, int sessionMode)
JMSContext
using the provided credentials and Session modeusername
- The user name to use when creating the context.password
- The password to use when creating the context.sessionMode
- The session mode to use when creating the context.javax.jms.JMSRuntimeException
- if an error occurs while creating the new JMS Context.protected void populateProperties(Properties props)
props
- a properties object that should be filled in with this objects property values.Copyright © 2017–2019. All rights reserved.