background image

JMS Message Consumers

<< JMS Sessions | JMS Message Listeners >>
<< JMS Sessions | JMS Message Listeners >>

JMS Message Consumers

MessageProducer producer = session.createProducer(dest);
MessageProducer producer = session.createProducer(queue);
MessageProducer producer = session.createProducer(topic);
You can create an unidentified producer by specifying null as the argument to
createProducer
. With an unidentified producer, you do not specify a destination until you
send a message.
After you have created a message producer, you can use it to send messages by using the send
method:
producer.send(message);
You must first create the messages; see
"JMS Messages" on page 907
.
If you created an unidentified producer, use an overloaded send method that specifies the
destination as the first parameter. For example:
MessageProducer anon_prod = session.createProducer(null);
anon_prod.send(dest, message);
JMS Message Consumers
A message consumer is an object that is created by a session and used for receiving messages sent
to a destination. It implements the MessageConsumer interface.
A message consumer allows a JMS client to register interest in a destination with a JMS
provider. The JMS provider manages the delivery of messages from a destination to the
registered consumers of the destination.
For example, you could use a Session to create a MessageConsumer for a Destination object, a
Queue
object, or a Topic object:
MessageConsumer consumer = session.createConsumer(dest);
MessageConsumer consumer = session.createConsumer(queue);
MessageConsumer consumer = session.createConsumer(topic);
You use the Session.createDurableSubscriber method to create a durable topic subscriber.
This method is valid only if you are using a topic. For details, see
"Creating Durable
Subscriptions" on page 942
.
After you have created a message consumer, it becomes active, and you can use it to receive
messages. You can use the close method for a MessageConsumer to make the message
consumer inactive. Message delivery does not begin until you start the connection you created
by calling its start method. (Remember always to call the start method; forgetting to start the
connection is one of the most common JMS programming errors.)
The JMS API Programming Model
Chapter 31 · The Java Message Service API
905