Java Development JMS for JBOSS4

Source: Internet
Author: User
Tags message queue jboss

For many friends, want to be familiar with JMS related development knowledge, online example write is also more chaotic, special manual compiled an example and server configuration, I hope you like, support my


1 JBoss Configuration Queue Message Queue

Open the JBoss installation directory and locate the file Jbossmq-destinations-service.xml file under SERVER/DEFAULT/DEPLOY/JMS

Add the configuration code as follows:

<mbean code= "Org.jboss.mq.server.jmx.Queue"
Name= "Jboss.mq.destination:service=queue,name=testqueue" >
<depends optional-attribute-name= "Destinationmanager" >jboss.mq:service=DestinationManager</depends>
</mbean>

Start JBoss under Jmx-console, you can query the release Testqueue queue success message, very simple

2 Develop the sending client, copy the Jbossall-client.jar under the Client folder under the JBoss directory to project Lib, write code as follows

Import java.util.Properties;
Import javax.jms.JMSException;
Import Javax.jms.Queue;
Import javax.jms.QueueConnection;
Import Javax.jms.QueueConnectionFactory;
Import Javax.jms.QueueSender;
Import javax.jms.QueueSession;
Import javax.jms.Session;
Import Javax.jms.TextMessage;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;


public class Jmssender {
public static void Main (string[] args) throws JMSException, Namingexception {

Properties env = new properties ();
Env.put (Context.provider_url, "127.0.0.1:1099");
Env.put (Context.initial_context_factory, "org.jnp.interfaces.NamingContextFactory");
Env.put (context.url_pkg_prefixes, "org.jboss.naming:org.jnp.interfaces");
Context context = new InitialContext (env);

Queueconnectionfactory factory = (queueconnectionfactory) context.lookup ("ConnectionFactory");
Queueconnection queueconnection = Factory.createqueueconnection ();
Queuesession queuesession = queueconnection.createqueuesession (False,session.auto_acknowledge);
Queue queue = (queue) context.lookup ("Queue/testqueue");
TextMessage message = Queuesession.createtextmessage ();
Message.settext ("ddddd");
Queuesender Queuesender = queuesession.createsender (queue);
Queuesender.send (queue, message);
System.out.println ("-------------");
}
}

Development completed, very simple

3 The receiving message side opens as follows

Import javax.jms.JMSException;
Import Javax.jms.Queue;
Import javax.jms.QueueConnection;
Import Javax.jms.QueueConnectionFactory;
Import Javax.jms.QueueReceiver;
Import javax.jms.QueueSession;
Import Javax.jms.TextMessage;
Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import javax.naming.NamingException;

public class Jmsreader {
public static void Main (string[] args) throws JMSException, Namingexception {
Properties env = new properties ();
Env.put (Context.provider_url, "localhost:1099");
Env.put (Context.initial_context_factory,
"Org.jnp.interfaces.NamingContextFactory");
Env.put (Context.url_pkg_prefixes,
"Org.jboss.naming:org.jnp.interfaces");
/*
* Connect the server with the parameters.
*/
InitialContext IC = new InitialContext (env);
Queueconnection cs = ((queueconnectionfactory) IC
. Lookup ("ConnectionFactory")). Createqueueconnection ();
/*
* Configure and connect the appropriate queue
*/
Queue q = (queue) ic.lookup ("Queue/testqueue");
Queuesession QSS = cs.createqueuesession (False,queuesession.auto_acknowledge);
QueueReceiver qr = qss.createreceiver (q);
/*
* Launch link
*/
Cs.start ();

/*
* Begin to accept and then process the message received
*/
TextMessage message= (TextMessage) qr.receive ();
System.out.println (Message.gettext ());

Cs.close ();
}

}


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.