JMS server and client example

Opublikowany

What is JMS? Java Message Service could be considered as high level framework that communicates two or more clients each other by sending messages. JMS is fully supported by Java EE and has been specified in JSR -914 (see links section).
Base operations that we could done via JMS are:
- send messages
- receive messages
Typically one JMS client create message and send it to the server (JMS message broker) and other JMS client receive it and perform some operation. Of course this is high level overview and lower process is more complicated while added i.e. security and so on.

Where we use it? Well we could use JMS everywhere where we need increase scalability, integrate different environments, batch processing support, asynchronous communication without time pressure.
Common use is email sending process where one client create message that templates email should be send to appropriate receiver/s and send message to message broker where it is queued and receivers can download them when connected.

What are the key players on the JMS market? Below You could find my private favorite JMS vendors list:
- Amazon SQS
- ActiveMQ
- Oracle Weblogic
- HornetQ (perv. JBoss messaging) see link no 2

What is activemq? ActiveMQ is implementation of Message Oriented Middleware which is used to communicate ie via JMS clients. As we could read at official page (see link no 3) it is open source system that is fast, supports cross language clients and protocols and what’s the most important supporting fully JMS 1.1 specification.
Latest version is 5.x.x and this version will be used in demo.

Active mq server From the download section of official ActiveMQ page (see link no 3) You can download latest server binary package. After un-zip it took almost 100MB of disk space.
Unzipped binaries is all You need to start Your server.
At my windows machine starting script is located at: apache-activemq-5.x.x\bin. I use activemq.bat but there ale similar scripts for Linux too.
Bat script setup local env and finally run jar file called activemq.jar which is located at bin folder.

After run in command line starting script and successfully started server You should see something similar to:

Now You could type http://localhost:8161/admin/ to access Your ActiveMQ server administration console, note that default login and password is admin.

It should look like that:

Now You could add Your first queue in Queues menu.

ActiveMQ client example I created sample client for ActiveMQ server in Java 8 using threads and lambdas. Whole code You could find at my github: https://github.com/tjancz/jms-activemq-example.git

Below You could see sender code:

and the receiver code:

If You would like to see all working stuff just form my github repo.
There is also great documentation on ActiveMQ page.

Summary JMS is not scary as it looks and ActiveMQ is great MOM that could easily help You set up message queues and integrate different systems in MQ way.

Links
1. https://jcp.org/aboutJava/communityprocess/final/jsr914/index.html
2. http://hornetq.jboss.org/
3. http://activemq.apache.org/

Autor