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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
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%.

Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio

Use Explicit Conversion Functions to Avert Reckless Implicit Conversions
Polyglot Programming: Building Solutions by Composing Languages
Automated testing for .NET by Ben Hall
"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant

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: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES