Message Consumption:
Messages can be consumed in either of two ways:
• Synchronously:
A subscriber or a receiver explicitly fetches the message from the destination by calling the receive method.
The receive method can block until a message arrives or can time out if a message does not arrive
within a specified time limit.
• Asynchronously:
A client can register a message listener with a consumer. A message listener is similar to an event listener. Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener’s
onMessage() method.
JMS API Programming Model
The basic building blocks of a JMS application consist of
• Administered objects: connection factories and destinations
• Connections
• Sessions
• Message producers
• Message consumers
• Messages
Messages can be consumed in either of two ways:
• Synchronously:
A subscriber or a receiver explicitly fetches the message from the destination by calling the receive method.
The receive method can block until a message arrives or can time out if a message does not arrive
within a specified time limit.
• Asynchronously:
A client can register a message listener with a consumer. A message listener is similar to an event listener. Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener’s
onMessage() method.
JMS API Programming Model
The basic building blocks of a JMS application consist of
• Administered objects: connection factories and destinations
• Connections
• Sessions
• Message producers
• Message consumers
• Messages
Administered Objects:
Two parts of a JMS application—destinations and connection factories—are best maintained administratively rather than programmatically.
JMS clients access these objects through interfaces that are portable, so a client application can run with little or no change on more than one implementation of the JMS API. Ordinarily, an administrator configures administered objects in a Java Naming and Directory Interface (JNDI) API namespace, and JMS clients
ADMINISTERED OBJECTS 23 then look them up, using the JNDI API. J2EE applications always use the JNDI
API.
Connection Factories:
A connection factory is the object a client uses to create a connection with a provider. A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator.
Each connection factory is an instance of either the QueueConnectionFactory or the TopicConnectionFactory interface.
You can also create new connection factories by using the following commands:
j2eeadmin -addJmsFactory jndi_name queue
j2eeadmin -addJmsFactory jndi_name topic
For example, the following code fragment obtains an InitialContext object and uses it to look up the QueueConnection-Factory and the TopicConnectionFactory by name:
Context ctx = new InitialContext();
QueueConnectionFactory queueConnectionFactory =
(QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
Two parts of a JMS application—destinations and connection factories—are best maintained administratively rather than programmatically.
JMS clients access these objects through interfaces that are portable, so a client application can run with little or no change on more than one implementation of the JMS API. Ordinarily, an administrator configures administered objects in a Java Naming and Directory Interface (JNDI) API namespace, and JMS clients
ADMINISTERED OBJECTS 23 then look them up, using the JNDI API. J2EE applications always use the JNDI
API.
Connection Factories:
A connection factory is the object a client uses to create a connection with a provider. A connection factory encapsulates a set of connection configuration parameters that has been defined by an administrator.
Each connection factory is an instance of either the QueueConnectionFactory or the TopicConnectionFactory interface.
You can also create new connection factories by using the following commands:
j2eeadmin -addJmsFactory jndi_name queue
j2eeadmin -addJmsFactory jndi_name topic
For example, the following code fragment obtains an InitialContext object and uses it to look up the QueueConnection-Factory and the TopicConnectionFactory by name:
Context ctx = new InitialContext();
QueueConnectionFactory queueConnectionFactory =
(QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");