Implementing and installing message listeners

Message listeners allow you to receive messages asynchronously. Once you have implemented a listener, install it on a message consumer. When a message is delivered to the message consumer, the listener can send the message to other consumers or notify one or more components.

Message listeners are EAServer components that implement the CtsComponents::MessageListener interface, which contains only this onMessage method:

void onMessage(in CtsComponents::Message msg);

You can install message listeners two ways. The recommended way is to use EAServer Manager to install a message listener on a message queue or topic—see Chapter 8, “Setting up the Message Service,” in the EAServer System Administration Guide. You can also install a message listener within your application.

This example installs a message listener implemented by the EAServer component “MyPackage/MailService” on the message queue “MyClient:email”:

MessageService cms = getMessageService();
cms.addListener("MyClient:email",
                "MyPackage/MailService");

When you create a message listener, you can optionally provide the name of a thread pool. The thread pool must have one or more worker threads. A thread pool with multiple worker threads enables the message listener to process multiple messages at the same time. If you do not specify the name of a thread pool, the message listener uses the system default thread pool, which has a single worker thread.

This code sample adds a message listener and specifies “MailPool” as the name of the thread pool:

cms.addListener("MyClient:email",
                "MyPackage/MailService[MailPool]");