Message-driven beans are primarily used to accept and process asynchronous messages, which may come from an external system or other components in the same application, and are called asynchronous, because they can arrive at any time, and the result of a different generic remote method call is immediately immediate. Similar to how event handling in the UI works, message-driven beans are used to listen for asynchronous messages sent to it. It is worth noting that, unlike the remote calling method, the sender of an asynchronous message does not stop to wait for the other person's response.
The message-driven bean is a standard service (tool) in the Java EE, the full name of which is the JavaScript message service API, referred to as JMS. The Java Message Service API is a Java API that is located in the JAVAX.JMS package and provides an interface for all applications that require messaging system services. The message system can send and receive messages asynchronously, which can be text, objects, or some other type. This is in contrast to the remote procedure call (RPC) model, where the interaction between components occurs synchronously in a remote procedure call. Any implementation of a message system that conforms to the JMS API specification can be called a JMS provider (JMS Provider). In the Java EE SDK with this JMS Provider, the example in the book is also the JMS Provider used, and each vendor has its own business implementation for consuming services in enterprise applications. The context of the JMS example given in the book can be used to indicate:
In, Timeit is a session bean whose main function is to send asynchronous messages to Messagewriter, a message-driven bean, Timeit and messagewriter two enterprise beans can be called JMS clients, Because they are all clients of the underlying messaging system. The messaging system implements asynchronous communication by providing a destination (destination) to hold messages until those messages are sent to the receiver (recipient). The middle Logwriterqueue is a destination that holds all the messages from the Timeit session Bean, which are sent to the Messagewriter message-driven bean. There are two types of destinations (destination) in JMS:
Queue, which holds messages sent from one JMS client to another JMS client, which is called a point-to-point (point-to-point);
Subject (TOPIC), which holds messages from many potential JMS clients to multiple potential JMS clients, known as the Publish/subscribe (publish/subscribe) model;
The example used in the book is the destination of the queue type. There are also two "roles" in the JMS message producer and the JMS message consumer, which is the class in the JMS provider that represents the JMS client to send and receive messages. When we use a message-driven bean, we don't have to create a JMS message consumer ourselves, because the EJB container will do it for us, but the JMS message maker needs us to create it ourselves, and we need to create a JMS message maker to the destination (possibly a queue or a topic , the example in the book is the queue, the Logwriterqueue in which the message is sent. To achieve this, we need two management objects (administered object), which are usually managed by the management tools that the JMS provider takes, and these two management objects are:
Connection factory (Connection factory) for creating a connection to a JMS provider
Destination (destination), which can be javax.jsm.Queue and javax.jsm.Topic types
A typical process for sending messages is as follows:
The first step is to get a connection to the factory, in the book example, because the type of queue used is the destination, as shown here
Queueconnectionfactory, where xxxxx is the Jndi name set at deployment time
InitialContext jndicontext = new InitialContext ();
Queueconnectionfactory queueconnectionfactory =
(queueconnectionfactory) jndicontext.lookup ("XXXX")
The second step, get the destination, the book example uses the QUEUE,XXXXX is also the deployment time set Jndi name
Queue queue = (queue) jndicontext.lookup ("XXXXX");
Step three, get a connection from the connection factory
Queueconnection =queueconnectionfactory.createqueueconnection ();
Fourth step, create a session
Queuesession queuesession =
Queueconnection.createqueuesession (False,
Session.auto_acknowledge);
Fifth step, create a sender to queue for the current session
Queuesender Queuesender = queuesession.createsender (queue);
Sixth step, create a message
TextMessage message = Queuesession.createtextmessage ();
SimpleDateFormat SDF =new SimpleDateFormat ("yyyy. Mm.dd ' at ' HH:mm:ss. SSS ");
Message.settext ("Log entry, the time is:" + Sdf.format (new Date ()));
Seventh step, send a message
Queuesender.send (message);
Eighth step, close the connection
Queueconnection.close ();
In a message-driven bean that handles the actions that are performed after receiving an asynchronous message, a message-driven bean must inherit two interfaces, which are:
Messagedirvenbean: Contains the life cycle method of the message-driven bean
MessageListener: Contains the OnMessage () method, which is called when a message arrives at the message-driven bean.
(Java EE) message-driven bean