ActiveMQ的简单案例
这里导入lib包后即可运行代码,具体的流程可以看资料:首先启动activeMQ,再运行send和receive程序!
package jms; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.BrokerService; import org.apache.log4j.PropertyConfigurator; public class PTP_sends { /** * @param args */ public PTP_sends(){ } private Connection connection; private String subject = "TOOL.DEFAULT"; public void send(String msg){ //创建connectionFaction ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory("tcp://localhost:61616"); try { //创建connection connection=connectionFactory.createConnection(); connection.start(); //创建session,设置消息确认机制 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //创建destination Destination destination=session.createQueue(subject); //创建producer MessageProducer producer=session.createProducer(destination); //设置JMS的持久性 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //JMS消息体 TextMessage message = session.createTextMessage(msg); //发生消息message producer.send(message); System.out.println("消息已经发送。。。。"); //关闭资源 message.clearProperties(); session.close(); connection.stop(); connection.close(); System.out.println("关闭资源。。。。"); } catch (JMSException e) { e.printStackTrace(); } } public static void main(String[] args) { PropertyConfigurator.configure("c:\\log4j.properties"); //代码启动activeMQ // BrokerService broker = new BrokerService(); // try { // broker.addConnector("tcp://localhost:61616"); // broker.start(); // } catch (Exception e) { // e.printStackTrace(); // } PTP_sends ptpSends=new PTP_sends(); ptpSends.send("this is JMS ....."); } }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!