EJB Series-Messages and MDB

Source: Internet
Author: User
Tags sessions

What is a message

Loosely coupled asynchronous communication process

1. Message-oriented middleware (MOM): Message senders are called producers; 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: Destination becomes a queue, messages can only be consumed once
B. Publish-Subscribe: The destination becomes the main body, the consumer is called the Subscriber, the message can be consumed by any quantity

Java Messaging Service

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

2. Developing the message producer process:
A. Using dependency injection to obtain connection factory ConnectionFactory and destination destination objects
B. Use the connection factory createconnection to open the connection connection
C. Creating session sessions with createsession connected connection and specifying transaction parameters
D. Create a freight queue using the session Createproducer producer
E. Use the createmessage of Session sessions to create a message and set
F. Sending a message using a send from a freight queue producer
G. Freeing Resources
Note: The above process is based on the Java EE 6 scenario, Java EE 7 provides a more simplified a development process

3. Message interface: header, message properties, message body; Implementation class: ObjectMessage Pass object, Bytemessage pass byte, Mapmessage pass map, Streammessage pass stream data, TextMessage pass text

Message-driven beans (MDB)

1. Advantages: Multithreading, simplified message code

2. Design principles:
A. The MDB class must implement the message listener interface directly or indirectly
B. must be specific and public, not final and abstract
C. Must be pojo and cannot be a subclass of another MDB
D. Must have an argument-free constructor
E. cannot have the final method
F. Cannot throw any run-time exceptions, because when thrown is an MDB instance will be terminated

3. Developing consumer processes with MDBs
A. Using annotations @messagedriven to mark a class as an MDB and specify the MDB configuration
B. Implement the MessageListener interface and implement the OnMessage method
C. Implementing logic in OnMessage

4. @MessageDriven: The Annotated class is an MDB with 3 parameters, name specifies the name of the MDB, messagelistenerinterface specifies the message interface that the MDB implements (which can be implements interface directly on the class). Activationconfig used to specify a proprietary configuration property

5. Messagelisener: Register MDB as message consumer, can implement different listener interface according to different scene

6. Activationconfigproperty: Configure configuration information for the message system
A. DESTINATIONTYPE: Notification container whether the MDB listens on a queue or a topic
B. Connectionfactoryjndiname: Specifies the connection factory that is used to create the JMS connection for the MDB Jdni
C. destianname: Specify the destination being monitored
D. Acknowledgemode: Specify JMS session acknowledgement mode
E. subscriptiondurability: Used to set up for persistent subscribers
F. Messageselector: Filtering messages

7. mdb life cycle:
A. Creating MDB instances and setting them
B. Injecting resources
C. Store in managed pools
D. When a destination is detected when a message arrives, remove the idle bean from the pool
E. Execute the message listener method, which is the OnMessage method
F. When the OnMessage method executes, the idle bean is stored back into the pool
G. Withdrawing/destroying beans from the pool as required

8. Send a message from MDB: Inject queue from Jndi, connect factory object, and then act like 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. Transactions can be rollback through the message context object

MDB Best Practices

1. Choose whether to use MDB based on usage

2. Select the message model: you should decide whether to program with PTP or publish-subscribe, but fortunately, switching between the two requires only modifying the configuration

3. Maintain modularity: MDB's OnMessage method should not handle business logic, the business logic should be placed in the corresponding session bean, and injected into the MDB, the MDB is responsible for invoking the corresponding session bean

4. Make full use of the filter or divide the destination according to the scene

5. Select the message type: the type of message to use when selecting a transfer based on the usage scenario

6. Beware of poisonous messages: messages that cannot be consumed but rolled back are trapped in an infinite loop of receive/rollback, although individual vendors have their own implementations of dead messages, but be careful when programming

7. Configure MDB Pool amount Size: Configure according to scenario and requirements

Reference

EJB 3 in Action, Second Edition

EJB Series-Messages and MDB

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.