Creating message consumers

Message consumers receive messages that are either sent to a queue or published.

Beginning in version 6.0, EAServer provides a message consumer object, which you can use for receiving messages that are either sent or published. In earlier versions of EAServer, two types of message consumers were required, one for receiving messages that are sent to a queue and one for subscribing to published messages.

The following code fragment uses a previously created Session object mySession, and creates a MessageConsumer to retrieve messages that are sent to the message queue myQueue:

MessageConsumer myConsumer =
   mySession.createConsumer(myQueue);

A MessageConsumer object that acts as a topic subscriber receives published messages and can be either durable or nondurable. A nondurable subscriber can only receive published messages while it is connected to EAServer. If you want guaranteed message delivery, make the subscriber durable. For example, if you create a durable subscription on a topic, EAServer saves all published messages for the topic in a database. If a durable subscriber is temporarily disconnected from EAServer, its messages are delivered when the subscriber reconnects. Messages are deleted from the database only after they are delivered to the durable subscriber.

This example illustrates how to create both durable and nondurable topic subscribers. In both cases, reference previously created Topic and Session objects:

// Create a durable subscriber

MessageConsumer subscriber = 
   mySession.createDurableSubscriber(myTopic,
      “mySubscription”)

// Create a non-durable subscriber

MessageConsumer subscriber =
   mySession.createSubscriber(myTopic);

To remove a durable topic subscription, call the Session.unsubscribe method, and pass in the subscription name; for example:

mySession.unsubscribe(“subscriptionName”);

When you no longer need a message consumer, call MessageConsumer.close to release its resources.