Instance of a message-driven bean (MDB)

Source: Internet
Author: User

so far, I've described earlier about java EE is synchronous, that is, the caller invokes a method, and the method must execute immediately and return to the execution result. In the official language, "the client invokes a method through the business interface, and the server completes the method call before returning control to the client." This is the most natural and easiest way to achieve most of the operations we are exposed to. However, in some cases the client does not need to wait for the server to respond, but only to tell the server what should be done, after the completion of the "Tell" task the client can continue to work, and the server silently on the side processing the client's request (usually a lot of clients, 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 exploits the message (JMS? Requests to the server, and of course these requests are ultimately paid toMDBto deal with. Each time the server receives the request, it calls theMDBBusiness interface (a bit similar to a conversation hereBean, but the interface is not added by the developer), and the corresponding implementation will do the corresponding processing.

As blog post said earlier, when using a sessionBean, developers typically create a business interface andBeanclass to implement it (although you can invoke the session in a completely 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 you need 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 possible for message delivery). ForJMSMessage-drivenBean, his business interface isJavax.jms.MessageListener, which defines a single method:OnMessage().

The following code example shows a message-driven Bean (with 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 (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_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); }

@MessageDriventhe annotation marks the class as aMDB. by@ActivationConfigPropertyThe activation configuration attribute defined by the annotation notifies the server of the type of the message delivery system (inQueueand the details of any configuration required by the system. In this case, only ifJMSthe message has a name ofRECIPIENTproperty, and its value isReprotprocessoris called when theMDB. In this example, you need to do a JMS configuration on the server. Whenever the server accepts a message, it calls the message as a parameter.OnMessage() method. Because there is no synchronization connection to the client,OnMessage() method does not return any content.

For a message-driven bean, you only need to know his two tags: asynchronous, message.

Instance of a message-driven bean (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.