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.

 DevX Skillbuilding from IBM developerWorks
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 48
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%.

Apple Details iPhone/Mac Developer Event
RIM Ups Ante With Mobile Software Push
Novell Readies Silverlight Clone for Linux
Yahoo Pitches The 'Next Generation of Search'
Alfresco's Latest ECM: Prying Open a Sector?
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads for the JON 2.0
Out with the Old, in with the New at JavaOne

Create Secure Java Applications Productively, Part 1: Use Rational Application Developer and Data Studio
.NET Building Blocks: Custom User Control Fundamentals
Secure Internet File-Sharing with PHP, MySQL, and JavaScript
Getting Started with TBB on Windows
Moving to VoIP: Should You Go It Alone?
Introduction to the WPF Command Framework
7.0, Microsoft's Lucky Version?
Will Hyper-V Make VMware This Decade's Netscape?
Eliminate Fragmentation Frustration with Netbiscuits
Taming Trees: Building Branching Structures

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



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES