[image]
[image]

[image]
[image]
Java [image]
[image] [image] [image]
[image]
add to:
[image]Del.icio.us
[image]Digg
[image]Google
[image]Furl
[image]Slashdot
[image]Y! MyWeb
[image]Blink
TABLE OF CONTENTS
July 02, 2007

Java Message Service

(Page 4 of 4)

Sample E-mail Reader Application

The reader application performs many of the same steps as the sender in terms of initializing its JMS connection and session. The differences are:

The constructor creates a javax.jms.MessageConsumer object for the "Inbox" queue because its purpose is to receive, or consume, messages. The reader application implements the javax.jms.MessageListener interface and provides the resulting object to the JMS provider via a call to the MessageConsumer object's setMessageListener method.

Once initialized, the application is free to continue with its own processing. In the case of an actual e-mail client application, this may involve letting users compose new e-mail messages, organize existing e-mail into folders, and so on. The provider eventually calls back on the application's MessageListener object when a new message arrives. This is the way JMS implements asynchronous messaging, which lets the client application perform other tasks while waiting for messages to arrive.

JMS also supports synchronous messaging, which requires the client application (or one of its child threads) to block until a message arrives. This type of an application is strictly an event-driven application, meaning it cannot do anything unless certain events occur; a JMS message arrival, in this case. This type of application can be developed to respond differently to different messages received, and can in effect be driven remotely by sending it the proper messages in the proper order.

Again, the setMessageListener method was called with a reference to an object that implements the MessageListener interface. Because of this, once the JMS connection is started, the provider asynchronously calls the client application's onMessage method when a message arrives for the destination the consumer is listening to; see the EmailReader class's implementation of onMessage (available electronically). The javax.jms.Message object is provided as a parameter to the onMessage method, representing the received JMS message. Once the message is cast to a javax.jms.ObjectMessage object, the Email POJO is then extracted via a call to getObject. For illustrative purposes, the remainder of the code simply prints out the e-mail's subject and body (see Figure 5).

When this application is executed, it receives all of the messages, one by one, that were placed on the queue earlier by the e-mail sender application. Once all of the messages have been received, the application simply waits to be notified that another message has been placed on the queue.

Conclusion

Writing JMS client applications is straightforward once you understand the basics. The power of JMS is in the ability to leverage built-in transaction management, and reliable message delivery, without knowing much more than the basics.

Previous Page | 1 What Is Messaging? | 2 JMS Message Paradigms | 3 Sample E-mail Sender Application | 4 Sample E-mail Reader Application
FREE WEBINAR
Fully automating the release management process is a critical element of ALM strategy. Attend this Electric Cloud webinar for release process best practices. Register today!
[image]
DR. DOBB'S CAREER CENTER [image]
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
Most Recent Posts:


[image]

MICROSITES
FEATURED TOPIC [image]

ADDITIONAL TOPICS [image]

[image]


[image]

 
[image]

[image]

[image]
[image]

Related Sites: DotNetJunkies, SD Expo, SqlJunkies
[image]


You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser