Java Message Service Learning (i)

Source: Internet
Author: User
Tags sub domain

One, background

Recently, you need to use ACTIVEMQ to receive the results of Oozie execution after the job is executed. Oozie as the producer of the message, sends the message to ACTIVEMQ, and the client can asynchronously go to ACTIVEMQ to fetch the message.

ACTIVEMQ is a JMS open source-based Apache Message Provider, which records JMS-related fundamentals.

Second, basic knowledge & Fundamental Concepts

1) Message-oriented middleware

message-oriented Middleware (MOM) is the best described as a category of software for communication in an loosely-coupled, rel Iable, scalable and secure MANNERAMONGST distributed applications or systems
Message-oriented middleware is primarily designed to reduce the coupling between applications.

The communication between the previous software is as follows:

Each terminal (application) communicates directly with the host (mainframe).

The idea of MOM is that there is a message mediator between the sender and the recipient of the message. This mediation provides a whole new level of loose coupling for enterprise messaging.

Benefits of Introducing Mom:

①this is the mechanism so allows for loose coupling between senders and receivers as there are no requirement for each be connected to the MOM for sending and receiving messages.

You can do this without requiring both the sender and the receiver to be "online" (actived) for message communication. The sender simply sends the message to mom and then "leaves", and the receiver can fetch it at any subsequent time (depending on what mode of communication is used)

That is, the sender does not need to know the presence of the receiver and can communicate asynchronously.

②moms added many additional features to enterprise messaging this weren ' t previously possible when systems were tightly co Upled.

MOM can also provide some additional functionality: for example, the persistence of messages (message persistence), the conversion of messages, the routing between messages ... These are difficult to implement without introducing the MOM system.

③moms on the market today provide support for a diverse set of protocols for connectivity

Support for a wide variety of connection protocols, i.e. client can be passed through such as http/tcp .... The protocol connects to MOM.

④furthermore, it ' s typical for a MOM to provide an API for sending and receiving messages and otherwise interacting with T He MOM.

Provides the appropriate API to send and receive messages.

2) Why is JMS?

1) describes the benefits of MOM, because there are many MOM vendors in the real world who have developed different MOM products, such as Apache ACTIVEMQ, such as webspheremq .... Each MOM product has its own API for sending and receiving messages, so the compatibility between different products and the uniformity of operations is problematic. The JMS is thus present.

The Java Message Service (JMS) moved beyond Vender-centric MOM APIs to provide a API for enterprise messaging. JMS aims to provide a standardized API to send and receive messages using the Java programming language in a Vendor-neutra l manner.

The JMS API lowers the barrier to the creation of enterprise messaging applications among different JMS providers. It also eases the portability to other JMS providers.

It is seen that JMS clients access a wide variety of JMS-based message middleware products through JMS-defined APIs. This is very similar to how the client accesses the database.

Every JMS has its own products, such as Apache ActiveMQ, and each company adheres to the JMS protocol to develop interfaces for its own products. Users only need to deal with the unified JMS API.

In the database domain, there are MySQL, Oracle, SQL Server ..., but the user program only needs JDBC to access the various databases.

JMS clients utilize the JMS API for interacting with the JMS provider. Similar in concept to the using the-the JDBC API to access data in relational databases, JMS clients with the JMS API for stand ardized access to the messaging service.

Some basic concepts about JMS

JMS PRODUCER-A client application that creates and sends JMS messages.

JMS CONSUMER-A client application that receives and processes JMS messages.

JMS domains-the The messaging that include point-to-point and publish/subscribe.---Point-to-point model and the publish subscription model

JMS provider-the Implementation of the JMS interfaces which are ideally written in 100% pure Java. Equivalent to each vendor who developed a JMS product

Administered objects-preconfigured JMS Objects this contain provider-specific configuration data for use by clients. These objects is typically accessible by clients via JNDI.

Connection factory-clients Use a Connection Factory to create connections to the JMS provider

Destination-an object to which messages is addressed and sent and from which messages is received.

JMS Message

A JMS message consists of three parts: Headers, Properties, Payload

Headers contains the properties of the message: for example, where will the message be sent? Represented by a jmsdestination field;

The delivery mode of the message, represented by Jmsdeliverymode, has two modes of transmission:

1) persistent:advises The JMS provider to persist the message so it's not lost if the provider fails. A JMS provider must deliver a persistent message once-and-only-once. In other words, if the JMS provider fails, the message is not to be lost and is not being delivered more than once.

In this mode, the provider is down, the message is not lost, and the message is delivered only once.

2) non-persistent:instructs The JMS provider not to persist the message. A JMS provider must deliver a non-persistent message at-most-once. In other words, if the JMS provider fails, the message is lost, but it won't be delivered twice.

...//There are many other head properties

Properties are somewhat similar to headers.

Payload: The place where JMS actual messages are stored. Messages can be stored in text format, binary form.

JMS Selector

Consider the fact that there was times when a JMS client was subscribed to a given destination, but it could want to filter t He types of messages it receives. This is exactly where headers and properties can be used.

Message selectors allow a JMS client to specify which messages it wants to receive from a destination based on values in M Essage headers.

Message selector allows users to receive only messages of interest to them.

JMS Domain---message transport model

The point-to-point (PTP) Messaging domain uses destinations known as queues.

The destination of a point-to-point model transfer is a queue.

Each message received on the queue was delivered to once-and-only-once consumer.

Point to point, the message can only be sent to the only one consumer.

Consumers receive messages from the queue either synchronously using the Messageconsumer.receive () method or ASYNCHRONOUSL Y by registering a MessageListener implementation using the Messageconsumer.setmessagelistener () method.

It supports synchronous communication and asynchronous communication, and synchronous communication uses messageconsumer.receive () to receive messages. Asynchronous communication requires the support of the MessageListener listener.

Publish-Subscribe Model

The Publish/subscribe (pub/sub) Messaging domain uses destinations known as topics

The destination of the publish-subscribe model transfer is topics.

Any messages sent to the topic is delivered to all subscribers via a push model, as messages is automatically delivered to the Subscriber.

JMS uses push to proactively push messages to subscribers.

There are two kinds of subscription methods in the publish-subscribe model, one is a durable subscription (durable subscriptions) and the other is a non-persistent subscription.

Non-persistent subscription: A message sent to a topic is received only if the client remains connected to a JMS Provider (such as ActiveMQ). If the client is offline, messages sent to topic during this time period are lost.

Persistent Subscription: Using A durable subscription, when a subscriber disconnects from the JMS provider, it is the responsibility of the JM S provider to store messages for the Subscriber

For more detailed explanations, refer to:

The difference between message durability and message persistence

Message durability for pub/sub domain, it refers to how the recipient receives the message, and if a non-persistent subscription is used, the recipient does not receive the message when it is posted to the message server without connecting to the message server.

Message persistence has nothing to do with domain, and there is a message persistence problem in the point-to-point model. Because it is for the messaging server, it describes the reliability of the message, that is, when the message server goes down, the message is lost.

Message persistence is independent of the message domain. It is used to indicate the JMS application's ability to handle missing messages in the event of a JMS provider failure.

This quality of service are specified on the producer using the Jmsdeliverymode property using either the persistent or non -persistent property.

Reference book: "ActiveMQ in Action" chapter II

Java Message Service Learning (i)

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: info-contact@alibabacloud.com 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.