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
       connection.start();
       Message message = QueueReceiver.receive();
       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#
       connection.Start();
       Message message = QueueReceiver.receive();
       connection.Close();

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