advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Run a Background Process in a Web Container Using Spring and ActiveMQ :

How It Will Work

The most interesting part about this app is how JMS integrates with Spring. Though you are probably already familiar with this workflow, it's always to have a reminder. Here are the usual steps you take while working with JMS:
  1. Get the Connection Factory instance from the JMS provider.Do this by looking in JNDI, for example.
  2. Create a JMS Connection. This is an active connection to JMS provider. You use Connection Factory to do this.
  3. Create a JMS Session. This is a helper object used to send/receive messages. You use the JMS Connection to get it. During this stage creation, you are specifying if you want transactions or not.
  4. Get a JMS Destination channel to send or receive messages. For example, you can get it from JNDI. The destination can be a queue or a topic depending on what style of messaging you are using: either point-to-point (PTP) or publish-and-subscribe (PUB/SUB).
  5. Depending on whether you're sending or recieving messages, you need to create a Message Producer or a Message Consumer.
Using ActiveMQ with Spring really provides an incredibly flexible integration mechnaism. It allows you to use the JMS Message Broker inside the same JVM where your Web application is running.

To accomplish Step #1 from the above list, first define the jmsFactory bean, and use the JMS Broker URL as the "insider" for your JVM. You don't need JNDI ir separate running servers. You can check and follow all these steps by opening the \mailsender_src\web\WEB-INF\activemq-spring.xml file.

Next, you will see the example defines myJmsTemplate, which is a Spring's JmsTemplate bean—a handy abstraction for working with JMS, it is pretty useful for messaging in a simple way. As you can see, the example gives Connection Factory to it.

The next line in the configuration file is the destination bean; this examples uses the queue style of messaging.

Last, but not least, are the definitions for producer and consumer. The producer bean is simple—it only does one thing: sends TextMessages to the consumer. For this purpose, the example "injects" myJmsTemplate and destination into the producer bean. This means that consumer now has more work to do. To be a real object that receives asynchronous messages, it has to implement Message Listener with only one method: void onMessage(Message message). The example also "injects" userManager, which will operates the users receiving the emails.

The only two classes you need to create are classes of the producer and consumer beans. Everything else is done for you by Spring and ActiveMQ.

\mailsender_src\src\java\com\objecty\mq\MailsenderProducer.java is implemented like an ActiveMQ's Service, and void start() sends messages and nothing else. In your own application, the messages will most likely contain information about who sent it and why, etc. Your Message Consumer can filter messages depending on what gets sent to it, and you can have multiple Message Consumers, with each of them only receiving their own messages.

Step #3" takes place \mailsender_src\src\java\com\objecty\mq\MailsenderConsumer.java. This class plays two roles: it is a Message Listener as well as ActiveMQ's Service. In the beginning, it establishes JMS Session. And it's void onMessage(Message message) catches all incoming messages. Again, in real life applications, your Message Consumer must to do something else about received messages, in order to filter and fetch required properties. But this consumer is catching all messages, and thinks that any incoming message is a request to start mailing.

To enable the Web client to run mailing in the background, you've got mailController defined in the \mailsender_src\web\WEB-INF\springmvc-servlet.xml configuration file. The example "injects" this into both producer and consumer. Once this controller is executed, it performs the following functions (check it here \mailsender_src\src\java\com\objecty\mvc\MailController.java):

  1. It calls consumer.start(), which in turn establishes the JMS Session, and is "waiting" for incoming messages.
  2. It calls producer.start(), which sends a message and returns a message.
  3. The controller performs as usual, returns the control further, and the Web client is notified that mailing has started.
  4. This time, Message Consumer's void onMessage(Message message) recieves a message from Producer, and starts mailing. When it's finished, it calls the internal void stop() to close all connections. This isn't necessary after each message processing, but in the example code, the Message Consumer is not expecting anything else, so it's free to "clean up" after itself.
It's worth mentionning that without constants, you cannot start mailing. They are placed in the \mailsender_src\src\java\com\objecty\Constants.java file. Before you try to build and run the code, you have to edit this file and set your own settings for SMTP server authentification.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs