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.