Feeds

Programming message services in Java

Asynchronous interactions seem to be the way of the future

The essential guide to IT transformation

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.

Secure remote control for conventional and virtual desktops

More from The Register

next story
Apple promises to lift Curse of the Drained iPhone 5 Battery
Have you tried turning it off and...? Never mind, here's a replacement
Mozilla's 'Tiles' ads debut in new Firefox nightlies
You can try turning them off and on again
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
Scratched PC-dispatch patch patched, hatched in batch rematch
Windows security update fixed after triggering blue screens (and screams) of death
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
prev story

Whitepapers

Endpoint data privacy in the cloud is easier than you think
Innovations in encryption and storage resolve issues of data privacy and key requirements for companies to look for in a solution.
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.
Advanced data protection for your virtualized environments
Find a natural fit for optimizing protection for the often resource-constrained data protection process found in virtual environments.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Next gen security for virtualised datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.