Ejb mdb (Message-driven bean) 4-sample code

Source: Internet
Author: User
Tags jboss server

1. queue:

Client:

Public class mymdbbeanclient {public static void main (string [] ARGs) throws exception {initialcontext ICT = new initialcontext (); // obtain the ICT onfactory object queueconnectionfactory factory = (queueconnectionfactory) ICT. lookup ("connectionfactory"); // gets the connection object queueconnection connection = factory. createqueueconnection (); // gets the session object. The first parameter indicates that the transaction is automatically committed, and the second parameter indicates that once the message is correctly sent, it will automatically send back the response to queuesession session = connecti On. createqueuesession (false, queuesession. auto_acknowledge); // get the destination object queue = (Queue) ICT. lookup ("queue/myqueue"); // sets the message textmessage MSG = session. createtextmessage ("Hello World"); // obtain the sender object queuesender sender = session. createsender (Queue); // send the message sender. send (MSG); Session. close (); system. out. println ("the message has been sent! ");}}

MDB:

Public class mymdbbeanclient {public static void main (string [] ARGs) throws exception {initialcontext ICT = new initialcontext (); // obtain the ICT onfactory object queueconnectionfactory factory = (queueconnectionfactory) ICT. lookup ("connectionfactory"); // gets the connection object queueconnection connection = factory. createqueueconnection (); // gets the session object. The first parameter indicates that the transaction is automatically committed, and the second parameter indicates that once the message is correctly sent, it will automatically send back the response to queuesession session = connecti On. createqueuesession (false, queuesession. auto_acknowledge); // get the destination object queue = (Queue) ICT. lookup ("queue/myqueue"); // sets the message textmessage MSG = session. createtextmessage ("Hello World"); // obtain the sender object queuesender sender = session. createsender (Queue); // send the message sender. send (MSG); Session. close (); system. out. println ("the message has been sent! ");}}

2. Topic

Client:

Public class mytopicmdbbeanclient {public static void main (string [] ARGs) throws exception {initialcontext ICT = new initialcontext (); // obtain the ICT onfactory object topicconnectionfactory factory = (topicconnectionfactory) ICT. lookup ("connectionfactory"); // gets the connection object topicconnection connection = factory. createtopicconnection (); // gets the session object. The first parameter indicates that the transaction is automatically committed, and the second parameter indicates that once the message is correctly sent, the response topicsession session = con is automatically returned. Nection. createtopicsession (false, topicsession. auto_acknowledge); // obtain the destination object topic = (topic) ICT. lookup ("Topic/mytopic"); // sets the message textmessage MSG = session. createtextmessage ("Hello World"); // get the sender object topicpublisher publisher = session. createpublisher (topic); // send the publisher message. publish (MSG); Session. close (); system. out. println ("the message has been sent! ");}}

Mdb1:

@ Messagedriven (activationconfig = {@ activationconfigproperty (propertyname = "destinationtype", propertyvalue = "javax. JMS. topic "), @ activationconfigproperty (propertyname =" destination ", propertyvalue =" Topic/mytopic ")}) public class mytopicmdbbean1 implements messagelistener {public void onmessage (Message MSG) {textmessage message = (textmessage) MSG; try {system. out. println ("mytopicmdbbean1 called:" + message. gettext ();} catch (jmsexception e) {e. printstacktrace ();}}}

Mdb2:

@ Messagedriven (activationconfig = {@ activationconfigproperty (propertyname = "destinationtype", propertyvalue = "javax. JMS. topic "), @ activationconfigproperty (propertyname =" destination ", propertyvalue =" Topic/mytopic ")}) public class mytopicmdbbean2 implements messagelistener {public void onmessage (Message MSG) {textmessage message = (textmessage) MSG; try {system. out. println ("mytopicmdbbean2 called:" + message. gettext ();} catch (jmsexception e) {e. printstacktrace ();}}}

3. Deploy and run the test

Deploy the ejb mdb (queue and toppic) on the JBoss server to run the client.

NOTE: If jboss4.0 is deployed, the following error occurs: javax. Naming. namenotfoundexception: myqueue not bound.

Solution:

Add the following to the mail-service.xml file under the JBoss root directory \ Server \ Default \ deploy:

<Mbeancode = "org. JBoss. MQ. server. JMX. queue" name = "jboss.org. Destination: Server = queue, name = myqueue">

<Attributename = "jndiname"> queue/myqueue </attribute>

<Dependsoptional-Attribute-name = "destinationmanager"> JBoss. MQ: service = destinationmanager </depends>

</Mbean>

Topic not found

<Mbean code = "org. JBoss. MQ. server. JMX. Topic" name = "jboss.org. Destination: Server = topic, name = mytopic">

<! -- JNDI name -->

<Attributename = "jndiname"> topic/mytopic </attribute>

<Dependsoptional-Attribute-name = "destinationmanager"> JBoss. MQ: service = destinationmanager </depends>

</Mbean>

Restart JBoss to solve the problem.

Of course, if there are other mdb, you still need to add jndiname.

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.