
To send messages to a certain topic, you have to create a topic publisher.
Creating a Message Producer
Create a topic publisher using the following code:
TopicPublisher topicPublisher = topicSession.createTopicPublisher(topic);
For more information, see Topic Publisher Example .
Creating a Message Consumer
Here we refer to synchronous and asynchronous todescribe how a JMS consumer application receives messages. The overall JMS client-to-client communication process itself is always asynchronous, since the producer application only waits until the message is received by the JMS Provider, not until the message is successfully received and processed by the consumer, to complete its next task.
You can use the TopicSubscriber in two ways:
// create subscriber to a topic
TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
// set message listener
topicSubscriber.setMessageListener(this);
public void onMessage(javax.jms.Message message) {
// process the messageFor an example of receiving messages asynchronously, see Asynchronously Receiving Messages Sent to a Topic .
// create subscriber to a topic TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); // receive a text message TextMessage textMessage = (TextMessage) topicSubscriber.receive();
For an example of receiving messages synchronously, see Synchronously Receiving Messages Sent to a Topic .