Feeds

Programming message services in Java

Asynchronous interactions seem to be the way of the future

Boost IT visibility and business value

What is a message service?

So what is a message service? A message service provides for distributed, loosely coupled communication between software components or applications. That is, it allows an application running on one host using one particular technology to communicate with another application running on another host, without the two clients needing to know anything directly about each other.

The communication is achieved by an intermediary, called the queue, on which a message can be placed, and from which messages can be read. As the intermediary is a queue, the general rule is that messages are read form the queue in a First In/First Out (or FIFO) manner.

Illustrates typical message queuing.

For example, in the above diagram (Figure 1), two message producers send messages to a queue. The queue sends the message onto the message consumer in a different thread of execution. Thus, the producers are not blocked while the consumer is processing the message. The messages may be held in the queue until the consumer is available to process them and delivery of the message to the consumer may be guaranteed (the promise of IBM’s MQ Series, for example, to deliver messages once and once only, was an important contribution to processing integrity when it first came out – Ed).

In addition, a message service may support many-to-one connections as above, or many-to-many as supported by the JMS topic concept. Both queues and topics can alter the order in which messages are retrieved using concepts such as priorities etc.

Implementing the producers and consumers

So, now we have the basic concepts of a message service, how do we use JMS to create producers and consumers of messages using the JMS API? To explore these concepts, I’ll step through the definition of a message producer and the implementation of an EJB3 based Message Driven Bean. Note that I could just implement a stand alone message consumer application, (it does not need to be an EJB) however, in my experience in a Java Enterprise Edition environment, it’s much more common to use an EJB for this; and EJBs make the whole process so much easier.

As the application server used to support our example, I’ll be using JBoss (in particular, I’m using JBoss 4.2.1) and the JBossMQ message service. Note that it’s also possible with JBOSS 4.2.1 to use the newer JBOSS Messaging JMS provider, which can give better performance than the older JBossMQ system and which replaces it entirely in JBoss 5.x. However, this requires some configuration and from a Java perspective it is hidden behind the façade of the JMS API.

Having sorted our message service provider, we can now consider the steps we must follow to create our JMS based application. These steps are presented below:

  1. Publish destinations (queues) using a server-specified mechanism. This can be done in JBoss using the JBossmq-destinations-service.xml file. Essentially this is a JNDI entry that is used to define a queue (or topic) as a destination.
  2. Define producers that generate messages.
  3. Define consumers that receive messages (note there is nothing to stop a client being both a producer and a consumer).
  4. Start the message server.
  5. Compile and start the producers and consumers.

We shall follow each of these steps below for a simple queue based application.

Build a business case: developing custom apps

More from The Register

next story
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
Leaked Windows Phone 8.1 Update specs tease details of Nokia's next mobes
New screen sizes, dual SIMs, voice over LTE, and more
Mozilla keeps its Beard, hopes anti-gay marriage troubles are now over
Plenty on new CEO's todo list – starting with Firefox's slipping grasp
Apple: We'll unleash OS X Yosemite beta on the MASSES on 24 July
Starting today, regular fanbois will be guinea pigs, it tells Reg
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Cloudy CoreOS Linux distro declares itself production-ready
Lightweight, container-happy Linux gets first Stable release
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
The Essential Guide to IT Transformation
ServiceNow discusses three IT transformations that can help CIO's automate IT services to transform IT and the enterprise.
Maximize storage efficiency across the enterprise
The HP StoreOnce backup solution offers highly flexible, centrally managed, and highly efficient data protection for any enterprise.