最近,因工作需要对JMS进行了些了解和学习。不敢藏私,把些许经验和大家分享下,顺带自己也总结下,如有不足请指出。学习ActiveMq首先要对JMS规范有所了解。
JMS教程 这篇文章介绍的挺不错,虽然也是转载的不过好在排版还可以!呵呵!
要进行ActiveMq的开发,除了对概念要有所了解外,还要对JMS常用的接口有所了解,上面文档里也有描述。
下面2个程序,一个是向ActiveMq发送JMS消息,另一个是从ActiveMq里读取JMS消息!
package cn.fengsage.jms; import java.util.Hashtable; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; /** * @author Fred Zhu * */ public class SenderTest { private static final String DistDestQueName = "test"; private static final String JMS_FACTORY = "ConnectionFactory"; private Context jndiContext = null; private QueueConnectionFactory qconFactory = null; private QueueConnection qconn = null; private QueueSession qsession = null; private Queue queueDest = null; private QueueSender qsender = null; public void sendMessage(String msg) throws NamingException, JMSException { jndiContext = this.getJndiContext(); qconFactory = (QueueConnectionFactory) jndiContext.lookup(JMS_FACTORY); qconn = qconFactory.createQueueConnection(); qsession = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueDest = (Queue) jndiContext.lookup(DistDestQueName); qsender = qsession.createSender(queueDest); TextMessage objMsg = null; objMsg = qsession.createTextMessage(); qconn.start(); objMsg.setText(msg); qsender.send(objMsg); qconn.close(); qsender.close(); } private Context getJndiContext() throws NamingException { Hashtable properties = new Hashtable(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); properties.put(Context.PROVIDER_URL, "tcp://localhost:61616"); return new InitialContext(properties); } public static void main(String[] args) throws NamingException, JMSException { SenderTest test = new SenderTest(); test.sendMessage("jms test"); } }
package cn.fengsage.jms; 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.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; /** * @author Fred Zhu * */ public class ReceiveTest { private static final String DistDestQueName = "test"; private static final String JMS_FACTORY = "ConnectionFactory"; private Context jndiContext = null; private QueueConnectionFactory qconFactory = null; private QueueConnection qconn = null; private QueueSession qsession = null; private Queue queueDest = null; private QueueReceiver qreceiver = null; public void reveiveMessage() throws NamingException, JMSException { jndiContext = this.getJndiContext(); qconFactory = (QueueConnectionFactory) jndiContext.lookup(JMS_FACTORY); qconn = qconFactory.createQueueConnection(); qsession = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueDest = (Queue) jndiContext.lookup(DistDestQueName); qreceiver = qsession.createReceiver(queueDest); qconn.start(); TextMessage message = (TextMessage) qreceiver.receive(); System.out.println(message.getText()); qconn.close(); } private Context getJndiContext() throws NamingException { Hashtable properties = new Hashtable(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); properties.put(Context.PROVIDER_URL, "tcp://localhost:61616"); return new InitialContext(properties); } public static void main(String[] args) throws NamingException, JMSException { ReceiveTest test = new ReceiveTest(); test.reveiveMessage(); } }
评论关闭。