to date, as described earlier java EE Everything is in sync. This means that the caller invokes a method. Then this method must run immediately and return to the running result.
In the official language, "client calls a method through the business interface, and the server finishes the method call before returning control to the client." This is the most natural and easiest way to implement most of the operations we are exposed to. However, there are cases where the client does not need to wait for the server response. And just need to tell the server what to do. After the "Tell" task has been completed, the client can continue to work, while the server silently handles the client's request (which would typically be a lot of client, very many requests).
in thejava EEhas its own solution to the processing of messages--message-drivenBean(Message-driven Bean,MDB). It isjava EEused in asynchronous message delivery.EJBcomponents. Using message-drivenBeanThe client can continue to work normally after requesting the server, as described above. The client uses the message (JMS? Requests to the server, and of course these requests are finally paid toMDBto deal with. Each time the server receives the request. will be calledMDBBusiness interface (a bit similar to the conversation hereBean, but the interface here is not a developer to join, and the corresponding implementation will do the corresponding processing.
just like the blog post said before. When using a sessionBeantime. Developers typically create a business interface andBeanclass to implement it (although you are fully able to invoke the session in a non-interface wayBean). But for message-drivenBean, it needs to implement a specificMDBThe interface of the message system on which it is based. This is a bit of a mouthful, in short, which type of message mechanism is required to implement its corresponding interface. Here toJMSFor example, of courseJMSIs also the most common case (note that otherJavaConnector Architecture (Java connectorarchitecture, ShortJCAMessaging system is also capable of delivering message delivery).
For JMS message-driven beans . His business interface is javax.jms.MessageListener, which defines a single method:onMessage().
The following code shows a sample that shows a message-driven Bean (Used JMS the basic structure of the 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, for the above message-driven bean, his message originates from a servlet (core code such as the following)
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_acknow Ledge); 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/se Rver.log. "); catch (Exception e) {throw new Servletexception (e); }} printhtmlfooter (out); }
@MessageDriven the annotation marks the class as a MDB .
The activation configuration property defined by the @ActivationConfigProperty annotation notifies the server of the type of the message delivery system (in Queue ) and the specific information that the system requires, regardless of configuration.
In such a case, there is only when JMS message has a name of recipient mdb
This example requires a JMS configuration in the server. Corresponds to each time the server accepts a message, it calls the onMessage() method as a number of parameters. Because there is no synchronization connection with the client, the onMessage() method does not return any content whatsoever.
For a message-driven bean, it only needs to know his two tags: asynchronous, message.
Instance of a message-driven bean (MDB)