Java Messaging Services Basics

Source: Internet
Author: User
Tags publish subscribe require

One of the major obstacles to exchanging information among different systems is how to achieve consistency in the precise exchange and formatting of data. The Java Message Service (Java Messaging Services, referred to as JMS) solves this problem by providing a way to interact with Java-EE applications or traditional systems.

The universal interface collection of JMS sends or receives messages asynchronously. Receiving messages asynchronously is clearly the best choice for clients that use intermittent network connections, such as mobile phones and PDAs. In addition, JMS uses a loosely coupled approach to consolidating enterprise systems, whose primary goal is to create portable enterprise-class applications that can use Cross-platform data information, freeing the development workforce.

The Java Messaging Service supports two message models: Point-to-Point messages (Peer-to-peer) and publishing subscription messages (Publish Subscribe messaging, referred to as pub/sub). The JMS specification does not require vendors to support both message models at the same time, but developers should be familiar with the advantages and disadvantages of the two message models.

The Peer-to-peer messaging model is used when passing messages between point-to-point points. If the application developer wants every message to be processed, the Peer-to-peer messaging model should be used. Unlike the PUB/SUB message model, Peer-to-peer messages can always be routed to a specified location.

The PUB/SUB model is used when one or more messages are broadcast. Application developers can also use the PUB/SUB message model if the reliability of a certain degree of message delivery is acceptable. In other words, it applies to all message consumption programs that do not require the ability to receive all of the information or the message consumption program and do not want to receive any messages.

JMS simplifies time dependencies by allowing the creation of persistent subscriptions, and can receive messages even if the message Subscriber is not activated. In addition, using persistent subscriptions provides flexibility and reliability through queues, while still allowing messages to be sent to many recipients.

Topic Subscriber topic Subscriber =
topicSession.createDurableSubscriber(topic, subscriptionName);

The Connection object represents the connection of a message system to either of the two message models. Server-side and client-side objects require management of the state of the JMS connection that is created. The connection was created by the connection factory and positioned through the Jndi lookup.

//取得用于 P2P的 QueueConnectionFactory
QueueConnectionFactory = queueConnectionFactory( );
Context messaging = new InitialContext( );
QueueConnectionFactory = (QueueConnectionFactory)
Messaging.lookup(“QueueConnectionFactory”);
//取得用于 pub/sub的 TopicConnectionFactory
TopicConnectonFactory topicConnectionFactory;
Context messaging = new InitialContext();
topicConnectionFactory = (TopicConnectionFactory)
messaging.lookup(“TopicConnectionFactory”);

Note: The code used for Peer-to-peer is very similar to the code used for Publishsubscribe.

If the session is marked as transactional, the confirmation message is automatically processed by confirmation and correction. If the session is not marked as transactional, you have three options for message acknowledgement.

· Auto_acknowledge session automatically confirms receipt of a message.

· The Client_acknowledge client program confirms that a message is received, calling the confirmation method of the message.

· Dups_ok_acknowledge this option commands session "lazy" acknowledgment message delivery, which can be thought of as a result of some replication messages that the message provider may be passing. This acknowledgement should only be used in cases where the message consumer can tolerate the existence of a potential replica message.

queueSession = queueConnection.createQueueSession(false, session.AUTO_ACKNOWLEDGE);//P2P
topicSession = topicConnection.createTopicSession(false, session.AUTO_ACKNOWLEDGE); //Pub-Sub

Related Article

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.