ActiveMQ消息队列是apache下面的开源的消息中间件,使用方便扩展性好。下面我就我学习mq跟大家分享一下:
ActiveMQ环境的配置 下载ActiveMQ:http://activemq.apache.org/ 解压缩apache-activemq-5.9.1-bin.zip,然后双击apache-activemq-5.9.1\bin\win64\activemq.bat运行ActiveMQ程序。启动ActiveMQ以后,登陆:http://localhost:8161/admin/ 用户名和密码都是admin打开eclipse创建一个Java项目
1. 创建一个消息生产者 package com.java.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class MessageProvider { private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; private static final String BROKERURL=ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { ConnectionFactory connectionFactory;//创建连接工厂 Connection connection = null;//创建连接 Session session;//创建session 消息的线程 Destination destination;//创建消息发送目的地 MessageProducer messageProducer;//消息生产者 try { connectionFactory = new ActiveMQConnectionFactory(MessageProvider.USERNAME, MessageProvider.PASSWORD, MessageProvider.BROKERURL); connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue1"); messageProducer = session.createProducer(destination); sendMessage(session, messageProducer); session.commit(); } catch (JMSException e) { e.printStackTrace(); }finally{ try { if(connection != null){ connection.close(); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void sendMessage(Session session , MessageProducer messageProducer)throws JMSException{ for(int i = 0 ;i<10;i++){ TextMessage message = session.createTextMessage("activeMQ发送的消息:"+i); messageProducer.send(message); System.out.println("发送消息:activeMQ发送的消息:"+i); } } } 运行的效果如下:log4j:WARN No appenders could be found for logger (org.apache.activemq.thread.TaskRunnerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 发送消息:activeMQ发送的消息:0 发送消息:activeMQ发送的消息:1 发送消息:activeMQ发送的消息:2 发送消息:activeMQ发送的消息:3 发送消息:activeMQ发送的消息:4 发送消息:activeMQ发送的消息:5 发送消息:activeMQ发送的消息:6 发送消息:activeMQ发送的消息:7 发送消息:activeMQ发送的消息:8 发送消息:activeMQ发送的消息:9
2. 创建一个消息消费者 package com.java.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class JMSConsumer { private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; private static final String BROKERURL=ActiveMQConnection.DEFAULT_BROKER_URL; public static void main(String[] args) { ConnectionFactory connectionFactory;//创建连接工厂 Connection connection = null;//创建连接 Session session;//创建session 消息的线程 Destination destination;//创建消息发送目的地 MessageConsumer messageConsumer;//消息生产者 try { connectionFactory = new ActiveMQConnectionFactory( JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKERURL); connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue1"); messageConsumer = session.createConsumer(destination); while(true){ TextMessage textMessage = (TextMessage)messageConsumer.receive(100000); if(textMessage != null){ System.out.println("接收到的消息为:"+textMessage.getText()); }else{ break; } } } catch (JMSException e) { e.printStackTrace(); }finally{ try { if(connection != null){ connection.close(); } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }执行的效果是:
log4j:WARN No appenders could be found for logger (org.apache.activemq.thread.TaskRunnerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 接收到的消息为:activeMQ发送的消息:0 接收到的消息为:activeMQ发送的消息:1 接收到的消息为:activeMQ发送的消息:2 接收到的消息为:activeMQ发送的消息:3 接收到的消息为:activeMQ发送的消息:4 接收到的消息为:activeMQ发送的消息:5 接收到的消息为:activeMQ发送的消息:6 接收到的消息为:activeMQ发送的消息:7 接收到的消息为:activeMQ发送的消息:8 接收到的消息为:activeMQ发送的消息:9