Home » Java » Enterprise Java » JMS Development Fundamentals using Apache ActiveMQ

About Alvin Reyes

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

3 comments

  1. 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. 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. 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

Your email address will not be published. Required fields are marked *

*


+ 1 = four

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.