Message-driven bean (MDB) instance and beanmdb instance
So far, all the Java ee-related items described above are synchronized. That is to say, if the caller calls a method, the method must be executed immediately and return the execution result. In some official languages, the client calls a method through a service interface. The server completes this method call before returning control to the client ". This is the most natural and easy way to implement most of the operations we have come into contact. However, in some cases, the client does not need to wait for the server to respond, but only needs to tell the server what to do. After the "" task is completed, the client can continue to work, while the server silently processes client requests (usually many clients and many requests ).
In JavaEE, there is a solution for Message Processing-Message-Driven bean (MDB ). It is an EJB component used in JavaEE for asynchronous message transmission. Using message-driven bean, the client mentioned above can continue to work normally after requesting the server. The client uses the message (JMS ?) Send requests to the server. Of course, these requests are ultimately delivered to MDB for processing. Whenever the server receives a request, it will call the MDB Service Interface (this is a bit similar to Session bean, but the interface here is not added by developers ), then the corresponding implementer will handle the problem accordingly.
As mentioned in the previous blog post, when using a session bean, developers usually create a business interface, and implement it in the bean class (although you can call the session bean in a non-interface way ). However, for message-driven bean, it needs to implement an interface specific to the Message System Based on MDB. There are some interfaces in this sentence. In short, the corresponding interfaces must be implemented based on the message mechanism. Here we use JMS as an example. Of course, JMS is also the most common situation. (Note that other Java connector-based architectures (Java ororarchitecture, JCA for short) ). For the JMS message-driven bean, its business interface is javax. jms. MessageListener, which defines a single method: onMessage ().
The following code example shows the basic structure of a message-driven bean (using the JMS method.
@MessageDriven( mappedName="destinationQueue", activationConfig = { @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), @ActivationConfigProperty(propertyName="messageSelector", propertyValue="RECIPIENT='ReportProcessor'")})public class ReportProcessorBean implements javax.jms.MessageListener { public void onMessage(javax.jms.Message message) { try { System.out.println("Processing message: " + ((TextMessage) message).getText()); } catch (JMSException e) { e.printStackTrace(); } }}
In this example, the above message-driven bean is generated from a Servlet (the core code is as follows)
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); printHtmlHeader(out); // if there was info submitted, send the message String messageText = request.getParameter("message"); if (messageText != null) { try { QueueConnectionFactory factory = (QueueConnectionFactory) new InitialContext().lookup("java:comp/env/jms/MyQueueConnectionFactory"); Queue destinationQueue = (Queue) new InitialContext().lookup("java:comp/env/jms/MyQueue"); QueueConnection connection = factory.createQueueConnection(); QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender sender = session.createSender(null); Message message = session.createTextMessage(messageText); message.setStringProperty("RECIPIENT", "ReportProcessor"); sender.send(destinationQueue, message); connection.close(); // print a response to the html stream out.println("Message \"" + messageText + "\" sent! See the console " + "or the log file at <EXAMPLES_HOME>/glassfish/domains/domain1/logs/server.log."); } catch (Exception e) { throw new ServletException(e); } } printHtmlFooter(out); }
@ MessageDriven annotation marks the class as an MDB. The activation Configuration Attribute defined by @ ActivationConfigProperty annotation notifies the server of the type of the message passing system (in Queue mode) and the detailed information of any configuration required by the system. In this case, MDB is called only when the JMS message has a property named RECIPIENT and its value is ReprotProcessor. In this example, configure JMS on the server. When the server receives a message, it calls the onMessage () method as a parameter. Because there is no synchronous connection with the client, the onMessage () method does not return any content.
For a message-driven bean, you only need to know its two tags: asynchronous and message.