public class ConnectionContextFactory extends Object implements AutoCloseable
producer or
consumer contexts. You can then pass these created contexts to
MessageProcessor to send and receive messages.
This class can reuse a connection so that it is shared across multiple contexts. See
createOrReuseConnection(ConnectionContext, boolean). If this object was told not to reuse
its connection, it will create a new connection for each context is creates.
When you are done with sending and receiving messages through the created contexts, you should call close()
to free up resources and close the connection to the broker.
Subclasses are free to extend this class to add or override functionality or to provide stricter type-checking.| Modifier and Type | Field and Description |
|---|---|
protected javax.jms.ConnectionFactory |
connectionFactory |
| Constructor and Description |
|---|
ConnectionContextFactory(boolean reuseConnection,
javax.jms.ConnectionFactory connectionFactory)
Initializes with the given factory.
|
ConnectionContextFactory(boolean reuseConnection,
String brokerURL)
Initializes the factory with the given broker URL.
|
ConnectionContextFactory(boolean reuseConnection,
String brokerURL,
String username,
String password)
Initializes the factory with the given broker URL and the given security credentials.
|
ConnectionContextFactory(javax.jms.ConnectionFactory connectionFactory)
Initializes with the given factory.
|
ConnectionContextFactory(String brokerURL)
Initializes the factory with the given broker URL.
|
ConnectionContextFactory(String brokerURL,
String username,
String password)
Initializes the factory with the given broker URL and the given security credentials.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
cacheConnection(javax.jms.Connection connection,
boolean closeExistingConnection)
To store a connection in this processor object, call this setter.
|
void |
close()
This will close its open connection that it has cached, thus freeing up resources.
|
protected void |
createConnection(ConnectionContext context)
Creates a connection using this object's connection factory and stores
that connection in the given context object.
|
protected void |
createConsumer(ConsumerConnectionContext context,
String messageSelector)
Creates a message consumer using the context's session and destination.
|
ConsumerConnectionContext |
createConsumerConnectionContext(Endpoint endpoint)
Creates a new consumer connection context, reusing any existing connection that might have already been created.
|
ConsumerConnectionContext |
createConsumerConnectionContext(Endpoint endpoint,
String messageSelector)
Creates a new consumer connection context, reusing any existing connection that might have
already been created.
|
protected void |
createDestination(ConnectionContext context,
Endpoint endpoint)
Creates a destination using the context's session.
|
protected void |
createOrReuseConnection(ConnectionContext context,
boolean start)
This method provides a way to cache and share a connection across
multiple contexts.
|
protected void |
createProducer(ProducerConnectionContext context)
Creates a message producer using the context's session and destination.
|
ProducerConnectionContext |
createProducerConnectionContext(Endpoint endpoint)
Creates a new producer connection context, reusing any existing connection that might have already been created.
|
protected void |
createSession(ConnectionContext context)
Creates a default session using the context's connection.
|
protected javax.jms.Connection |
getConnection()
The stored connection.
|
protected javax.jms.ConnectionFactory |
getConnectionFactory() |
protected boolean |
isReuseConnection() |
public ConnectionContextFactory(String brokerURL) throws javax.jms.JMSException
brokerURL - the broker that is used for the contexts created by this factory;
all messages sent and received through the contexts will go through this broker.javax.jms.JMSException - any errorpublic ConnectionContextFactory(boolean reuseConnection,
String brokerURL)
throws javax.jms.JMSException
reuseConnection - if true this factory will reuse its connection so contexts it creates can share it.
Use this with caution because if a shared connection is closed, all contexts created
with that connection will no longer work.brokerURL - the broker that is used for the contexts created by this factory;
all messages sent and received through the contexts will go through this broker.javax.jms.JMSException - any errorpublic ConnectionContextFactory(String brokerURL, String username, String password) throws javax.jms.JMSException
brokerURL - the broker that is used for the contexts created by this factory;
all messages sent and received through the contexts will go through this broker.username - user to connect topassword - credentials of userjavax.jms.JMSException - any errorpublic ConnectionContextFactory(boolean reuseConnection,
String brokerURL,
String username,
String password)
throws javax.jms.JMSException
reuseConnection - if true this factory will reuse its connection so contexts it creates can share it.
Use this with caution because if a shared connection is closed, all contexts created
with that connection will no longer work.brokerURL - the broker that is used for the contexts created by this factory;
all messages sent and received through the contexts will go through this broker.username - user to connect topassword - credentials of userjavax.jms.JMSException - any errorpublic ConnectionContextFactory(javax.jms.ConnectionFactory connectionFactory)
throws javax.jms.JMSException
connectionFactory - the factory that will be used to create contexts.javax.jms.JMSException - any errorpublic ConnectionContextFactory(boolean reuseConnection,
javax.jms.ConnectionFactory connectionFactory)
throws javax.jms.JMSException
reuseConnection - if true this factory will reuse its connection so contexts it creates can share it.
Use this with caution because if a shared connection is closed, all contexts created
with that connection will no longer work.connectionFactory - the factory that will be used to create contexts.javax.jms.JMSException - any errorpublic ProducerConnectionContext createProducerConnectionContext(Endpoint endpoint) throws javax.jms.JMSException
endpoint - where the producer will send messagesjavax.jms.JMSException - any errorpublic ConsumerConnectionContext createConsumerConnectionContext(Endpoint endpoint) throws javax.jms.JMSException
endpoint - where the consumer will listen for messagesjavax.jms.JMSException - any errorpublic ConsumerConnectionContext createConsumerConnectionContext(Endpoint endpoint, String messageSelector) throws javax.jms.JMSException
endpoint - where the consumer will listen for messagesmessageSelector - message consumer's message selector expression.javax.jms.JMSException - any errorpublic void close()
throws javax.jms.JMSException
close in interface AutoCloseablejavax.jms.JMSException - any errorprotected boolean isReuseConnection()
protected javax.jms.ConnectionFactory getConnectionFactory()
protected javax.jms.Connection getConnection()
createConnection(ConnectionContext).createConnection(ConnectionContext)protected void cacheConnection(javax.jms.Connection connection,
boolean closeExistingConnection)
createConnection(ConnectionContext) does
not set this processor's connection - that method only creates the
connection and puts that connection in the context. It does not save that
connection in this processor object. You must explicitly set the
connection via this method if you want that connection cached here. See
also createOrReuseConnection(ConnectionContext, boolean).connection - the connectioncloseExistingConnection - if true, and if there was already a connection
cached, that connection will be closed. Otherwise
it will be left alone but the new connection
will take its place.createOrReuseConnection(ConnectionContext, boolean)protected void createOrReuseConnection(ConnectionContext context, boolean start) throws javax.jms.JMSException
getConnection() is non-null). If
there is no connection yet, one will be created. Whether the connection
is created or reused, that connection will be stored in the given
context.
Note that if this object was told not to cache connections, this method
will always create a new connection and store it in this object, overwriting
any previously created connection (see cacheConnection(javax.jms.Connection, boolean)).context - the connection will be stored in this contextstart - if true, the created connection will be started.javax.jms.JMSException - any errorprotected void createConnection(ConnectionContext context) throws javax.jms.JMSException
cacheConnection(javax.jms.Connection, boolean) must be passed the connection
found in the context after this method returns. See also
createOrReuseConnection(ConnectionContext, boolean).context - the context where the new connection is storedjavax.jms.JMSException - any errorIllegalStateException - if the context is nullcreateOrReuseConnection(ConnectionContext, boolean),
cacheConnection(javax.jms.Connection, boolean)protected void createSession(ConnectionContext context) throws javax.jms.JMSException
context - the context where the new session is storedjavax.jms.JMSException - any errorIllegalStateException - if the context is null or the context's connection is nullprotected void createDestination(ConnectionContext context, Endpoint endpoint) throws javax.jms.JMSException
context - the context where the new destination is storedendpoint - identifies the queue or topicjavax.jms.JMSException - any errorIllegalStateException - if the context is null or the context's session is null or endpoint is nullprotected void createProducer(ProducerConnectionContext context) throws javax.jms.JMSException
context - the context where the new producer is storedjavax.jms.JMSException - any errorIllegalStateException - if the context is null or the context's session is null
or the context's destination is nullprotected void createConsumer(ConsumerConnectionContext context, String messageSelector) throws javax.jms.JMSException
context - the context where the new consumer is storedmessageSelector - the message selector expression that the consumer will use to filter messagesjavax.jms.JMSException - any errorIllegalStateException - if the context is null or the context's session is null
or the context's destination is nullCopyright © 2015 Red Hat, Inc.. All rights reserved.