Receiving messages asynchronously

To receive messages asynchronously using the .NET, C++, and Java APIs, you can write and register a message listener function that is called by QAnywhere when a message appears in the queue. The message listener takes the incoming message as a parameter. The task you perform in your message listener depends on your application. For example, in the TestMessage sample application the message listener adds the message to the list of messages in the main TestMessage window.

Development tip for .NET, C++ and Java

It is safer to use QAManagers in mode EXPLICIT_ACKNOWLEDGEMENT to guard against the possibility of an application error occurring part way through the processing of received messages and the message being acknowledged anyway.

If the QAManager is opened in mode EXPLICIT_ACKNOWLEDGEMENT, the message can be acknowledged in the onMessage method only after it has been successfully processed. That way if there was an error processing the message, the message is received again because it was not acknowledged.

If the QAManager is opened in mode IMPLICIT_ACKNOWLEDGEMENT, the message passed to onMessage is acknowledged implicitly when onMessage returns. If the user application encounters an error while processing the message, the message is acknowledged and never received again.

To receive messages asynchronously (.NET)

  1. Implement a message handler method.

    private void onMessage(QAMessage msg) {
        // Process message.
    }
  2. Register the message handler.

    To register a message handler, create a QAManager.MessageListener object that has the message handler function as its argument. Then use the QAManager.SetMessageListener function to register the MessageListener with a specific queue. In the following example, queue-name is a string that is the name of the queue the QAManager object listens to.

    MessageListener listener;
    listener = new MessageListener( onMessage );
    mgr.SetMessageListener( "queue-name", listener );

    See MessageListener delegate and SetMessageListener method.

To receive messages asynchronously (C++)

  1. Create a class that implements the QAMessageListener interface.

    class MyClass: public QAMessageListener {
       public:
       void onMessage( QAMessage * Msg);
    };

    See QAMessageListener class.

  2. Implement the onMessage method.

    The QAMessageListener interface contains one method, onMessage. Each time a message arrives in the queue, the QAnywhere library calls this method, passing the new message as the single argument.

    void MyClass::onMessage(QAMessage * msg) {
         // Process message.
    }
  3. Register the message listener.

    my_listener = new MyClass();
    mgr->setMessageListener( "queue-name", my_listener );

    See setMessageListener function.

To receive a message asynchronously (Java)

  1. Implement a message handler method and an exception handler method.

    class MyClass implements QAMessageListener {
      public void onMessage(QAMessage message) {
        // Process the message.
      }
      public void onException(
        QAException exception, QAMessage message) {
        // Handle the exception.
      }
    }
  2. Register the message handler.

    MyClass listener = new MyClass();
    mgr.setMessageListener("queue-name", listener);

See Interface QAMessageListener and setMessageListener method.

To receive messages asynchronously (SQL)

  • Create a stored procedure with the name ml_qa_listener_queue, where queue is the name of a message queue.

    This procedure is called whenever a message is queued on the given queue.

    See ml_qa_listener_queue.