Jboss5+ejb3+mdb Queue

Source: Internet
Author: User
Tags jboss

The JMS queue is first configured in JBOSS5 when using JBOSS5 for an MDB experiment.

1) under the JBoss installation directory: server\default\deploy\messaging

Opens the Destinations-service.xml file, adding:

<MbeanCode= "Org.jboss.jms.server.destination.QueueService"name= "Jboss.messaging.destination:service=queue,name=expiryqueue"XMBEAN-DD= "Xmdesc/queue-xmbean.xml">      <dependsOptional-attribute-name= "Serverpeer">Jboss.messaging:service=serverpeer</depends>      <depends>Jboss.messaging:service=postoffice</depends></Mbean>  

Start Jboss5, enter http://localhost:8080/jmx-console/(user name, password required beforehand)

In the right-hand interface, click service=jndiviews----> Click the Invoke button for the list to indicate that the JMS queue queue configuration is successful if the corresponding queue Jndi configuration appears

2) Writing an MDB

The Java code is as follows:

ImportJavax.ejb.ActivationConfigProperty;ImportJavax.ejb.MessageDriven;Importjavax.jms.JMSException;ImportJavax.jms.Message;ImportJavax.jms.MessageListener;Importjavax.jms.TextMessage; @MessageDriven (Activationconfig={@ActivationConfigProperty (propertyname= "DestinationType", PropertyValue = "Javax.jms.Queue"), @ActivationConfigProperty (PropertyName= "Destination", PropertyValue = "Queue/expiryqueue")         }) Public classMyqueuemdbbeanImplementsMessageListener { Public voidonMessage (Message arg0) {Try{TextMessage TM=(TextMessage) arg0; System.out.println ("MDB received a message =" +Tm.gettext ()); } Catch(jmsexception e) {e.printstacktrace (); }            }}

3) Client Writing

The Java code is as follows: (need to introduce a JAR file in the client folder under the JBoss installation package)

Importjavax.jms.Connection;Importjavax.jms.ConnectionFactory;ImportJavax.jms.MessageProducer;ImportJavax.jms.Queue;Importjavax.jms.QueueSession;Importjavax.jms.Session;ImportJavax.jms.TextMessage;ImportJavax.naming.Context;ImportJavax.naming.InitialContext; Public classpoint2pclient { Public Static voidMain (string[] args)throwsException {Context Context=NewInitialContext (); ConnectionFactory Factory=(connectionfactory) context. Lookup ("ConnectionFactory"); Queue Queue= (Queue) context.lookup ("Queue/expiryqueue"); Connection Connection=factory.createconnection (); Session Session= Connection.createsession (false, Queuesession.auto_acknowledge); TextMessage msg= Session.createtextmessage ("This is a text: # # # #pppppp"); MessageProducer Sender=session.createproducer (queue);        Sender.send (msg);        Session.close ();        Connection.close (); System.out.println ("+++++success"); }}

The configuration file is as follows: (need to be placed under the project SRC)

java.naming.factory.initial=org.jnp.interfaces.namingcontextfactoryjava.naming.factory.url.pkgs= Org.jboss.naming:org.jnp.interfacesjava.naming.provider.url=localhost

4) The results of the operation are as follows:

Precautions:

1.javax.naming.namenotfoundexception: mytopic not bound

The exception that appears indicates that your JMS queue is misconfigured or that the JMS queue in the EJB and client is wrong

2.TOIPC queues are configured and used in the same way as queue mode, and you can try to change the queue in the above code to topic.

Jboss5+ejb3+mdb Queue

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.