新建一个java project:ActiveMQTest
从apache-activemq-5.8.0\lib文件夹下导入activemq相关的jar包(该文件夹下的jar基本都能满足需求,只需从该文件夹下导入即可)。
消息发送方代码:
package com.mycom.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* ActiviteMQ方式1:JMS方式
* 消息接收方(消费者)
*
* @author guweiqiang
*/
public class JMSReceiver {
/**
* 接收消息
*/
public static void receiveMessage(String brokerUrl) {
// ConnectionFactory:连接工厂
ConnectionFactory connectionFactory;
// Connection:JMS客户端到JMS Provider的连接
Connection connection = null;
// Session:发送/接收消息的会话
Session session = null;
// Destination:消息目的地
Destination destination;
// MessageConsumer:消息消费者
MessageConsumer messageConsumer;
try {
// 创建连接工厂ConnectionFactory实例
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, brokerUrl);
// 创建连接对象
connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建发送/接收消息的会话
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
destination = session.createQueue("FirstQueue");
// 创建消息消费方
messageConsumer = session.createConsumer(destination);
// 准备工作已经完成,可以开始接收消息了
while (true) {
TextMessage message = (TextMessage) messageConsumer
.receive(100 * 1000);
if (message != null) {
System.out.println("接收到消息:" + message.getText());
} else {
break;
}
}
// 提交会话
session.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 释放资源
if (session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
/**
* 测试
*/
public static void main(String[] args) {
String brokerUrl = "tcp://127.0.0.1:61616";
JMSReceiver.receiveMessage(brokerUrl);
}
}
消息接收方代码:
package com.mycom.activemq;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* ActiviteMQ方式1:JMS方式
* 消息发送方(生产者)
*
* @author guweiqiang
*/
public class JMSSender {
/**
* 发送消息
*/
public static void sendMessage(String brokerUrl, String msg) {
// ConnectionFactory:连接工厂
ConnectionFactory connectionFactory;
// Connection:JMS客户端到JMS Provider的连接
Connection connection = null;
// Session:发送/接收消息的会话
Session session = null;
// Destination:消息目的地
Destination destination;
// MessageProducer:消息生产者
MessageProducer messageProducer;
try {
// 创建一个连接工厂ConnectionFactory实例
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, brokerUrl);
// 创建连接对象
connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建发送/接收消息的会话
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 创建消息目的地
destination = session.createQueue("FirstQueue");
// 创建消息发送方(生产者)
messageProducer = session.createProducer(destination);
messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 设置是否持久化
// 准备工作已经完成,可以开始发送消息了
// 发送消息
TextMessage message = session.createTextMessage(msg);
messageProducer.send(message);
System.out.println("发送消息:" + message.getText());
// 提交会话
session.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 释放资源
if (session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
/**
* 测试方法
*/
public static void main(String[] args) {
String brokerUrl = "tcp://127.0.0.1:61616";
String msg = "test mq msg";
JMSSender.sendMessage(brokerUrl, msg);
}
}
启动ActiveMQ,再在本地执行上述发送方和接收方代码,运行结果如下:
发送方console:
发送消息:test mq msg
接收方console:
接收到消息:test mq msg