Building Asynchronous Applications and Web Services using Java Messaging
With the advent of J2EE 1.3, we've become familiar with the message-driven bean (MDB) as a key architecture component.
The use of asynchronous messaging is an aid to application scalability.
There are additional ways in which messaging can be used to build robust Java applications and Web services.
Enterprises are currently faced with the challenges of 1) time-to-market, 2) data distribution, and 3) business flexibility.
Globally dispersed operations and end users: Applications work across the Internet and must be reliable, scalable, and easily manageable. In addition, in a rapid and unpredictably changing business environment,
architects need to provide for frequent changes during the lifetime of a deployment.
Demanding architectural requirements:
These requirements typically involve the Internet, the J2EE platform, .NET, Web services, and multiple-legacy execution environments.
How can you use asynchronous Java messaging to meet these challenges? I will now explain how to use asynchronous messaging in Java applications and Web services.
I will also discuss
the advantages of an asynchronous model,
when to adopt a message-based model, and
typical applications of these techniques.
A Simple Asynchronous Model
The basic constructs of a simple asynchronous programming model include
the synchronization and locking of shared data.
These constructs are used in multithreaded programs within a single Java VM process. The same basic model applies with minor changes to C# and .NET.
To any multithreaded model - except the construct names are changed. Java's synchronized keyword is replaced with C#'s lock to
control access to critical blocks of code.
Essentially, events, listeners, synchronized data objects, and code blocks are today's more flexible and sophisticated versions of
The asynchronous model can certainly increase throughput and reduce latency (response times) even on single-processor systems, as different
threads can simultaneously consume
On multiprocessor machines other related processes (including database shadow processes) can also execute in parallel.
Figure 1 shows some typical interactions between layers in a J2EE architecture.
The gold bars highlight periods where synchronous activities may be blocked, waiting for return of information from other layers.
If you can shorten the critical path of the overall process by either returning control faster or moving work into the gold sections, you
can reduce the overall response time.
You can also offload work into longer-running background threads; for example, in principle the Place Order EJB could choose not to wait
for the database update to complete before returning control to the servlet.
Program threads interact through shared data (in process, or external) or using passed-in parameters.
The more the various threads share data, the tighter the coupling; it becomes much harder to maintain the components separately.
Shared data also raises the possibility of data corruption; locks or semaphores (synchronization) must be used to avoid the possibility
of two threads simultaneously updating data objects.
This complicates the programming model and tends to reduce performance as threads serialize (block and wait) as they queue up for access
to locked data items.
When to Adopt Messaging
Question: How does moving to a multiprocess, message-based asynchronous model add significant benefits to your applications?
Figure 2 shows the most basic reason for executing a business transaction using multiple processes.
In this simple example, three actors are involved:
the Agent, and
Each has control over its own activities - but no one is in charge of the whole activity of communication.
The various parties exchange documents (business messages, if you like) to trigger each stage of the overall business transaction.
This is because there's no single database they can all share.
This style of interaction can be called
to distinguish it from the database-centric
model commonly used to build internal systems such as ERP and CRM.
The document (the message passed from system to system) contains all necessary data items and takes the same role as input parameters in a
well-structured function call.
No actor in a value chain like this can safely make assumptions about specific vendor or technology-platform decisions made by his
Each actor is a transaction island and interaction has to be loosely coupled.
In document-centric process models (Web services are a prime example), business conversations are characterized by the different ( maybe interlocking or interlaced) transaction scopes at work.
In this type of system, messaging is used to safely pass control between the different actors without tying together their individual physical transaction islands.
In the flight reservation example, any flow across the lanes (between actors) is implemented as a message.
Flows within a lane could also be messages - that depends on the scale of each separate subsystem.
An inevitable consequence of the separate development of these autonomous systems is that system A (the Agent) cannot know how long it
will take for system B (the Airline) to complete its part of the conversation (reserving seats and billing a credit card) before replying
with a flight confirmation message.
In these circumstances, a tightly coupled two-phase commit transaction simply isn't feasible - both technology and organizational
politics rule it out.
To be able to reverse the effect of any exceptions that may arise, you have to approach errors and corrections using compensating
Think of using your credit card in a store.
You buy a sweater, pay with your credit card, then notice that you've picked up the wrong size.
The sales assistant doesn't simply roll back your transaction. Instead, he or she performs a second, compensating transaction that
reverses the effect of the mistake.
You get both credit card slips, and you should see both the debit and the credit on your monthly statement. Any nontrivial business
conversation will anticipate the possibility of errors and need for corrections.
Figure 3, a RosettaNet model of a purchase order, shows how both the buyer and the seller can compensate for
production problems, or
lack of end-user demand
by varying the details of the order over a period of hours, days, weeks, or even months.
It also highlights that the purchase order is just one part of a bigger buyer/seller relationship - loosely integrated with other
conversations like Quote, Forecast, Shipment, and Billing/Payment.
The Benefits of Messaging
As before, you can benefit from improvements in throughput and latency - but now in a wider range of cases.
You can also introduce scalability, as you can load balance not just between threads on a single processor but also across servers in clustered and distributed systems.
Messaging also offers increased reliability and availability. Because the message server itself can be clustered, message consumers (such as J2EE MDBs) can be spread over multiple (redundant) host servers;
in the event of a failure of one part of the message-server cluster, message clients can seamlessly reconnect and retransmit any unacknowledged messages.
Finally, it is easy to reconfigure message flows by interfering at the message layer.
Messages can be filtered, enriched, transformed, and rerouted using simple standards-based tools such as XSLT;
or published on a topic to multiple subscribers. Any message broker can be used to achieve this kind of adaptability.
Try doing that with a remote procedure call!
Separation of Concerns
Perhaps the most important benefit of a document-centric approach is the clean separation of application components - each of which can be owned, developed, and deployed autonomously - either because different actors (as in our flight reservations example) are involved, or simply because the best-of-breed application components used to construct a single business application may use different technology platforms. When you have millions of dollars invested in software assets, the last thing you want is to trash them just because they
don't match your current preferred platform.
Different document-centric components can have different development life cycles, languages, and platforms.
You can mix-and-match your 30-year-old mainframe systems - using MQSeries to kick off CICS transactions - with your J2EE/UNIX, Windows/.NET, and any other legacy processing. Using message-oriented middleware, it is easy to plug these components together. Because it's easier, it's also cheaper. Interfaces tend to be simpler and cleaner, which discourages the spaghetti interconnections typical of multithreaded programming and drives toward a true loosly-coupled approach. You can assemble systems from best-of-breed pieces, which may be hosted in different departments or even companies Management of each component is autonomous; each can be separately scaled, replicated, or replaced. Easier integration also promotes more frequent reuse rather than redevelopment of components.
So, what kind of applications are we talking about in which asynchronous messaging patterns can be applied? System-to-system workflow, which is a catch-all for any kind of fully automated integration;
in-house enterprise application integration (EAI); and external integration between businesses - what we used to call B2B, then e-business integration, now called Web services.
The integration may involve multiple deployment platforms - J2EE, .NET, or any number of legacy platforms, hardware, and software, from
many different vendors. CIOs want to be able to plan their infrastructure to be completely uniform, but however hard they try, along comes
a merger, acquisition, new technology, or just an opinionated CEO to thwart their tidy ideas.
Even if you have managed to stick to J2EE, you're probably looking at more than one vendor's product stack.
Maybe you're developing on JBoss but deploying on BEA WebLogic, with a nice helping of IBM WebSphere and SonicMQ on the side, and
perhaps a sprinkling of Tibco Rendezvous as well. In financial services, message-oriented systems have been in place for the past 10-15
years. Over the past five years Java, J2EE, and JMS have brought increasing productivity benefits as the institutions reach toward
straight-through processing and next-day/same-day settlement.
In the telco market, in spite of the sector's deep recession, JMS is a key component of the OSS/J (Operational Support Systems for
Java) initiative. A message-based approach is used to simplify the integration of ordering, provisioning, billing, and network management
systems. With industry-wide support for messaging and formatting standards equipment manufacturers, service providers and network operators
can easily plug in new products and services.
In manufacturing, we're seeing RosettaNet and (more slowly) ebXML evolve to define and coordinate complex supply-chain processes; and
now we're beginning to see Web services technology, with SOAP as the transport, adopted to support all kinds of loosely coupled B2B
Process description and choreography standards like BPML (Business Process Markup Language), WSFL (Web Services Flow Language), XLANG (the
choreography language used by Microsoft's BizTalk), and WSCI (Web Services Choreography Interface) are being used to represent how
individual collaborating processes are tied together by message flows, both in XML and intuitive graphical notations.
It all boils down to this: a message-based, document-centric approach is appropriate for any integration of autonomous components in a
business process flow - providing the technology underpinnings you need to connect your functional systems to each other and then to the
rest of your organization and your employees, customers, partners, suppliers, and regulators. Most developers today are faced with the need
to connect heterogeneous applications and services across the inherently unreliable and unpredictable Internet; they can make it easy on
themselves by simply using asynchronous Java messaging.