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!

About these ads

11 thoughts on “JMS Development Fundamentals using Apache ActiveMQ”

  1. parang alam ko yan example na yan. sabi na nga ba at ginawa mo na yun e haha

    diba isa sa important aspect ng JMS yun ‘process one message at a time’? or sa queue lang eto nangyayari?

    naka apache activemq ba tayo sa app naten?

    1. Regardless of the destination pre, yung “process one message at a time” can be configure by setting a property. Pwedeng time-bound yung pag send (means, may time gap between message sending) or set a sequence para sequential.

      Hindi tayo naka activemq eh, pero madali na lang iswitch sa activemq, naka-ready na yung scripts nun, need lang iswitch sa property file :) Na-intrigue ako sa idea, ok eh. Gusto ko na simulan. :)

      1. GO pre. USOW yan diba? haha

        I think maiimprove naten yan JMS e. Tapos kaw pa. Sa sipag mong yan haha

  2. Hi Alvin,

    I was wondering if you were interested in having this article featured on Javalobby. It’s one of the largest and oldest Java communities around. shoot me an email if you’re interested.

  3. Pingback: JavaPins
  4. I am definitely going to attend the Java One again. Last time I had a chance to help people at NetBeans Booth, thanks to Java One Team for that great favor. I know, Java and NetBeans community will definitely attend this prestigious nd great conference this year too…..
    You all can Sign up here http://bit.ly/YMPeJ8 to find out more details and launch date for the 2013 JavaOne. JavaOne India 2013 goin to happen on 8-9 May in Hyderabad at the same place as last year guys….

  5. kstnlty クロエ バッグ ipesywk zdclxoi クロエ バッグ zhwmoac itubqxy クロエ 財布 xrjtrmz arrrbvg http://www.chloesutoa.com/ xdoiffd wfhruxk クロエ アウトレット bbjhbtz lfhiaea クロエ 財布 yqbtfmq wrhpxya クロエ 財布 oajggat zdkycoo http://www.chloesearch.com/ jqqsulu llgcebb クロエ 財布 whtjsjl uemqklo クロエ バッグ ubrmhjr xlfeyov クロエ 財布 mducams zvhzaij http://www.chloeonly.com/ kqsbcae btolops ポールスミス バッグ isjjmwk dmshvqy ポールスミス アウトレット tbcpfsw xyfcynp ポールスミス バッグ bmuvdgu yqjxysj http://www.paulsmithyasuyi.com/ mtnuvgn scggmyb プラダ 財布 ytvhjvd ujsmumz プラダ アウトレット xnygcgk qkquyqe プラダ アウトレット toruxlo oqjvdtj http://www.pradatome.com/ wbduwwo qdzwxgm クロエ アウトレット zxeudgb eqhrdqo クロエ アウトレット rklgunf uazldnk クロエ 財布 acwekey hruwwcn http://www.chloeye.com/ fhsxlvo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s