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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

2 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

Leave a Reply


8 − six =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.