J2EE's asynchronous message mechanism (on)

xiaoxiao2021-03-06  63

In distributed enterprise applications, asynchronous message mechanism is used to effectively coordinate the work of each part. J2EE provides us with a Message-Driven bean that implements asynchronous messaging between applications. One. What is a message system? Typically a message system allows reliability communication between unpacked applications. In an enterprise application, there is a need for asynchronous, non-blocking messaging. For example, a client may wish to send a request to a request, do not care about whether it will receive it immediately. In this way, the client has no reason to wait for the server to process the request. After the client application is submitted, you can handle other tasks with just make sure that the request is requested to reach the server. Usually, this is very efficient. The message system provides the advantages of many other distributed object computing models. It encourages "loose coupling" between message generator and users, with a high degree of transaction between them. For the user, it does not care when to generate a message, whether the generator is still in the network and when the message is generated. This allows for establishing a dynamic, reliable and flexible system. The entire subsystem can be modified without affecting the rest of the system. Additionally, the high degree of scalability of the system is easy to integrate with other systems, as well as high reliability. Due to reliability and scalability, they are used to solve many business and scientific calculations. For example, the message system is the basis of many applications, which can be workflow, network management, communication service, or supply chain management program. In Java technology, the ability to process asynchronous messages is implemented by JMS. JMS initially designed to provide a standard Java interface to the traditional message object middleware. These products must be in an enterprise application. There are now many products that support JMS's pure Java. Message system types typically have two messages. 1. Publish / Subscribe (Publish / Subscribe) Publish / Subscription Message System supports an event-driven model, message generator, and user participating in the message. Generator publishing an event, while the user subscribes to the incident of interest and uses an event. Generator will connect messages and a specific topic (Topic), and the message system transmits the message to the user according to the interest of the user registered. 2. PEER TO Peer In a point-to-point message system, the message is distributed to a separate user. It maintains an "enter" message queue. Message application sends a message to a specific queue, and the client gets a message from a queue. Two. JMS Introduction The purpose of JMS is to provide a fixed interface to the message system customer, and is independent of the underlying message provider. In this way, the client's application can be transplanted in different machines and operating systems, and can be transferred between different messaging systems. All JMS clients are built in Java technology, so they can also use other JavaAPIs, such as JDBC database connections, using JavaBean component models, JDNI name service, JTA client transaction control, and J2SE and J2EE API to implement enterprise-level application services. program. 1.JMS Object Model Figure 1 shows the JMS object, which is used to provide an object connected to the JMS service provider. ConnectionFactory is a client to create a Connection management object. This object is relatively large because there is an authorization and communication establishment process when CONNECTION is created. The Destination object packages the purpose of the message and the address and configuration information related to the service provider. Session is a JMS entity to support transaction processing and asynchronous messaging.

JMS does not require the client's code for asynchronous messages or can handle multiple concurrent messages. Typically, the complexity of the transaction is packaged by a session. A session is an atomic unit work, like a transaction of the database, is difficult to implement multithreaded transactions. Session provides the advantage of concurrent in a thread programming mode. MessageProducer and MessageConsuMer objects are created by the Session object. Used to send and accept messages. To ensure that the message is passed, the JMS service provider processed the message to be in the Persistent mode. The Persistent mode allows the JMS provider to save the message. SESSION, MessageProducer and MessageConsumer are not supported, while ConnectionFactory, Destination, and Connection support concurrency. 2. JMS Application Development JMS Message in the message system, the key to communication between applications is the message. So using JMS must first understand the message. In JMS, the message consists of three parts: Message Header is used to identify messages, such as whether a given message is a "subscriber" Properties for the application related, provider-related and optional Information Body is the content of the message, supports several formats, including TextMessage (a simple package for String) and ObjectMessage (package of any object, but must support serialization), and other formats. TEXTMESSAGE A TextMessage is a package of String objects. It is useful when only text objects are passed. It assumes that many messages are built on XML. Thus TextMessage can be a container that packs them. Creating a textMessage object is simple, as follows: TextMessage Message = session.createMessage (); Message.Settext ("Hello, World!"); ObjectMessage is shown in the name, it is a message for a Java object. Any serialized Java object can be used for ObjectMessage, and if multiple objects can be packaged in one message, you can use the Collection object to include multiple serialized objects. Below is a creation of ObjectMessage ObjectMessage Message (); Message.SetObject (MyObject); create a typical JMS client to create a typical JMS client to create: Create a message system provider Connections Create a session that creating and sending messages to create MessageProducer and MessageConsumer to create and receive messages When completing the above steps, a message generator client will create and publish a message to a topic, and message user The client will receive messages related to one topic. 1. Create a Connection A Connection provides access to the underlying message system for the client. And achieve resource allocation and management.

转载请注明原文地址:https://www.9cbs.com/read-84655.html

New Post(0)