Monday, August 16, 2010

Spring, JMS, Weblogic, and FizzBuzz

There are 2 reasons for me posting this example:
I have not looked at anything Spring related for about a month, and last week I got to show someone new to Spring all the joy that it can bring, I couldn't shut up for about a hour. One of the reasons he is looking into using Spring happens to involve JMS, something I had not yet covered in my blog.
The other reason is:
I was challenged by a work colleague to come up with a more complex FizzBuzz implementation than his monster rule implementation and not being a person to back down on a challenge.

Here it is. Spring, JMS, Weblogic and FizzBuzz

All the code for this example will be available --Here--

There are a couple JMS examples out there using ActiveMQ, and either JMS "client type" implementations or Message Driven POJOs but not together. For this example I will show both methods and will use Weblogic, those that have worked with Weblogic know there are always little nuances. The first thing is to have a standalone client for Weblogic, you need the weblogic.jar (Full Client), I have uploaded this into my local maven repo so that I can just place this within my POM.


In the Weblogic admin console I set up the JMS modules as follows (5 Queues and a Connection Factory):


Now for what this example does:
* It creates 100 messages...
* It reads 100 messages... while reading, it checks the content, depending on the value it creates a message to a fizz queue or a buzz queue or a fizzbuzz queue and a result queue.
* There is message driven POJO on the result queue that prints out the content.

How it does this:
The Spring application context below is pretty straight forward but it does have a couple things to note:
1. The jndiTemplate, jmsQueueConnectionFactory, jmsDestinationResolver, jmsTemplate are all used in combination to get to the application server, and connect to the actual JMS queues.
2. The "messageDrivenListener" and "queue" beans are the configuration for the message driven POJO. In that config is "concurrentConsumers", this allows you to specify how many listeners will be created to process the messages on the queue. For this example I only require 1.



I have created just a simple example message structure for this, with a generic content which looks like:






The message driven POJO is just the standard javax.jms.MessageListener interface implementation:



The client classes to read and create messages, make use of Springs JMSTemplate to simplify your interaction with the JMS server:



And the finally the Main class, that creates the 100 messages, then reads them, publishing messages to 4 different queues. The reading of the results queue is triggered by the application server and handled by Springs "DefaultMessageListenerContainer"



Note: if you constantly run this example, you will end up with a ton of messages on the Fizz, Buzz and FizzBuzz queues as there is nothing reading those messages to clean it up.

3 comments:

  1. This is absolutely awesome - and I concede. You won this challenge...

    ReplyDelete
  2. Hi,

    I am have written client program for spring-jms module, it accept as Object Message and have configured MessageConverter it convert that Message type to my application specific Object Type say Customer. after that it ll process and send response to Response Queue. The problem is i want to set some message Identifier like CorrelationId in that i am setting that value to request queue's message, when it goes to my application its converting in to Object of my app not jms Message, how can i retireve that correlationId from request message. if i get that one i can set the same correlation id to response message and client want to check the response then it is easy to retrieve based on that correlation id. any help appreciated.

    Regards,
    Gangadhar

    ReplyDelete

Popular Posts

Followers