What is the message
Loose coupling of asynchronous communication processes
1. Message-oriented middleware (MOM): The sender of the message is called the producer; the location where the message is stored is called the destination; the component that accepts the message is called the consumer
2. Message model:
A. Point-to-point: the destination becomes a queue, the message can only be consumed once
B. Publish – subscribe: the destination becomes the subject, the consumer is called the subscriber, the message can be consumed by any number
Java Message Service
1. JMS API: Provides a unified standard way to access MOM (message middleware) using Java
2. Develop a message producer:
A. Use dependency injection to get the connection factory ConnectionFactory and the destination Destination object
B. Use the connection factory's createConnection to open the Connection
C. Use the Connection createSession method to create a session and specify the transaction parameters
D. Create a shipping queue Producer using the createProducer of the session
E. Create Message Message with the createMessage of the session session
F. Send the message using the shipment queue Producer's send
G. Release resources
Note: The above process is based on JavaEE 6 , JavaEE 7 provides a more simple A development process
3. Message interface: message header, message attribute, message body; implementation class: ObjectMessage pass object, ByteMessage pass bytes, MapMessage pass Map, StreamMessage transfer stream data, TextMessage pass text
Message-driven bean (MDB)
1. Advantages: multi-threaded, simplified message code
2. Design principles:
A. The MDB class must implement the message listener interface directly or indirectly
B. Must be specific public, can not be final and abstract class
C. Must be POJO, can not be another subclass of MDB
D. There must be a constructor with no arguments
E. There can be no final method
F. You can not throw any runtime exceptions because the MDB instance will be terminated when it is thrown
3. Develop a consumer process using MDB
A. Use the annotation @MessageDriven to mark the class as an MDB and specify the MDB configuration
B. Implement the MessageListener interface and implement the onMessage method
C. Implement logic in onMessage
4.MessageDriven: annotated annotated class MDB, the annotation has three parameters, name specifies the name of the MDB, messageListenerInterface specifies the MDB implementation of the message interface (can be directly on the class implements interface), activationConfig used to specify the proprietary Configure properties
5. MessageLisener: the MDB registered as a message consumer, according to different scenes to achieve different listener interface
6. ActivationConfigProperty: Configure the configuration information of the message system
A. DestinationType: Notification container The MDB listens for queues or topics
B. ConnectionFactoryJndiName: Specifies the connection to the JMS connection for creating the MDB. JDNI
C. DestianName: Specifies the destination to be monitored
D. AcknowledgeMode: Specifies the JMS session confirmation mode
E. SubscriptionDurability: used to set up as a durable subscriber
F. MessageSelector: Filtered messages
7. MDB life cycle:
A. Create MDB instances and set them up
B. Inject resources
C. Stored in a managed pool
D. When the destination of the message is detected when the message arrives, the free bean is taken from the pool
E. Execute the message listener method, the onMessage method
F. When the onMessage method is finished, put the free bean back in the pool
G. Undo / destroy beans from the pool as needed
8. Send a message from the MDB: inject the queue from the JNDI, connect the factory object, and then the same as the Java message
9. Manage transactions: Normally, the transaction is opened before the onMessage method, and the transaction is committed at the end of the method. Can use the message context object to rolledback
MDB best practices
1. Select whether to use MDB depending on usage
2. Select the message model: the design should be decided to Point-to-point or Publish – subscribe, but fortunately, the switch between the two need only modify the configuration can be
3. To maintain modularity: MDB's onMessage method should not deal with business logic, business logic should be placed on the corresponding session bean, and into the MDB, MDB is responsible for calling the corresponding session bean
4. Make full use of filters or assign destinations based on your scenes
5. Select the message type: Select the type of message to use when using the scenario
6. Attention to toxic message: can not consume but the news of the rollback will fall into the infinite loop of the reception / rollback, although individual manufacturers have their own deal with the realization of dead messages, but in the programming time to pay attention
7. Configure the MDB pool size: Configure according to the scenario and requirements
Reference