Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

3.4 Message Producers

A message producer is an object created by a session and is used for sending messages to a destination. The PTP form of a message producer implements the QueueSender interface. The pub/sub form implements the TopicPublisher interface.

For example, you use a QueueSession to create a sender for the queue myQueue, and you use a TopicSession to create a publisher for the topic myTopic:

QueueSender queueSender = queueSession.createSender(myQueue);
TopicPublisher topicPublisher = 
 topicSession.createPublisher(myTopic);

You can create an unidentified producer by specifying null as the argument to createSender or createPublisher. With an unidentified producer, you can wait to specify which destination to send the message to until you send or publish a m_essage.

Once you have created a message producer, you can use it to send messages. (You have to create the messages first; see Section 3.6 on page 23.) With a QueueSender, you use the send method:

queueSender.send(message);

With a TopicPublisher, you use the publish method:

topicPublisher.publish(message);

If you created an unidentified producer, use the overloaded send or publish method that specifies the destination as the first parameter.

  • + Share This
  • 🔖 Save To Your Account