ActiveMQ 入门使用实例
Posted panie2015
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActiveMQ 入门使用实例相关的知识,希望对你有一定的参考价值。
1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/download-archives.html
2.运行ActiveMQ
解压缩apache-activemq-5.9.0-bin.zip,然后双击apache-activemq-5.9.0\bin\activemq.bat运行ActiveMQ程序。
3.在Eclipse 创建项目并运行
如果是使用 Maven 项目,则在pom.xml 中配置
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.9.0</version> </dependency>
或者 直接创建项目,导入 activemq-all-5.9.0.jar ,以及其日志依赖包
4.创建一个消息队列
Consumer.java
package com.panie.mq.queue; import java.util.Date; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Consumer { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; String url = ActiveMQConnection.DEFAULT_BROKER_URL; String subject = "TOOL.DEFAULT"; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); try { Connection connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(subject); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message msg) { MapMessage message = (MapMessage)msg; Date date = new Date(); try { System.out.println("--收到消息:"+new Date(message.getLong("count"))); session.commit(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); Thread.sleep(10000); session.close(); connection.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Producer.java
package com.panie.mq.queue; import java.util.Date; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.MessageProducer; import javax.jms.Session; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Producer { public static void main(String[] args) { String user = ActiveMQConnection.DEFAULT_USER; String password = ActiveMQConnection.DEFAULT_PASSWORD; String url = ActiveMQConnection.DEFAULT_BROKER_URL; String subject = "TOOL.DEFAULT"; ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url); try { Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(subject); MessageProducer producer = session.createProducer(destination); for(int i=0;i<=20;i++) { MapMessage message = session.createMapMessage(); Date date = new Date(); message.setLong("count", date.getTime()); Thread.sleep(1000); producer.send(message); System.out.println("--发送消息:"+date); } session.commit(); session.close(); connection.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行这两个方法,则可以在控制台看到 consumer 输出了 producter 产生的消息
以上是关于ActiveMQ 入门使用实例的主要内容,如果未能解决你的问题,请参考以下文章