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.
I have recently been slacking on content on my blog, between long stressful hours at work and to the wonderful toy that is an iPhone, I have...
I make no claim to be a "computer scientist" or a software "engineer", those titles alone can spark some debate, I regar...
I saw an article (well more of a rant) the other day, by Rob Williams Brain Drain in enterprise Dev . I have to say, I do agree with some o...
This series of posts will be about me getting to grips with JBoss Drools . The reasoning behind it is: SAP bought out my company's curre...
I recently finished 97 Things every programmer should know . Well to be completely honest I did skim over a couple of the 97, but all and al...