# Create a Completion Listener for Asynchronous Sending

TIBCO Enterprise Message Service provides APIs for a Message Producer to send messages either synchronously or asynchronously. For asynchronous sending, you need to implement a `CompletionListener` that serves as an asynchronous event handler for message send result notification.

A completion listener implementation has two methods: onCompletion() is invoked after a message has successfully been sent, and onException() is invoked if the send failed. These methods are invoked in a different thread from that in which the message was sent. You implement the methods to perform the desired actions when the application is notified of send success or failure. Your implementation should handle all exceptions, and it should not throw any exceptions.

Once you create a completion listener, you pass it as an argument into the `MessageProducer` send method, or into the JMSProducer setAsync() method. If passed into the JMSProducer `setAsync` method, the JMSProducer will always send asynchronously.

- Java

  Create an implementation of the CompletionListener interface, create a CompletionListener and pass that into the appropriate send method:

      /* create connection, session, producer, message */
      TibjmsCompletionListener completionListener = new
        TibjmsCompletionListener();
      msgProducer.send(destination, msg, completionListener);

  Create a CompletionListener class and Implement the `onCompletion()` and `onException()` method to perform the desired actions when a message arrives:

      class TibjmsCompletionListener implements CompletionListener
      {
        public void onCompletion(Message msg)
        {
          /* Handle the send success case for the message */
        }
        public void onException(Message msg, Exception ex)
        {
          /* Handle the send failure case for the message */
        }
      }

  See the `tibjmsMsgProducer.java` sample client for a working example.

- C

  In C, Implement an `onCompletion()` function to perform the desired actions when a message is sent:

      static void
      onCompletion(tibemsMsg msg, tibems_status status, void* closure)
      {
          if (status == TIBEMS_OK)
          {
              /* Handle the send success case for the message */
          }
          else
          {
              /* Handle the send failure case for the message */
          }
      }
      /* Create a connection, session, and producer. When sending, pass
       * the onCompletion() function as the tibemsMsgCompletionCallback
       */
      status = tibemsMsgProducer_AsyncSend(producer, msg, onCompletion, NULL);

  See the `tibemsMsgProducer.c` sample client for a working example.

- C#

  Create an implementation of the `ICompletionListener` interface, create a `CompletionListener` and pass that into the appropriate send method.

      EMSCompletionListener completionListener = new EMSCompletionListener();
      producer.Send(destination, msg, completionListener);

  Create an implementation of the `IMessageListener` interface to perform actions when a message is sent:

      class EMSCompletionListener : ICompletionListener
      {
        public void OnCompletion(Message msg)
        {
          /* Handle the send success case for the message */
        }
        public void OnException(Message msg, Exception ex)
        {
          /* Handle the send failure case for the message */
        }
      }

  See the `csMsgProducer.cs` sample client for a working example.

Copyright © Cloud Software Group, Inc. All rights reserved.