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.
Develop the Consumer – Consumer will be the one receiving the message created by the producer.
Reference: JMS Development Fundamentals using Apache ActiveMQ from our JCG partner Alvin Reyes at the Alvin “Jay” Reyes Blog.
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.