Class JMSContext
- java.lang.Object
-
- org.objectweb.joram.client.jms.JMSContext
-
- All Implemented Interfaces:
AutoCloseable,JMSContext
public class JMSContext extends Object implements JMSContext
AJMSContextis the main interface in the simplified JMS API introduced for JMS 2.0. This combines in a single object the functionality of two separate objects from the JMS 1.1 API: aConnectionand aSession.When an application needs to send messages it use the
createProducermethod to create aJMSProducerwhich provides methods to configure and send messages. Messages may be sent either synchronously or asynchronously.When an application needs to receive messages it uses one of several
createConsumerorcreateDurableConsumermethods to create aJMSConsumer. AJMSConsumerprovides methods to receive messages either synchronously or asynchronously.In terms of the JMS 1.1 API a
JMSContextshould be thought of as representing both aConnectionand aSession. Although the simplified API removes the need for applications to use those objects, the concepts of connection and session remain important. A connection represents a physical link to the JMS server and a session represents a single-threaded context for sending and receiving messages.A
JMSContextmay be created by calling one of severalcreateContextmethods on aConnectionFactory. AJMSContextthat is created in this way is described as being application-managed. An application-managedJMSContextmust be closed when no longer needed by calling itsclosemethod.Applications running in the Java EE web and EJB containers may alternatively inject a
JMSContextinto their application using the@Injectannotation. AJMSContextthat is created in this way is described as being container-managed. A container-managedJMSContextwill be closed automatically by the container.Applications running in the Java EE web and EJB containers are not permitted to create more than one active session on a connection so combining them in a single object takes advantage of this restriction to offer a simpler API.
However applications running in a Java SE environment or in the Java EE application client container are permitted to create multiple active sessions on the same connection. This allows the same physical connection to be used in multiple threads simultaneously. Such applications which require multiple sessions to be created on the same connection should use one of the
createContextmethods on theConnectionFactoryto create the firstJMSContextand then use thecreateContextmethod onJMSContextto create additionalJMSContextobjects that use the same connection. All theseJMSContextobjects are application-managed and must be closed when no longer needed by calling theirclosemethod.- Since:
- Joram 5.9
-
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedAllows to prevent exception if the context is closed more than on time.private ContextConnectionconnectionstatic org.objectweb.util.monolog.api.Loggerloggerprivate Sessionsession-
Fields inherited from interface javax.jms.JMSContext
AUTO_ACKNOWLEDGE, CLIENT_ACKNOWLEDGE, DUPS_OK_ACKNOWLEDGE, SESSION_TRANSACTED
-
-
Constructor Summary
Constructors Constructor Description JMSContext(Connection cnx)Creates a new Context using a newly created JMS connection.JMSContext(Connection cnx, int sessionMode)Creates a new Context using a newly created JMS connection.JMSContext(ContextConnection connection, int sessionMode)Creates a new Context sharing the connection of the calling context.
-
Method Summary
-
-
-
Field Detail
-
logger
public static org.objectweb.util.monolog.api.Logger logger
-
closed
private boolean closed
Allows to prevent exception if the context is closed more than on time.
-
connection
private ContextConnection connection
-
session
private Session session
-
-
Constructor Detail
-
JMSContext
public JMSContext(ContextConnection connection, int sessionMode)
Creates a new Context sharing the connection of the calling context.- Parameters:
connection- the connection of the calling context.sessionMode- indicates which of four possible session modes will be used.
-
JMSContext
public JMSContext(Connection cnx)
Creates a new Context using a newly created JMS connection.- Parameters:
cnx- the created JMS connection.
-
JMSContext
public JMSContext(Connection cnx, int sessionMode)
Creates a new Context using a newly created JMS connection.- Parameters:
cnx- the created JMS connection.sessionMode- indicates which of four possible session modes will be used.
-
-
Method Detail
-
createContext
public JMSContext createContext(int sessionMode)
API method. Creates a newJMSContextwith the specified session mode using the same connection as thisJMSContextand creating a new session.This method does not start the connection. If the connection has not already been started then it will be automatically started when a
JMSConsumeris created on any of theJMSContextobjects for that connection.- If
sessionModeis set toJMSContext.SESSION_TRANSACTEDthen the session will use a local transaction which may subsequently be committed or rolled back by calling theJMSContext'scommitorrollbackmethods. - If
sessionModeis set to any ofJMSContext.CLIENT_ACKNOWLEDGE,JMSContext.AUTO_ACKNOWLEDGEorJMSContext.DUPS_OK_ACKNOWLEDGE. then the session will be non-transacted and messages received by this session will be acknowledged according to the value ofsessionMode. For a definition of the meaning of these acknowledgement modes see the links below.
This method must not be used by applications running in the Java EE web or EJB containers because doing so would violate the restriction that such an application must not attempt to create more than one active (not closed)
Sessionobject per connection. If this method is called in a Java EE web or EJB container then aJMSRuntimeExceptionwill be thrown.- Specified by:
createContextin interfaceJMSContext- Parameters:
sessionMode- indicates which of four possible session modes will be used. The permitted values areJMSContext.SESSION_TRANSACTED,JMSContext.CLIENT_ACKNOWLEDGE,JMSContext.AUTO_ACKNOWLEDGEandJMSContext.DUPS_OK_ACKNOWLEDGE.- Returns:
- a newly created JMSContext
- Throws:
JMSRuntimeException- if the JMS provider fails to create the JMSContext due to- some internal error or
- because this method is being called in a Java EE web or EJB application.
- Since:
- JMS 2.0
- See Also:
JMSContext.SESSION_TRANSACTED,JMSContext.CLIENT_ACKNOWLEDGE,JMSContext.AUTO_ACKNOWLEDGE,JMSContext.DUPS_OK_ACKNOWLEDGE,ConnectionFactory.createContext(),ConnectionFactory.createContext(int),ConnectionFactory.createContext(java.lang.String, java.lang.String),ConnectionFactory.createContext(java.lang.String, java.lang.String, int),JMSContext.createContext(int)
- If
-
close
public void close()
API method. Closes the JMSContextThis closes the underlying session and any underlying producers and consumers. If there are no other active (not closed) JMSContext objects using the underlying connection then this method also closes the underlying connection.
Since a provider typically allocates significant resources outside the JVM on behalf of a connection, clients should close these resources when they are not needed. Relying on garbage collection to eventually reclaim these resources may not be timely enough.
Closing a connection causes all temporary destinations to be deleted.
When this method is invoked, it should not return until message processing has been shut down in an orderly fashion. This means that all message listeners that may have been running have returned, and that all pending receives have returned. A close terminates all pending message receives on the connection's sessions' consumers. The receives may return with a message or with null, depending on whether there was a message available at the time of the close. If one or more of the connection's sessions' message listeners is processing a message at the time when connection
closeis invoked, all the facilities of the connection and its sessions must remain available to those listeners until they return control to the JMS provider.This method must not return until any incomplete asynchronous send operations for this JMSContext have been completed and any CompletionListener callbacks have returned. Incomplete sends should be allowed to complete normally unless an error occurs.
For the avoidance of doubt, if an exception listener for the JMSContext's connection is running when
closeis invoked, there is no requirement for theclosecall to wait until the exception listener has returned before it may return.Closing a connection causes any of its sessions' transactions in progress to be rolled back. In the case where a session's work is coordinated by an external transaction manager, a session's
commitandrollbackmethods are not used and the result of a closed session's work is determined later by the transaction manager.Closing a connection does NOT force an acknowledgment of client-acknowledged sessions.
Invoking the
acknowledgemethod of a received message from a closed connection's session must throw anIllegalStateRuntimeException. Closing a closed connection must NOT throw an exception.A MessageListener must not attempt to close its own JMSContext as this would lead to deadlock. The JMS provider must detect this and throw a IllegalStateRuntimeException.
A CompletionListener callback method must not call close on its own JMSContext. Doing so will cause an IllegalStateRuntimeException to be thrown.
This method must not be used if the
JMSContextis container-managed (injected). Doing so will cause aIllegalStateRuntimeExceptionto be thrown.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceJMSContext- Throws:
IllegalStateRuntimeException-- if this method has been called by a MessageListener on its own JMSContext
- if this method has been called by a CompletionListener callback method on its own JMSContext
- if the
JMSContextis container-managed (injected)
JMSRuntimeException- if the JMS provider fails to close theJMSContextdue to some internal error. For example, a failure to release resources or to close a socket connection can cause this exception to be thrown.
-
acknowledge
public void acknowledge()
- Specified by:
acknowledgein interfaceJMSContext
-
commit
public void commit()
- Specified by:
commitin interfaceJMSContext
-
recover
public void recover()
- Specified by:
recoverin interfaceJMSContext
-
rollback
public void rollback()
- Specified by:
rollbackin interfaceJMSContext
-
createMessage
public Message createMessage()
- Specified by:
createMessagein interfaceJMSContext
-
createBytesMessage
public BytesMessage createBytesMessage()
- Specified by:
createBytesMessagein interfaceJMSContext
-
createMapMessage
public MapMessage createMapMessage()
- Specified by:
createMapMessagein interfaceJMSContext
-
createObjectMessage
public ObjectMessage createObjectMessage()
- Specified by:
createObjectMessagein interfaceJMSContext
-
createObjectMessage
public ObjectMessage createObjectMessage(Serializable object)
- Specified by:
createObjectMessagein interfaceJMSContext
-
createTextMessage
public TextMessage createTextMessage()
- Specified by:
createTextMessagein interfaceJMSContext
-
createTextMessage
public TextMessage createTextMessage(String text)
- Specified by:
createTextMessagein interfaceJMSContext
-
createStreamMessage
public StreamMessage createStreamMessage()
- Specified by:
createStreamMessagein interfaceJMSContext
-
createTemporaryQueue
public TemporaryQueue createTemporaryQueue()
- Specified by:
createTemporaryQueuein interfaceJMSContext
-
createTemporaryTopic
public TemporaryTopic createTemporaryTopic()
- Specified by:
createTemporaryTopicin interfaceJMSContext
-
createQueue
public Queue createQueue(String name)
- Specified by:
createQueuein interfaceJMSContext
-
createTopic
public Topic createTopic(String name)
- Specified by:
createTopicin interfaceJMSContext
-
getAutoStart
public boolean getAutoStart()
- Specified by:
getAutoStartin interfaceJMSContext
-
setAutoStart
public void setAutoStart(boolean autoStart)
- Specified by:
setAutoStartin interfaceJMSContext
-
createConsumer
public JMSConsumer createConsumer(Destination destination)
- Specified by:
createConsumerin interfaceJMSContext
-
createConsumer
public JMSConsumer createConsumer(Destination destination, String selector)
- Specified by:
createConsumerin interfaceJMSContext
-
createConsumer
public JMSConsumer createConsumer(Destination destination, String selector, boolean noLocal)
- Specified by:
createConsumerin interfaceJMSContext
-
createBrowser
public QueueBrowser createBrowser(Queue queue)
- Specified by:
createBrowserin interfaceJMSContext
-
createBrowser
public QueueBrowser createBrowser(Queue queue, String messageSelector)
- Specified by:
createBrowserin interfaceJMSContext
-
createDurableConsumer
public JMSConsumer createDurableConsumer(Topic topic, String name)
- Specified by:
createDurableConsumerin interfaceJMSContext
-
createDurableConsumer
public JMSConsumer createDurableConsumer(Topic topic, String name, String selector, boolean noLocal)
- Specified by:
createDurableConsumerin interfaceJMSContext
-
createProducer
public JMSProducer createProducer()
- Specified by:
createProducerin interfaceJMSContext
-
createSharedConsumer
public JMSConsumer createSharedConsumer(Topic topic, String sharedSubscriptionName)
- Specified by:
createSharedConsumerin interfaceJMSContext
-
createSharedConsumer
public JMSConsumer createSharedConsumer(Topic topic, String sharedSubscriptionName, String messageSelector)
- Specified by:
createSharedConsumerin interfaceJMSContext
-
createSharedDurableConsumer
public JMSConsumer createSharedDurableConsumer(Topic topic, String name)
- Specified by:
createSharedDurableConsumerin interfaceJMSContext
-
createSharedDurableConsumer
public JMSConsumer createSharedDurableConsumer(Topic topic, String name, String messageSelector)
- Specified by:
createSharedDurableConsumerin interfaceJMSContext
-
getCopyOfSession
private Session getCopyOfSession()
-
getSessionMode
public int getSessionMode()
JMS 2.0 API method. Returns the session mode of the JMSContext's session. This can be set at the time that the JMSContext is created. Possible values are JMSContext.SESSION_TRANSACTED, JMSContext.AUTO_ACKNOWLEDGE, JMSContext.CLIENT_ACKNOWLEDGE and JMSContext.DUPS_OK_ACKNOWLEDGEIf a session mode was not specified when the JMSContext was created a value of JMSContext.AUTO_ACKNOWLEDGE will be returned.
- Specified by:
getSessionModein interfaceJMSContext- Returns:
- the session mode of the JMSContext's session
- Throws:
JMSRuntimeException- if the JMS provider fails to return the acknowledgment mode due to some internal error.- Since:
- JMS 2.0
- See Also:
Connection.createSession(boolean, int)
-
getTransacted
public boolean getTransacted()
- Specified by:
getTransactedin interfaceJMSContext
-
getMetaData
public ConnectionMetaData getMetaData()
- Specified by:
getMetaDatain interfaceJMSContext
-
getClientID
public String getClientID()
- Specified by:
getClientIDin interfaceJMSContext
-
setClientID
public void setClientID(String clientID)
- Specified by:
setClientIDin interfaceJMSContext
-
getExceptionListener
public ExceptionListener getExceptionListener()
- Specified by:
getExceptionListenerin interfaceJMSContext
-
setExceptionListener
public void setExceptionListener(ExceptionListener listener)
- Specified by:
setExceptionListenerin interfaceJMSContext
-
start
public void start()
- Specified by:
startin interfaceJMSContext
-
stop
public void stop()
Temporarily stops the delivery of incoming messages by the JMSContext's connection. Delivery can be restarted using thestartmethod. When the connection is stopped, delivery to all the connection's message consumers is inhibited: synchronous receives block, and messages are not delivered to message listeners.This call blocks until receives and/or message listeners in progress have completed.
Stopping a connection has no effect on its ability to send messages. A call to
stopon a connection that has already been stopped is ignored.A call to
stopmust not return until delivery of messages has paused. This means that a client can rely on the fact that none of its message listeners will be called and that all threads of control waiting forreceivecalls to return will not return with a message until the connection is restarted. The receive timers for a stopped connection continue to advance, so receives may time out while the connection is stopped.If message listeners are running when
stopis invoked, thestopcall must wait until all of them have returned before it may return. While these message listeners are completing, they must have the full services of the connection available to them.A message listener must not attempt to stop its own JMSContext as this would lead to deadlock. The JMS provider must detect this and throw a IllegalStateRuntimeException
For the avoidance of doubt, if an exception listener for the JMSContext's connection is running when
stopis invoked, there is no requirement for thestopcall to wait until the exception listener has returned before it may return.This method must not be used in a Java EE web or EJB application. Doing so may cause a
JMSRuntimeExceptionto be thrown though this is not guaranteed.This method must not be used if the
JMSContextis container-managed (injected). Doing so will cause aIllegalStateRuntimeExceptionto be thrown.- Specified by:
stopin interfaceJMSContext- Throws:
IllegalStateRuntimeException-- if this method has been called by a MessageListener on its own JMSContext
- if the
JMSContextis container-managed (injected).
JMSRuntimeException- if the JMS provider fails to stop message delivery for one of the following reasons:- an internal error has occurred or
- this method has been called in a Java EE web or EJB application (though it is not guaranteed that an exception is thrown in this case)
- See Also:
JMSContext.start()
-
unsubscribe
public void unsubscribe(String name)
- Specified by:
unsubscribein interfaceJMSContext
-
getSession
Session getSession()
-
-