Win7
1.下载ActiveMQ
去官方网站下载:官网为http://activemq.apache.org/。
2.运行ActiveMQ
解压缩apache-activemq-5.9.0-bin.zip,然后双击apache-activemq-5.9.0\bin\activemq.bat运行ActiveMQ程序。
启动ActiveMQ以后,登录:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。
登录activemq:http://localhost:8161/admin/
创建简单的生产者----消费者模式例子:
maven依赖:
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.11.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
消息生产者:
package com.controller.demo;
/**
* Created by qixuan.chen on 2017/7/21.
*/
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import javax.jms.*;
/**
* 消息生成者
*/
public class Producer {
private static final Logger LOG = Logger.getLogger(Producer.class);
public static void main(String[] args) {
// 获取连接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://192.168.1.200:61616");
/* 获取连接 */
Connection connection = null;
try {
connection = factory.createConnection();
connection.start();
} catch (JMSException e) {
LOG.error("获取连接出现异常", e);
}
/* 创建会话 */
Session session = null;
try {
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
LOG.error("创建会话出现异常", e);
}
/* 创建消息生产者 */
Destination destination = null;
try {
destination = session.createQueue("TestQueue");
} catch (JMSException e) {
LOG.error("创建队列出现异常", e);
}
/* 创建队列 */
MessageProducer producer = null;
try {
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
} catch (JMSException e) {
LOG.error("创建消息生产者出现异常", e);
}
/* 发送消息 */
ObjectMessage message = null;
try {
message = session.createObjectMessage("hello world...");
producer.send(message);
} catch (JMSException e) {
LOG.error("发送消息出现异常", e);
}
try {
session.commit();
} catch (JMSException e) {
LOG.error("提交会话出现异常", e);
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
LOG.error("关闭连接出现异常", e);
}
}
LOG.info("sent...");
}
}
消费者:
package com.controller.demo;
/**
* Created by qixuan.chen on 2017/7/21.
*/
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
public class Consumer {
private static final Logger LOG = Logger.getLogger(Consumer.class);
// 是否继续响应,可按需由其他逻辑修改值,true:继续响应,false-停止响应
public static volatile boolean handleFlag = true;
public static void main(String[] args) {
// 获取连接工厂
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://192.168.1.200:61616");
/* 获取连接 */
Connection connection = null;
try {
connection = factory.createConnection();
connection.start();
} catch (JMSException e) {
LOG.error("获取连接出现异常", e);
}
/* 创建会话 */
Session session = null;
try {
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
LOG.error("创建会话出现异常", e);
}
/* 创建消息生产者 */
Destination destination = null;
try {
destination = session.createQueue("TestQueue");
} catch (JMSException e) {
LOG.error("创建队列出现异常", e);
}
/* 创建消费者 */
MessageConsumer consumer = null;
try {
consumer = session.createConsumer(destination);
} catch (JMSException e) {
LOG.error("创建消费者出现异常", e);
}
/* 获取消息对象 */
ObjectMessage objectMessage = null;
while(handleFlag) {
try {
objectMessage = (ObjectMessage)consumer.receive();
handleMessage(objectMessage);
} catch (JMSException e) {
LOG.error("接收消息出现异常", e);
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
LOG.error("关闭连接出现异常", e);
}
}
}
/**
* 处理消息对应的业务
* @param objectMessage 消息对象
*/
public static void handleMessage(final ObjectMessage objectMessage) {
if (objectMessage == null) {
return;
}
/* 处理业务 */
Object object = null;
try {
object = objectMessage.getObject();
} catch (JMSException e) {
LOG.error("获取消息内容出现异常", e);
}
handleMessage(object);
}
/**
* 处理消息对应的业务
*/
public static void handleMessage(Object object) {
if (object == null) {
return;
}
String messageString = (String)object;
LOG.info("Receive : " + messageString); // 这里仅作打印业务而已
}
}
看到控制台打印出:Receive : hello world...,可知接收到消息了。