Home > Java > javaTutorial > body text

What is an asynchronous communication process with loosely coupled messages?

零下一度
Release: 2017-06-28 09:38:00
Original
1461 people have browsed it

What is a message

Loosely coupled asynchronous communication process

1. Message-oriented middleware (MOM): message sender is called the producer; the location where the message is stored is called the destination; the component that receives the message is called the consumer

2. Message model:
a. Point-to-point: The destination becomes a queue, and the message can only be consumed once
b. Publish-subscribe: The destination becomes the subject, the consumer is called a subscriber, and the message can be consumed by any number

Java Message Service

1. JMS API: Provides a unified and standard way to access MOM (message middleware) using Java

2. Develop message producer process:
a. Use dependency injection to obtain the connection factory ConnectionFactory and destination Destination objects
b. Use createConnection of the connection factory to open the connection Connection
c. Use createSession of Connection to create a session and specify transaction parameters
d. Use the createProducer of the session to create the freight queue Producer
e. Use createMessage of Session to create message Message and set
f. Use send of freight queue Producer to send message
g. Release resources
Note: The above process is based on JavaEE 6. JavaEE 7 provides a simpler A development process

3. Message interface: message header, message attributes, message body; implementation class: ObjectMessage transfers objects, ByteMessage transfers bytes, MapMessage transfers Map, StreamMessage transfers stream data, and TextMessage transfers text

Message-driven bean (MDB)

1. Advantages: multi-threading, simplified message code

2. Design principles:
a. The MDB class must directly or indirectly implement the message listener interface
b. It must be concrete and public, it cannot be final or abstract class
c. It must be a POJO and cannot be a subclass of another MDB
d. There must be a constructor without parameters
e. There cannot be a final method
f. No runtime exception can be thrown, because the MDB instance will be terminated when it is thrown

3. Use MDB to develop consumer processes
a. Use the annotation @MessageDriven to mark the class as MDB and specify the MDB configuration
b. Implement the MessageListener interface and implement the onMessage method
c. Implement logic in onMessage

4. @MessageDriven: The annotated class is MDB. This annotation has three parameters. name specifies the name of the MDB. messageListenerInterface specifies the message interface implemented by the MDB (the interface can be implemented directly on the class). activationConfig is used to specify the proprietary Configuration properties

5. MessageLisener: Register MDB as a message consumer and implement different listener interfaces according to different scenarios

6. ActivationConfigProperty: Configure the configuration information of the message system
a. destinationType: informs the container whether the MDB is listening to a queue or a topic
b. connectionFactoryJndiName: Specifies the connection factory JDNI
used to create a JMS connection to the MDB c. destianName: Specifies the listening destination
d. acknowledgeMode: Specify JMS session confirmation mode
e. subscriptionDurability: used to set as a durable subscriber
f. messageSelector: filter messages

7. MDB life cycle:
a. Create MDB instances and set them up
b. Inject resources
c. Store in a managed pool
d. When detecting the destination where the message arrives, take out the idle bean
from the pool. e. Execute the message listener method, that is, onMessage method
f. When the onMessage method is executed, save the idle bean back to the pool
g. Revoke/destroy beans from the pool as needed

8. Send messages from MDB: Inject into the queue from JNDI, connect to the factory object, and then perform the same operation as Java messages

9. Manage transactions: Under normal circumstances, the transaction is started before the onMessage method and the transaction is submitted at the end of the method. The transaction can be rolled back through the message context object

MDB best practices

1. Choose whether to use MDB according to usage

2. Choose the message model: PTP or publish-subscribe should be decided during program design, but fortunately, switching between the two only requires modifying the configuration

3. Maintain modularity: MDB’s onMessage method should not handle business logic. Business logic should be placed in the corresponding session bean and injected into MDB. MDB is responsible for calling the corresponding session bean

4. Make full use of filters or divide destinations according to the scenario

5. Select the message type: Select the message type used for transmission according to the usage scenario

6. Be wary of toxic messages: Messages that cannot be consumed but are rolled back will fall into an infinite loop of receiving/rolling back. Although individual manufacturers have their own implementations of processing dead messages, you should pay attention when programming

7. Configure the MDB pool size: Configure according to the scenario and requirements

Reference

EJB 3 in Action, Second Edition

The above is the detailed content of What is an asynchronous communication process with loosely coupled messages?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template