Concepts and Fundamentals of JMS Programming
- What Is a Messaging System or Service?
- What Is the JMS API?
- Concepts of JMS Programming
- Summary
- Questions and Answers
In this chapter, you will learn the fundamentals and basic elements of Java Message Service (JMS) programming. In the next chapter, you will write three simple programs to help you understand how to develop a JMS application.
What Is a Messaging System or Service?
Messaging is a way or a mechanism that provides communication between software applications, programs, or objects on a distributed system. Remote Method Invocation (RMI) and socket programming are also types of messaging according to this definition. But, the focus of this book is on a message-based messaging system. As a simple definition, a message identifies the content transmitted between two or more applications or programs. One or more programs send a message, and the other one or more programs receive the message. You might think that a query from a SQL-based database using a graphical user interface (GUI) is a message. It is direct, one-to-one messaging, but a messaging system is more sophisticated than this simple example. It is more like using TCP/IP packets on a computer network. In a messaging system, there are clients that can send and receive messages. Each client connects to the messaging system, which provides a platform to create, send, and receive messages. A messaging system has three major features:
A messaging system is loosely coupled. This is the most important feature of a messaging system and might be an advantage compared to other systems such as RMI. An application or program, called a sender or publisher, sends a message to a destination, not directly to another client. Another application or program, called a receiver or subscriber, receives the message from a destination. Senders and receivers do not have to be aware of each other.
A messaging system isolates clients from each other. Neither sender nor receiver needs to know about each other. They only need to know the message format and destination.
A messaging system allows decoupling. A sender and receiver use the system at different times. They do not have to be up and running at the same time. A sender sends the message to a destination, and the receiver takes the messages whenever it is ready. A sender does not need to wait for a response. It can process another task without being blocked. I refer to this feature as asynchronous messaging in the remainder of the book, which generally means that clients are able to use the system at different times, and they do not have to know whether other clients in the system are up and running.
Some developers consider email a part of a messaging system. Although email is a way of communication between people, and sometimes between people and software applications, a messaging system is different. It is used for communication between software applications or objects.
A messaging system is based on message-oriented middleware (MOM), which was explained in the previous chapter. MOM defines the rule of messaging as:
How the message looks
How a sender application sends the message
How a receiver application receives the message
How a receiver application reads the message
Advantages and Disadvantages of a Messaging System
In the messaging service (or system), there is a server, and clients connect to this server to communicate with each other (see Figure 5.1).
Figure 5.1 The messaging service architecture.
The server provides some essential services such as message persistence, load balancing, and security. The server provides asynchronous communication and guaranteed delivery from senders to receivers.
A messaging service is a great way for applications to communicate with each other, but it has some disadvantages:
You have to send header and other information with the message content. Therefore, the total amount of information is larger than the message content itself, which might increase network traffic.
Every message goes to the receivers through a server, which makes communication slower than a direct connection.
Your messaging service provider (vendor) might not support all the JMS specifications defined by Sun Microsystems.
Before designing your JMS projects, you should compare the disadvantages such as network traffic, slower communication, and vendor-specific issues with the advantages such as loosely coupled or decoupled systems, portability, persistent messaging, and guaranteed delivery.
Briefly, if you only need to insert some new records into a local database and you have a very reliable network, you might not need to use a messaging system. Keep in mind that a messaging-based application consumes additional resources.
However, if you want to isolate networking problems in your client source code or if the database you want to access is in a different location, you might need to use a messaging service. A messaging service can ensure completion of transactions of an application properly such as manipulating data in a table of the database (for example, Insert, Delete, and Update statements), particularly if the database is unavailable (such as when using a laptop that is disconnected from the network). It will process the command (the message content) at a later time. Decoupling or loosely coupling is the best feature of a messaging service.