|
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:
- Get the Connection Factory instance from the JMS provider.Do this by looking in JNDI, for example.
- Create a JMS Connection. This is an active connection to JMS provider. You use Connection Factory to do this.
- 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.
- 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).
- 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 beana 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 simpleit 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):
- It calls
consumer.start(), which in turn establishes the JMS Session, and is "waiting" for incoming messages.
- It calls
producer.start(), which sends a message and returns a message.
- The controller performs as usual, returns the control further, and the Web client is notified that mailing has started.
- 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.
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.
|