Java JMS tutorial
This Java JMS tutorial gives you an introduction to the Java Message Service. It was originally published on YouTube on May 18, 2014.
Java JMS tutorial
This Java JMS tutorial gives you an introduction to the Java Message Service.
Transcript
Hi, I’m DJ Spiess and you’re watching What is Java Messaging on Deege U.
In this episode, we are going to look at what is Java messaging and how it works. Then we’ll look at sending and receiving messages. By the end, you should know all the basics for java message communication.
This video uses Java 7, JMS 2.0 and the Wildfly 8 application server. The code should run on other Java 7 EE application servers, but your milage may vary.
What is Java messaging and how does it work?
Messaging is as simple as it sounds. You are sending a message from one computer to another.<> Usually we use messaging systems to allow two very different systems to communicate.<> You can also use messaging to allow two or more systems to communicate asynchronously, in a store and forward architecture.
The Java message service, or JMS, is an interface to send messages. The interface describes the objects sent and the objects involved in the conversation, but leaves the implementation of the service to third party vendors.
The message can be anything you like, but usually it’s text or bytes organized in some agreed format.
The two computers involved in the messaging are usually called the producer and the consumer. The producer creates messages. The consumer receives messages. The communication is asynchronous, which means the consumer is free to receive the message when ever it wants at some later date and time.
In between the producer and the consumer is an object that facilitates the message communication called the destination.
There are two types of destinations, a queue and a topic.
If your message can be received by only one consumer, you use a queue. A queue guarantees delivery to one and only one consumer. If more than one consumer is connected to the queue, only one consumer gets the message. It’s first come first served.
If your message can be received by any and all consumers listening, you use a topic. A topic is a special type of queue that guarantees delivery to all consumers subscribed to the topic. If no one is listening, a message placed on the topic is dropped and gone forever. You only get the message if you are listening.
What if you weren’t paying attention, and you really needed to get that message? You can create a special kind of consumer called a durable subscriber. This guarantees that all messages get to any durable subscriber when it reconnects to the topic.
Out of the box, messaging is not enabled in Wildfly. The easiest way to turn it on, is to use the standalone-all.xml file. This means go into your wildly directory. Under the standalone/configuration subdirectory, rename standalone.xml to standalone.old. Then duplicate standalone-full.xml and rename it to standalone.xml.
We also need some queues to play with. To create a queue or topic, we need to add the following xml code to the standalone.xml file under the messaging system section.
<!-- DeegeU - Right Side -->
<ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-5305511207032009" data-ad-slot="5596823779"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
Tools Used
- Java 7
- WildFly 8
Media Credits
All media created and owned by DJ Spiess unless listed below.
- No infringement intended
Get the code
The source code for “Java JMS Tutorial” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:
git clone https://github.com/deege/deegeU-jms-001.git
Go to the Support > Getting the Code page for more help.
If you find any errors in the code, feel free to let me know or issue a pull request in Git.
Comments
DJ Spiess
Your personal instructor
My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.