Class JMSContext
- java.lang.Object
-
- org.ow2.joram.jakarta.jms.JMSContext
-
- All Implemented Interfaces:
jakarta.jms.JMSContext,AutoCloseable
public class JMSContext extends Object implements jakarta.jms.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 ContextConnectionconnectionprivate static booleanDEBUGprivate static Loggerloggerprivate Sessionsession
-
Constructor Summary
Constructors Constructor Description JMSContext(jakarta.jms.Connection cnx)Creates a new Context using a newly created JMS connection.JMSContext(jakarta.jms.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidacknowledge()voidclose()API method.voidcommit()jakarta.jms.QueueBrowsercreateBrowser(jakarta.jms.Queue queue)jakarta.jms.QueueBrowsercreateBrowser(jakarta.jms.Queue queue, String messageSelector)jakarta.jms.BytesMessagecreateBytesMessage()jakarta.jms.JMSConsumercreateConsumer(jakarta.jms.Destination destination)jakarta.jms.JMSConsumercreateConsumer(jakarta.jms.Destination destination, String selector)jakarta.jms.JMSConsumercreateConsumer(jakarta.jms.Destination destination, String selector, boolean noLocal)jakarta.jms.JMSContextcreateContext(int sessionMode)API method.jakarta.jms.JMSConsumercreateDurableConsumer(jakarta.jms.Topic topic, String name)JMSConsumercreateDurableConsumer(jakarta.jms.Topic topic, String name, String selector, boolean noLocal)jakarta.jms.MapMessagecreateMapMessage()jakarta.jms.MessagecreateMessage()jakarta.jms.ObjectMessagecreateObjectMessage()jakarta.jms.ObjectMessagecreateObjectMessage(Serializable object)JMSProducercreateProducer()jakarta.jms.QueuecreateQueue(String name)jakarta.jms.JMSConsumercreateSharedConsumer(jakarta.jms.Topic topic, String sharedSubscriptionName)jakarta.jms.JMSConsumercreateSharedConsumer(jakarta.jms.Topic topic, String sharedSubscriptionName, String messageSelector)jakarta.jms.JMSConsumercreateSharedDurableConsumer(jakarta.jms.Topic topic, String name)jakarta.jms.JMSConsumercreateSharedDurableConsumer(jakarta.jms.Topic topic, String name, String messageSelector)jakarta.jms.StreamMessagecreateStreamMessage()jakarta.jms.TemporaryQueuecreateTemporaryQueue()jakarta.jms.TemporaryTopiccreateTemporaryTopic()jakarta.jms.TextMessagecreateTextMessage()jakarta.jms.TextMessagecreateTextMessage(String text)jakarta.jms.TopiccreateTopic(String name)booleangetAutoStart()StringgetClientID()private SessiongetCopyOfSession()jakarta.jms.ExceptionListenergetExceptionListener()jakarta.jms.ConnectionMetaDatagetMetaData()(package private) SessiongetSession()intgetSessionMode()JMS 2.0 API method.booleangetTransacted()voidrecover()voidrollback()voidsetAutoStart(boolean autoStart)voidsetClientID(String clientID)voidsetExceptionListener(jakarta.jms.ExceptionListener listener)voidstart()voidstop()Temporarily stops the delivery of incoming messages by the JMSContext's connection.voidunsubscribe(String name)
-
-
-
Field Detail
-
logger
private static final Logger logger
-
DEBUG
private static final boolean DEBUG
-
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(jakarta.jms.Connection cnx)
Creates a new Context using a newly created JMS connection.- Parameters:
cnx- the created JMS connection.
-
JMSContext
public JMSContext(jakarta.jms.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 jakarta.jms.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 interfacejakarta.jms.JMSContext- 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:
jakarta.jms.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 interfacejakarta.jms.JMSContext- Throws:
jakarta.jms.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)
jakarta.jms.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 interfacejakarta.jms.JMSContext
-
commit
public void commit()
- Specified by:
commitin interfacejakarta.jms.JMSContext
-
recover
public void recover()
- Specified by:
recoverin interfacejakarta.jms.JMSContext
-
rollback
public void rollback()
- Specified by:
rollbackin interfacejakarta.jms.JMSContext
-
createMessage
public jakarta.jms.Message createMessage()
- Specified by:
createMessagein interfacejakarta.jms.JMSContext
-
createBytesMessage
public jakarta.jms.BytesMessage createBytesMessage()
- Specified by:
createBytesMessagein interfacejakarta.jms.JMSContext
-
createMapMessage
public jakarta.jms.MapMessage createMapMessage()
- Specified by:
createMapMessagein interfacejakarta.jms.JMSContext
-
createObjectMessage
public jakarta.jms.ObjectMessage createObjectMessage()
- Specified by:
createObjectMessagein interfacejakarta.jms.JMSContext
-
createObjectMessage
public jakarta.jms.ObjectMessage createObjectMessage(Serializable object)
- Specified by:
createObjectMessagein interfacejakarta.jms.JMSContext
-
createTextMessage
public jakarta.jms.TextMessage createTextMessage()
- Specified by:
createTextMessagein interfacejakarta.jms.JMSContext
-
createTextMessage
public jakarta.jms.TextMessage createTextMessage(String text)
- Specified by:
createTextMessagein interfacejakarta.jms.JMSContext
-
createStreamMessage
public jakarta.jms.StreamMessage createStreamMessage()
- Specified by:
createStreamMessagein interfacejakarta.jms.JMSContext
-
createTemporaryQueue
public jakarta.jms.TemporaryQueue createTemporaryQueue()
- Specified by:
createTemporaryQueuein interfacejakarta.jms.JMSContext
-
createTemporaryTopic
public jakarta.jms.TemporaryTopic createTemporaryTopic()
- Specified by:
createTemporaryTopicin interfacejakarta.jms.JMSContext
-
createQueue
public jakarta.jms.Queue createQueue(String name)
- Specified by:
createQueuein interfacejakarta.jms.JMSContext
-
createTopic
public jakarta.jms.Topic createTopic(String name)
- Specified by:
createTopicin interfacejakarta.jms.JMSContext
-
getAutoStart
public boolean getAutoStart()
- Specified by:
getAutoStartin interfacejakarta.jms.JMSContext
-
setAutoStart
public void setAutoStart(boolean autoStart)
- Specified by:
setAutoStartin interfacejakarta.jms.JMSContext
-
createConsumer
public jakarta.jms.JMSConsumer createConsumer(jakarta.jms.Destination destination)
- Specified by:
createConsumerin interfacejakarta.jms.JMSContext
-
createConsumer
public jakarta.jms.JMSConsumer createConsumer(jakarta.jms.Destination destination, String selector)- Specified by:
createConsumerin interfacejakarta.jms.JMSContext
-
createConsumer
public jakarta.jms.JMSConsumer createConsumer(jakarta.jms.Destination destination, String selector, boolean noLocal)- Specified by:
createConsumerin interfacejakarta.jms.JMSContext
-
createBrowser
public jakarta.jms.QueueBrowser createBrowser(jakarta.jms.Queue queue)
- Specified by:
createBrowserin interfacejakarta.jms.JMSContext
-
createBrowser
public jakarta.jms.QueueBrowser createBrowser(jakarta.jms.Queue queue, String messageSelector)- Specified by:
createBrowserin interfacejakarta.jms.JMSContext
-
createDurableConsumer
public jakarta.jms.JMSConsumer createDurableConsumer(jakarta.jms.Topic topic, String name)- Specified by:
createDurableConsumerin interfacejakarta.jms.JMSContext
-
createDurableConsumer
public JMSConsumer createDurableConsumer(jakarta.jms.Topic topic, String name, String selector, boolean noLocal)
- Specified by:
createDurableConsumerin interfacejakarta.jms.JMSContext
-
createProducer
public JMSProducer createProducer()
- Specified by:
createProducerin interfacejakarta.jms.JMSContext
-
createSharedConsumer
public jakarta.jms.JMSConsumer createSharedConsumer(jakarta.jms.Topic topic, String sharedSubscriptionName)- Specified by:
createSharedConsumerin interfacejakarta.jms.JMSContext
-
createSharedConsumer
public jakarta.jms.JMSConsumer createSharedConsumer(jakarta.jms.Topic topic, String sharedSubscriptionName, String messageSelector)- Specified by:
createSharedConsumerin interfacejakarta.jms.JMSContext
-
createSharedDurableConsumer
public jakarta.jms.JMSConsumer createSharedDurableConsumer(jakarta.jms.Topic topic, String name)- Specified by:
createSharedDurableConsumerin interfacejakarta.jms.JMSContext
-
createSharedDurableConsumer
public jakarta.jms.JMSConsumer createSharedDurableConsumer(jakarta.jms.Topic topic, String name, String messageSelector)- Specified by:
createSharedDurableConsumerin interfacejakarta.jms.JMSContext
-
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 interfacejakarta.jms.JMSContext- Returns:
- the session mode of the JMSContext's session
- Throws:
jakarta.jms.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 interfacejakarta.jms.JMSContext
-
getMetaData
public jakarta.jms.ConnectionMetaData getMetaData()
- Specified by:
getMetaDatain interfacejakarta.jms.JMSContext
-
getClientID
public String getClientID()
- Specified by:
getClientIDin interfacejakarta.jms.JMSContext
-
setClientID
public void setClientID(String clientID)
- Specified by:
setClientIDin interfacejakarta.jms.JMSContext
-
getExceptionListener
public jakarta.jms.ExceptionListener getExceptionListener()
- Specified by:
getExceptionListenerin interfacejakarta.jms.JMSContext
-
setExceptionListener
public void setExceptionListener(jakarta.jms.ExceptionListener listener)
- Specified by:
setExceptionListenerin interfacejakarta.jms.JMSContext
-
start
public void start()
- Specified by:
startin interfacejakarta.jms.JMSContext
-
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 interfacejakarta.jms.JMSContext- Throws:
jakarta.jms.IllegalStateRuntimeException-- if this method has been called by a MessageListener on its own JMSContext
- if the
JMSContextis container-managed (injected).
jakarta.jms.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 interfacejakarta.jms.JMSContext
-
getSession
Session getSession()
-
-