JMS Development Fundamentals using Apache ActiveMQ

It was last year when I had my hands on trying out JMS. The Idea and concept behind baffled me a little but as soon as I knew what It is for, I quickly grasp it. In this post, I will show the fundamentals of developing a simple producer/consumer in Java using Apache ActiveMQ as its backend.

Let’s first start with the concept, here is a simple JMS architecture:

A connection factory must first be created in order to connect to the destination. A Client (The Application) will have to create a connection instances and create a producer class so that it can create the message that will be sent to the destination. A message consumer class (external app) will have to subscribe to the destination, for it to consume the message, produced by the producer.

Think of this scenario (skip this part if you need the example urgently): You want to develop a question and answer application, you want it to be very efficient for the user. You don’t want users going and logging in a website to check for questions and answers, you want to them to be informed as quickly as possible with any questions raised, or new answers found. Then you thought of putting up a desktop application and put a notification bar, this way, users will be notified and they can reply or check the newly data stored.

Clearly, you don’t want the user clicking on refresh just to check new data, you wouldn’t allow that right? Thats just inefficient for anyone who uses it. How about polling? A goog approach, but that will possibly allocate more resource than it should’ve. What you want is to create an automatic pull request to clients every time a new data is pushed into the database. A server that does the client-push.

Think of it like this: Once a message is thrown at the database, the same message will be thrown on the Message Destination (Topic or Queue), and it will be send to all clients subscribe to the destination. :) Just like, being subscribe to a newsletter, you as a user subscribed and every time the newsletter is sent, all users is subject to receive the newsletter email.

This is just one of the many possibilities that JMS can solve in your development scenarios. Yet clearly, with this feature, you can just imagine how helpful it can be.
So how about I show a little example of how can this be done?, Just follow the instructions below and check it out yourself:

First, download and setup ActiveMQ (http://activemq.apache.org/)- “Apache ActiveMQ ™ is the most popular and powerful open source messaging and Integration Patterns server.” – According to its website. I actually won’t argue, aside from being powerful and open source, its easily the most light weight messaging server I’ve worked with so far (I’ve work with WeblogicMQ and tried JBossMQ).

After downloading the source code, put it in your favorite directory and run the activemq command. This is located at <activemq root>/bin.

After running the ActiveMQ, you should now be able to see the MQ Admin Console website. This is where all metrics of the MQ can be seen, including Consumers, topics, queues, messages, etc. By default, it uses the port 8161 (http://localhost:8161/) for this.

If you can see the site above, this means you’ve successfully started the Apache ActiveMQ! Lets now begin developing our clients. For this post, I’ll be using Eclipse to develop the code, but you may use any IDE you’re comfortable with.

Open up Eclipse and Create a new Java Project.
Get and import the activemq-all-<version>.jar file to your project. Note that this is an important step, since we will be using the classes in the jar to access MQ Management services.
Develop the Producer: – Producer will be the one sending the actual message.

Producer
Develop the Consumer – Consumer will be the one receiving the message created by the producer.

Consumer
Download the Complete Project here.

Enjoy!

Reference: JMS Development Fundamentals using Apache ActiveMQ from our JCG partner Alvin Reyes at the Alvin “Jay” Reyes Blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

3 Responses to "JMS Development Fundamentals using Apache ActiveMQ"

  1. John says:

    Sir, i am having trouble opening the .7z file . can u please suggest a detailed way to do so?
    how to develop producer, consumer.
    i am just starting out in IT sector.

  2. Kumar Shorav says:

    Hi Alvin,

    I have a Restful service API developed with JAX-RS and jersey. I have deployed the same in TOMCAT 7. Now I would like to implement Activemq so that I would keep all request in a queue and process the request resource. How to do this and integrate with tomcat7. How to integrate ActiveMq with Tomcat7 or my rest service webapp. How to call the service.

    Important :- Inside the Rest Api, I am using FilterChaining concept for security concern and after verification of the calling party, I am simply forwarding the request to the resource. For this I have added in web.xml.

    Thanks
    Kumar Shorav

  3. Vivek says:

    Hi , I am facing an issue with this . I have done all the setup and all . But when i run this it gives an error ” org.apache.activemq.ConnectionFailedException: The JMS connection has failed: Unknown data type: 47″.

Leave a Reply


1 + = five



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close