Messaging sample

This topic describes some sample applications that demonstrate a simple messaging scenario.

Overview

The simple scenario consists of three applications and is outlined in the following diagram.

Image containing the simple messaging topology described below.

zero.messaging.app1.demo
This application, app1, uses the Connection and the messaging protocol to send a message to a messaging queue hosted by a broker application, broker1.
zero.messaging.broker1.demo
This long running application, broker1, contains the messaging broker. It also contains a messaging kicker which is monitoring the queue and which will notify app2 when a message arrives.
zero.messaging.app2.demo
This application, app2, contains a messaging receiver which passes the message to the application code in a message event handler.

Creating the sample applications

After installing and configuring the command line tools you can create the three demonstration applications using the following commands:

zero create zero.messaging.broker1.demo from zero:zero.messaging.broker1.demo
zero create zero.messaging.app1.demo from zero:zero.messaging.app1.demo
zero create zero.messaging.app2.demo from zero:zero.messaging.app2.demo

You may wish to examine the contents of the three applications at this time and note that:

  • The broker application, broker1, contains no application code. A messaging broker is configured by the inclusion of the zero.messaging.broker dependency and the broker configuration in zero.config.
  • The zero.config files of both messaging client applications, app1 and app2, contain broker connection configurations referring to the port on which broker1 will provide the messaging service.
  • The sending application, app1, contains a destination configuration for queue1 and a script, public/sendMessage.groovy, that uses the Connection API to send a simple message.
  • The receiving application, app2, contains a simple handler for the message event, app/scripts/onMessageHandler.groovy.

Running the sample scenario

Start each of the applications in the normal manner, using zero start.

The messaging client applications, app1 and app2, will behave as normal applications. They will begin listening on their configured ports but their full runtimes will not be started until an HTTP request arrives. However, the long running broker application will start immediately to provide the messaging service and the error-0.0.log file for broker1 will show the progress of the initialisation.

Once all three applications have been started, use a web browser to invoke the sendMessage.groovy in app1 to send a message using the URL, http://localhost:8081/sendMessage.groovy .

If it is not already active, the full runtime for app1 will be started and the sendMessage.groovy script will send a message to the queue. The response displayed by the browser will be similar to the following:

Sending message, "Message from zero.messaging.app1.demo at 01/05/08 20:00", to queue1
Message sent successfully. See zero.messaging.app2.demo for receipt.

After a few seconds, the messaging kicker will notice that a message has arrived on queue1 and send a notification to the messaging receiver in app2, using HTTP. If it is not already active, the full runtime for app2 will now be started and the messaging receiver will fire a message event for the new message. The handler registered for this event, onMessageHandler.groovy, writes to standard output. The result can be seen in the trace-0.0.log file for app2 and will be similar to the following:

STDOUT [ Message received on queue1, "Message from zero.messaging.app1.demo at 01/05/08 20:00" ]

When you have finished experimenting with the sample applications, they can each be stopped in the normal manner, using zero stop.

Version 1.0.0.3.25591