Java Messaging Service (JMS) is a Message Oriented Middleware (MOM) API for sending messages between two or more clients.
JMS API supports two models:
1.Point to Point
2.Publish and Subscribe
Here we are going to look p2p example.
Requirement
Application server : JBoss Server 5.0 GA
Java : jdk 1.5 and above
IDE(optional) : Eclipse Indigo/ Juno
Step 1:Make sure the queue as created with name(here queue name is DLQ) in the path c:/dummypath/jboss 5.1 GA/server/default/deploy/messaging and file is destinations-service.xml
Step 2:Create a Producer to put a message in queue.
Producer
Consumer
Output(Client Console):
Hello World !!!
JMS API supports two models:
1.Point to Point
2.Publish and Subscribe
Here we are going to look p2p example.
Requirement
Application server : JBoss Server 5.0 GA
Java : jdk 1.5 and above
IDE(optional) : Eclipse Indigo/ Juno
Step 1:Make sure the queue as created with name(here queue name is DLQ) in the path c:/dummypath/jboss 5.1 GA/server/default/deploy/messaging and file is destinations-service.xml
<mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.messaging.destination:service=Queue,name=DLQ" xmbean-dd="xmdesc/Queue-xmbean.xml"> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean>
Step 2:Create a Producer to put a message in queue.
Producer
package com.bhuvan; import java.io.*; import java.util.Hashtable; 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 QueueProducer { public final static String JNDI_FACTORY="org.jnp.interfaces.NamingContextFactory"; //*************** Connection Factory JNDI name ************************* public final static String JMS_FACTORY="/ConnectionFactory"; //*************** Queue JNDI name ************************* public final static String QUEUE="queue/DLQ"; private QueueConnectionFactory qconFactory; private QueueConnection qcon; private QueueSession qsession; private QueueSender qsender; private Queue queue; private TextMessage msg; public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext(); QueueProducer qs = new QueueProducer(); qs.init(ic); qs.readAndSend(qs); qs.close(); } private static InitialContext getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, "localhost:1099"); return new InitialContext(env); } public void init(Context ctx)throws NamingException, JMSException { qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createQueueConnection(); qcon.start(); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = (Queue) ctx.lookup(QUEUE); qsender = qsession.createSender(queue); msg = qsession.createTextMessage(); } private void readAndSend(QueueProducer qs) throws IOException, JMSException { msg.setText("Hello World !!!"); qsender.send(msg); } public void close() throws JMSException { qsender.close(); qsession.close(); qcon.close(); } }Step 3:Create a Consumer to receive message.
Consumer
package com.bhuvan; import java.util.Hashtable; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueReceiver; 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 QueueConsumer { public final static String JNDI_FACTORY="org.jnp.interfaces.NamingContextFactory"; //*************** Connection Factory JNDI name ************************* public final static String JMS_FACTORY="/ConnectionFactory"; //*************** Queue JNDI name ************************* public final static String QUEUE="queue/DLQ"; private QueueConnectionFactory qconFactory; private QueueConnection qcon; private QueueSession qsession; private QueueReceiver qreceiver; private Queue queue; private TextMessage msg; public static void main(String[] args)throws NamingException, JMSException { QueueConsumer qc=new QueueConsumer(); try { Context ic=getInitialContext(); qc.getMessage(ic); qc.close(); } catch (NamingException e) { e.printStackTrace(); } } private static InitialContext getInitialContext() throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, "localhost:1099"); return new InitialContext(env); } public void getMessage(Context ctx)throws NamingException, JMSException { qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY); qcon = qconFactory.createQueueConnection(); qcon.start(); qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = (Queue) ctx.lookup(QUEUE); qreceiver = qsession.createReceiver(queue); TextMessage message = (TextMessage)qreceiver.receive(); System.err.println("Received message: " + message.getText()); } public void close() throws JMSException { qreceiver.close(); qsession.close(); qcon.close(); } }Step 4:Run the producer class first and then run the consumer.
Output(Client Console):
Hello World !!!