- 百度ActiveMQ,到官网下载对应压缩文件
- 解压文件到bin目录下启动项目
3.使用idea,创建springboot项目,添加ActiveMQ依赖
4.调用(JMS)规范接口API发送消息到MQ
package test.mq.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Sender {
public static void main(String[] args) throws JMSException {
//1、建立connectionFactory工厂(用户名,密码,url,均使用默认即可,默认端口为:tcp://localhost:61616)
ConnectionFactory factory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_USER,
ActiveMQConnectionFactory.DEFAULT_PASSWORD,
"tcp://localhost:61616");
//2、通过工厂创建connection,调用start方法开启连接,Connection默认是关闭
Connection connection = factory.createConnection();
connection.start();
//3、通过connection创建session,用于接收消息,参数配置1为是否启用事务,参数配置2为签收模式,一般配置为自动签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4、session创建destination对象,用户端用来指定生产消费目标和消费消息来源的对象,在程序中可使用多个Queue和Topic
//在PTP模式中,destination被称为Queue即队列,
//在Pub/Sub模式,destination被称为topic即主题
Destination destination = session.createQueue("queue1");
//5、通过session创建消息的发送和接收对象(生产者/消费者)MessageProducer/MessageConsumer
MessageProducer producer = session.createProducer(destination);
//6、可使用MessageProducer的setDeliveryMode方法为其设置持久化和非持久化特性(DeliveryMode)
// producer.setDeliveryDelay(DeliveryMode.NON_PERSISTENT);
//7、使用JMS规范的TextMessage形式创建数据(通过session对象),并用producer中的send方法发送数据,客户端使用receive接收
//JMS规范的message类型:StreamMessage、MapMessage、TextMessage、ObjectMessage、BytesMessage
for (int i =1;i<=5;i++){
TextMessage message = session.createTextMessage("消息内容,id为:" + i);
//message.setText("消息内容");
producer.send(message);
}
if (connection != null){
connection.close();
}
}
}
5.调用(JMS)规范接口收到MQ中的消息
package test.mq.activemq;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Receiver {
public static void main(String[] args) throws JMSException {
//1、建立connectionFactory工厂(用户名,密码,url,均使用默认即可,默认端口为:tcp://localhost:61616)
ConnectionFactory factory = new ActiveMQConnectionFactory(
ActiveMQConnectionFactory.DEFAULT_USER,
ActiveMQConnectionFactory.DEFAULT_PASSWORD,
"tcp://localhost:61616");
//2、通过工厂创建connection,调用start方法开启连接,Connection默认是关闭
Connection connection = factory.createConnection();
connection.start();
//3、通过connection创建session,用于接收消息,参数配置1为是否启用事务,参数配置2为签收模式,一般配置为自动签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4、session创建destination对象,用户端用来指定生产消费目标和消费消息来源的对象,在程序中可使用多个Queue和Topic
//在PTP模式中,destination被称为Queue即队列,
//在Pub/Sub模式,destination被称为topic即主题
Destination destination = session.createQueue("queue1");
//5、通过session创建消息的发送和接收对象(生产者/消费者)MessageProducer/MessageConsumer
MessageConsumer consumer = session.createConsumer(destination);
//6、可使用MessageProducer的setDeliveryMode方法为其设置持久化和非持久化特性(DeliveryMode)
// 作为消费者不需要持久化,只是取消息
//7、
while (true){
TextMessage message = (TextMessage)consumer.receive();
//MapMessage msg = (MapMessage)consumer.receive();
if (message == null) break;
System.out.println("收到的内容:" + message);
}
//关闭连接
if (connection != null){
connection.close();
}
}
}