Receive Messages

A Message Consumer receives messages from a destination and acknowledges the receipt of messages using the mode established for the session, as described in Create a Session.

Before receiving messages, the Message Consumer must start the connection to the EMS server. Before exiting, the Message Consumer must close the connection.

The following examples start the connection created in Connect to the EMS Server; synchronously receive messages from the queue created in Dynamically Create Topics and Queues, and then close the connection.

Note: You can also implement a Message Listener for your Message Consumer to asynchronously receive messages, as described in Create a Message Listener for Asynchronous Message Consumption.
  • Java

    Use the Connection object’s start() method to start the connection:

       connection.start();

    Use the MessageConsumer object’s receive() method to receive a message. This is typically used in a loop for the duration the client wishes to receive messages:

       Message message = QueueReceiver.receive();

    When the client has finished receiving messages, it uses the Close() method to close the connection:

       connection.close();

    See the tibjmsMsgConsumer.java sample client for a working example.

  • C

    Use the tibemsConnection_Start function to start the connection:

       status = tibemsConnection_Start(connection);

    Use the tibemsMsgConsumer_Receive function to receive a message. This is typically used in a loop for the duration the client wishes to receive messages:

       tibemsMsg message = NULL;
       status = tibemsMsgConsumer_Receive(QueueReceiver,&message);

    When the client has finished receiving messages, use the tibemsConnection_Close function to close the connection:

       status = tibemsConnection_Close(connection);

    See the tibemsMsgConsumer.c sample client for a working example.

  • C#

    Use the Connection.Start function to start the connection:

       connection.Start();

    Use the MessageConsumer.Receive function to receive a message. This is typically used in a loop for the duration the client wishes to receive messages:

       Message message = QueueReceiver.receive();

    When the client has finished receiving messages, use the Connection.Close function to close the connection:

       connection.Close();

    See the csMsgConsumer.cs sample client for a working example.