In-depth introduction to JMS (1)-Brief Introduction to JMS

Source: Internet
Author: User

What if the phone can only perform real-time calls without messages or text messages? When a phone call arrives, the phone call cannot receive any information. Why can't I save the information first? When the user needs to view the information, I can get the information again? Along with this doubt, short messages and messages came into being. no matter whether the phone is turned on or not received in time, we can get the information in it. JMS provides similar functions. In this chapter, we will systematically learn important content in JMS.

Understanding basic concepts and applicability of JMS

Ø differences between the point-to-point model and the publish/subscribe model and their application scenarios

Familiar with core and common JMS APIs

Familiar with and understand jmsclient development steps

Implementation of message synchronization and asynchronous receiving

Solution to the Problem of serial packets

Enterprise message system

Java Message Service is developed by Sun. It provides a method for Java programs to notify enterprise message systems. Before discussing JMS, let's analyze the enterprise message system separately.
The Enterprise message system (MOM) provides a flexible and loosely coupled mechanism for integrating applications. They provide asynchronous data transmission between storage and forwarding-based applications, that is, applications do not directly communicate with each other, but communicate with the mom as an intermediary. MOM provides guaranteed message sending. application developers do not need to understand the details of Remote Procedure Call (PRC) and network/communication protocols. Activemq is a good member of Mom.

Advantages of Enterprise message system

Let's take a look. application a sends the message to the server, and application B receives the message sent by a from the server. Through this figure, we will analyze the advantages of JMS.

Figure 1 JMS Communication

Message flexibility

Application A and application B send messages by using the mom Application Programming Interface (API. Mom routes the message to application B so that the message can exist in the mom. Mom is responsible for handling network communication. If the network connection is unavailable, mom will store the message until the connection becomes available, and then forward the message to application B.
Another aspect of flexibility is that when application a sends its messages, application B may not even be running. Mom will keep the message until application B starts running and tries to retrieve the message. This also prevents application a from being congested by waiting for application B to retrieve messages.
Such asynchronous communication requires that the design of applications be different from that of most today's applications. It is only time-independent or parallel processing. It may be a very practical method.

Loose coupling

The real power of Enterprise message systems lies in the loose coupling of applications. In the preceding figure, application a sends a message to specify a specific target, such as "order processing ". Today, application B provides the order processing function.
But in the future, if we can replace application B with different order handlers, application a will no longer be wise. The replacement application will continue to send the message after "order processing", and the message will still be processed.
Similarly, we can replace application a by simply replacing the application to continue sending messages for "order processing ", the order processing program does not need to know whether a new application is sending the order.

What is JMS?

JMS is a collection of interfaces and related semantics. Through these interfaces and methods, how does jmsclient send messages to the message system, creates, sends, receives, and reads messages from the enterprise message system.
Before JMS, every mom manufacturer used proprietary APIs to provide developers for their products. Generally, they can be used in many languages, including Java. Through MOM, JMS provides a standard and convenient method for Java programs to send and receive messages. Programs Written in JMS can be executed on the mom that implements the JMS standard.
The key to JMS portability is that the jms api is provided by Sun as a group of interfaces. Products that provide the JMS function do this by providing a provider that implements these interfaces. Developers can establish JMS applications by defining a group of messages and a group of applications that exchange these messages to Implement Asynchronous Communication.

JMS goals

Since its launch, JMS has been committed to the following goals:
Defines a set of common concepts and useful tools for messages.
All Java applications can use APIs defined in JMS to create, receive, and send messages. Whatever mom that implements the JMS standard, it can be used as a Message Broker, the storage and forwarding of messages are completed.
Maximize the portability of message applications.
MOM provides guaranteed message sending. application developers do not need to understand the details of Remote Process calling (PRC) and network/communication protocols, and provide program portability.
Minimize the coupling between applications and application systems.
Because of the existence of Mom, each application only cares about how to receive and send messages with Mom, without having to pay attention to the other side of mom and how other programs receive and send messages.

Two message models of JMS

JMS provides two message communication models:

Point-to-point (P2P) Model

Publish/subscribe (pub/Sub) Models

Figure 2 JMS Communication Model

It can be seen that clienta and clientb are message producers and send messages to clientc, clientd, cliente, and clientf respectively through two different destinations.
Messages Between clienta, C, and D are point-to-point models. Using this model, the client sends messages to the queue destination (Queue ), from this queue, only one message receiver can receive the message. Other receivers ask the recipient at the same destination not to receive the message. For example, if clientc receives the msg1 message in the queue, clientd receives the msg2 message in the queue.
Messages Between clientb, E, and F are published/subscribed models. With this broadcast model, a client sends messages to a topic, regardless of the number of consumer subscribers that can receive messages from the topic destination. For example, both cliente and clientf receive the message msg3.

Point-to-Point Model

Point-to-point transmission model: the producer sends a message to a specific queue, and the consumer obtains the message from a message queue, for example, as shown in the following figure:

Figure 3 point-to-point communication model

Point-to-Point model features:

Ø each message has a consumer

Each message has only one consumer. If a message is received by the message recipient, other consumers cannot obtain the message.

The sent and received messages are irrelevant to the time.

That is to say, after a producer sends a message, the consumer can receive the message at will, but there are two prerequisites:

1. The message has not expired.

2. The message is not received by other users.

The consumer can also execute the message first. When the producer sends the message to the queue, the consumer can obtain the message from the queue. This is called "waiting for a while".

The consumer must confirm the receipt of the message

After receiving the message, the consumer must confirm that the message has been received. Otherwise, the JMS service provider will feel that the message has not been received, and the message can still be received by others. The program can proactively confirm it without manual intervention.

Ø a non-persistent message can be sent only once

A non-persistent message can be sent only once, indicating that the message may not be sent. The reasons for unsent messages may be:

1. the JMS service provider experiences downtime and other situations, resulting in the loss of non-persistent information

2. The message in the queue has expired and has not been received

Strict sending of persistent messages once

We can set messages that are more important than consumers as persistent messages. Persistent messages will not be lost due to the failure of the JMS service provider or other reasons.

Publish/subscribe Model

Publishing/subscription model: the types of messages delivered by publishing/subscribing are related to topics. The producer publishes a message, and the consumer subscribes to a message of interest. The producer connects the message with a specific topic. The message passing system (MOM) is based on the consumer's interest in attention, send messages to consumers. This type is similar to publishing newspapers and magazines. For example, you can see the following:

Figure 4 publish/subscribe communication model

Announcement/Features of the subscription model:

Every message can have multiple ,......) Subscriber

Each message can have multiple consumers. If the newspaper and magazine are the same, anyone who subscribes to the message can get the message.

Subscribers can only consume messages published after their subscription

This requires that the subscriber subscribe to the subscriber before publishing the producer. That is, the subscriber must execute the subscriber first and then wait for the producer to execute the subscriber. This is different from the point-to-point type.

The subscriber must remain active to use the messages.

That is, the subscriber must remain active and wait for the message to be published by the subscriber. If the subscriber executes the message only after the subscriber publishes the message, the subscriber cannot obtain the message published by the previous subscriber.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.