!--a11y-->
Creating Message Consumer to a Topic 
The message consumer for a topic connection is TopicSubscriber. The topic subscriber must be applied to a topic at creation time. You can choose to use the TopicSubscriber in two ways:
· To register the class as javax.jms.MessageListener instance for asynchronous message reception.
· To use it to receive messages synchronously.
If you want to receive messages asynchronously, you must register the class as a javax.jms.MessageListener instance. This enables you to receive and unpack the messages using the onMessage(javax.jms.Message message) method:

|
// create subscriber to a topic TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
// set message listener topicSubscriber.setMessageListener( this );
public void onMessage(javax.jms.Message message) { // receive and unpack the message |
For an example source on receiving messages asynchronously, see Asynchronously Receiving Messages Sent to a Topic.
If you want to receive messages synchronously, use the receive() method. Apply it to the message you want to receive.

|
// create subscriber to a topic TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
// receive the message TextMessage textMessage = (TextMessage) topicSubscriber.receive(); |
For an example source on receiving messages synchronously, see Synchronously Receiving Messages Sent to a Topic.
