Feeds

Programming message services in Java

Asynchronous interactions seem to be the way of the future

New hybrid storage solutions

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.

Security for virtualized datacentres

More from The Register

next story
Not appy with your Chromebook? Well now it can run Android apps
Google offers beta of tricky OS-inside-OS tech
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
Greater dev access to iOS 8 will put us AT RISK from HACKERS
Knocking holes in Apple's walled garden could backfire, says securo-chap
NHS grows a NoSQL backbone and rips out its Oracle Spine
Open source? In the government? Ha ha! What, wait ...?
Google extends app refund window to two hours
You now have 120 minutes to finish that game instead of 15
Intel: Hey, enterprises, drop everything and DO HADOOP
Big Data analytics projected to run on more servers than any other app
prev story

Whitepapers

Providing a secure and efficient Helpdesk
A single remote control platform for user support is be key to providing an efficient helpdesk. Retain full control over the way in which screen and keystroke data is transmitted.
Top 5 reasons to deploy VMware with Tegile
Data demand and the rise of virtualization is challenging IT teams to deliver storage performance, scalability and capacity that can keep up, while maximizing efficiency.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.
Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.