Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

JMS, XML, and the heterogeneous enterprise

Using JMS and XML to improve enterprise application interoperability

Todd Sundsted (tsundsted@comframe.com)ComFrame Software Corporation
Todd Sundsted has been writing programs since computers became available in convenient desktop models. Though originally interested in building distributed applications in C++, Todd moved on to the Java programming language when it became the obvious choice for that sort of thing. In addition to writing, Todd works as a Java Architect with ComFrame Software Corporation. Todd can be reached at tsundsted@comframe.com.

Summary:  Most computing environments today are characterized more by a patchwork of different platforms than by an adherence to any one platform. The Java Message Service (JMS), together with the Extensible Markup Language (XML), extends the promise of integration to this heterogeneous environment. This article demonstrates how to use JMS to create and distribute XML-based messages to Java and non-Java applications alike.

Date:  02 May 2000
Level:  Introductory
Also available in:   Japanese

Activity:  8402 views
Comments:  

After years of building, extending, and maintaining large, distributed applications, programmers are learning to appreciate the benefits of platform-neutral behavior and platform-neutral data.

The Java programming language has stepped forward to fill the need for platform-neutral behavior (although scripting languages such as TCL still enjoy a moment or two in the spotlight). XML (Extensible Markup Language) is proving to be the backbone of open, platform-neutral data solutions.

This article demonstrates how XML fulfills the promise of platform-neutral data when used with the Java Message Service (JMS).

Messaging pulls it all together

JMS defines a generic Java language interface to a message service. It supports most common messaging models (both publish/subscribe and point-to-point).

At first it might seem strange to mention platform-neutral data in the same paragraph as Java technology. After all, why do we need platform-neutral data when we're using JMS, which is a Java-based (and therefore platform-neutral) technology?

The answer springs from the environment in which messaging is often used. One of messaging's great strengths is in the realm of application integration. More often than not, the applications being integrated aren't all Java applications.

JMS is ideal in this situation because it is an interface specification -- not an implementation. It is meant to be layered on top of an existing technology which might already be in place. (Of course, all-Java implementations can also take advantage of this JMS and XML solution.)


Figure 1. JMS layered on top of a proprietary message service
JMS layered on top of a proprietary message service

Figure 1 illustrates such an environment. Non-Java applications communicate with the proprietary message service directly. Java applications communicate via JMS. Everything is seamless, right?

Well, not quite. There's still the question of data.


Platform-neutral data saves the day

Consider JMS's five message types. JMS supplies three structured or semi-structured message types (MapMessage, ObjectMessage, and StreamMessage) and two unstructured or free-form message types (TextMessage and BytesMessage).

The structured message formats represent only a handful of the many ways to structure data (only a map, a serialized object, and a stream of data elements are directly represented). More importantly, they raise the question of interoperation with non-Java applications. How is the translation or mapping accomplished, especially when the translation involves serialized classes?

The unstructured message formats are more likely to interoperate well, but only because they impose very little structure on the messages. This small fact, however, places the burden of parsing and validation upon each recipient.

XML eases this burden. It provides a clean, standardized path to rich, functional data structures, and backs it up with a growing number of tools for doing the dirty work of parsing and validation.

With XML in the loop, everything is seamless.


The code

The following two pieces of code demonstrate how to transfer an XML message. Since XML is basically text, it is transferred in the body of a TextMessage instance. Both examples assume the existence of an XMLValidator class. This class is a thin wrapper around one of the many Java-based XML parsing and validation tools available.

The sending side is pretty straightforward. The code in Listing 1 demonstrates how to locate a Queue and QueueConnectionFactory, create a connection and a session, stuff the XML into a TextMessage, and send the message to the queue.


Listing 1. Example of sending an XML message

public
  static
  void
  send(
    String stringQueue,
    String stringQueueConnectionFactory,
    XMLValidator xmlvalidator,
    String stringXML)
  throws
    NamingException,
    JMSException,
    XMLValidationException {

    // Look up the Queue and the QueueConnection Factory
    // in a JNDI naming and directory service, or create
    // them directly.

    Context context = new InitialContext();

    Queue queue = null;
    queue = (Queue)context.lookup(stringQueue);

    QueueConnectionFactory queueconnectionfactory = null;
    queueconnectionfactory = 
       (QueueConnectionFactory)context.lookup(stringQueueConnectionFactory);

    QueueConnection queueconnection = null;
    queueconnection = queueconnectionfactory.createQueueConnection();

    QueueSession queuesession = null;
    queuesession = 
       queueconnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

    // Create the QueueSender.

    QueueSender queuesender = null;
    queuesender = queuesession.createSender(queue);

    // Create a TextMessage from the XML.  If the XML is
    // created from user input, it will be necessary to
    // validate the XML.  Send it.

    xmlvalidator.validate(stringXML);
    TextMessage textmessage;

    textmessage = queuesession.createTextMessage();
    textmessage.setText(stringXML);
    queuesender.send(textmessage);
  }

On the receiving end there is more work to do (see Listing 2). After receiving the TextMessage, the code extracts the XML and validates it. The process of validation checks the XML against a Document Type Definition (DTD), which may be part of the XML or external to it.

If the DTD is stored externally, you face the interesting possibility of creating and maintaining an enterprise-wide DTD repository. The repository rigorously defines the structure of the messages that participating enterprise applications can exchange.

The TextMessage is now the conduit of very structured, well-defined messages, understood by Java and non-Java applications alike.


Listing 2. Example of receiving an XML message

public
static
String
receive(
String stringQueue,
String stringQueueConnectionFactory,
XMLValidator xmlvalidator)
throws
NamingException,
JMSException,
XMLValidationException {
// Look up the Queue and the QueueConnection Factory
// in a JNDI naming and directory service, or create
// them directly.
Context context = new InitialContext();
Queue queue = null;
queue = (Queue)context.lookup(stringQueue);
QueueConnectionFactory queueconnectionfactory = null;
queueconnectionfactory =
(QueueConnectionFactory)context.lookup(stringQueueConnectionFactory);
QueueConnection queueconnection = null;
queueconnection = queueconnectionfactory.createQueueConnection();
QueueSession queuesession = null;
queuesession =
queueconnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the QueueReceiver.
QueueReceiver queuereceiver = null;
queuereceiver = queuesession.createReceiver(queue);
// Retrieve a TextMessage. Get the XML. Validate
// the XML before returning it. It is also possible to
// transform the XML into a DOM tree or other representation
// during the validation and to return that.
TextMessage textmessage;
textmessage = (TextMessage)queuereceiver.receive();
String stringXML = textmessage.getText();
xmlvalidator.validate(stringXML);
return stringXML;
}


Conclusion

There seems to be a general feeling that Message-oriented Middleware (of which JMS is a part) and XML belong together. I hope I've added some strength to the claim. It's really not that surprising. XML documents are natural messages -- they're structured and yet very flexible. In addition, MOM excels at integrating heterogeneous applications, as does XML.


Resources

About the author

Todd Sundsted has been writing programs since computers became available in convenient desktop models. Though originally interested in building distributed applications in C++, Todd moved on to the Java programming language when it became the obvious choice for that sort of thing. In addition to writing, Todd works as a Java Architect with ComFrame Software Corporation. Todd can be reached at tsundsted@comframe.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Java technology
ArticleID=10452
ArticleTitle=JMS, XML, and the heterogeneous enterprise
publish-date=05022000
author1-email=tsundsted@comframe.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers