Add the power of asynchronous processing to your JSPs
Create custom JSP tags to use with JMS
By Tarak Modi, JavaWorld.com, 02/09/01
Most JavaServer Pages (JSP) developers that I've worked with love JSP because it's easy to use and a powerful way to design
flexible Webpages. As passionate Web designers, most JSP developers are not interested in the details of n-tier architectures
and distributed programming, let alone the intricacies and complexities of asynchronous processing with Java Messaging Service
(JMS). Nevertheless, circumstances will present themselves when JSP developers may need the power of asynchronous processing.
For example, suppose an online Webstore written completely with JSP needs to update its inventory after each processed sale,
yet the inventory management system is across the globe. In that case, using JMS to update the inventory asynchronously may
provide an attractive alternative. Situations similar to that one most likely fueled Sun to make both JSP and JMS key pieces
of the Java 2 Platform, Enterprise Edition (J2EE).
So, is the answer as simple as making JSP developers master JMS? Unfortunately not. As I mentioned above, this answer will
probably displease many JSP developers. Then, what is the solution?
JSP 1.1 introduced an extremely valuable new capability: the ability to define your own JSP tags, also called custom tags.
You can group custom tags into tag libraries and reuse them in any number of JSP files. Custom tags allow complex programming
logic to boil down to a set of simple tags, which JSP developers can easily use to develop content. Although custom tags require
a bit more effort to set up than normal tags, the benefits definitely outweigh the costs. Tag libraries increase productivity
by encouraging labor division between library developers and library users. Developers expert in accessing data and other
services create JSP tags; Webpage authors who focus on the design of user interfaces use them.
In this article, I will design and implement two custom JSP tags that will allow JSP developers to work with JMS in the form
that they are most comfortable with -- tags. Plus, since all the JMS programming logic is buried deep within the custom tags,
JSP developers do not need to learn JMS. That's what I call killing two birds with one stone.
For example, using the write custom tag that you will create in this article, a JSP developer can send/publish a message as shown below:
<jms:write destination="jms://Queue/ModiQueue" message="Hello World"/>
and can receive a message as follows:
<jms:read destination="jms://Queue/ModiQueue"/>
Background on JMS
To understand JMS, you must understand message-oriented middleware (MOM), which has grown in popularity over the last few
years. Let's take a look at two key characteristics of MOM that differentiate it from the more traditional middleware based
on remote procedure call (RPC).
- MOM allows asynchronous communication between applications: When you send a letter to your friend via snail mail, you obviously do not wait for his response before doing anything else.
You go on with your life. At some point, you may receive a response to your letter but, in the meantime, your actions do not
depend upon it. That example demonstrates the basics of asynchronous communication and, hence, the basics of how MOM works.
- MOM allows the communicating applications to have completely separate lifetimes: Consider the following scenario: A salesperson fills in customer orders on his laptop while flying to the corporate office.
As the laptop is not connected to the central computer, it cannot process the orders at that time. However, both the laptop
and the central computer are equipped with MOM software. Though the laptop cannot communicate with the central computer at
the time the salesperson enters the orders, the MOM on the laptop keeps track of those order messages. Once the salesperson
is back in his office and hooked up to the corporate intranet, the laptop's MOM fires off those stored messages to the MOM
software on the central computer, which receives and processes those messages. The MOM ensures that the messages are not lost,
delivered out of sequence, or duplicated. That second aspect of MOM makes it even more critical to successful distributed
applications.
Those characteristics make MOM a popular alternative to RPC, which fails to offer either of those capabilities.