ActiveMQ延时消息推送
前两天做项目遇到一个延时给客户发短信的业务,最先考虑用Redis的订阅发布,但是由于一些原因最后选择了ActiveMQ的消息延时推送
ActiveMQ的下载和安装
1:下载链接
http://activemq.apache.org/activemq-5140-release.html
博主是在Linux下安装的,所以选择了apache-activemq-5.14.0-bin.tar.gz安装包
2:解压安装
tar -zxvf apache-activemq-5.14.0-bin.tar.gz
mv -zxvf apache-activemq-5.14.0-bin.tar.gz activeMQ
3:开启端口及防火墙
(1)如果云服务器访问需要设置安全组开启8161(web管理页面端口)、61616(activemq服务监控端口)两个端口
(2)打开防火墙
/sbin/iptables -I INPUT -p tcp --dport 8161 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status
/sbin/iptables -I INPUT -p tcp --dport 61616 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status
4:设置延时推送(可根据实际情况设置,这里我用到了这样的业务)
配置activemq.xml配置文件,启用延时推送
在activemq.xml配置文件中找到
<broker xmlns="http://activemq.apache.org/schema/core" ... schedulerSupport="true" >
.......
</broker>
注 schedulerSupport=“true” 开启延时投递
启动
cd activeMQ/bin
./activemq start
5:测试是否安装成功
打开浏览器输入http://IP:8161/admin 默认账号:admin 密码:admin
如果要修改控制台的登录用户名密码,可以在conf/jetty-realm.properties文件中修改
到这里ActiveMQ已成功安装
ActiveMQ整合SpringBoot
1 导入pom.xml jar包依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2 配置aplication. properties文件
#activeMQ
spring.activemq.broker-url=tcp://ip地址:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
3 创建生产者类
package com.zewei.producer;
import java.io.Serializable;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
@Service("Producer")
public class Producer {
//消费者1
public static final Destination ONEQUEUE = new ActiveMQQueue("one.queue");
//消费者2
public static final Destination TWOQUEUE = new ActiveMQQueue("two.queue");
@Autowired
private JmsMessagingTemplate jmsTemplate;
/**
* 发送消息
* @param destination destination是发送到的队列
* @param message message是待发送的消息
*/
public <T extends Serializable> void send(Destination destination, T message){
jmsTemplate.convertAndSend(destination, message);
}
/**
* 延时发送
* @param destination 发送的队列
* @param data 发送的消息
* @param time 延迟时间
*/
public <T extends Serializable> void delaySend(Destination destination, T data, Long time){
Connection connection = null;
Session session = null;
MessageProducer producer = null;
// 获取连接工厂
ConnectionFactory connectionFactory = jmsTemplate.getConnectionFactory();
try {
// 获取连接
connection = connectionFactory.createConnection();
connection.start();
// 获取session,true开启事务,false关闭事务
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// 创建一个消息队列
producer = session.createProducer(destination);
producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue());
ObjectMessage message = session.createObjectMessage(data);
//设置延迟时间
message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
// 发送消息
producer.send(message);
session.commit();
} catch (Exception e){
e.printStackTrace();
} finally {
try {
if (producer != null){
producer.close();
}
if (session != null){
session.close();
}
if (connection != null){
connection.close();
}
} catch (Exception e){
e.printStackTrace();
}
}
}
}
4 创建消费者类
这里博主用了两个消费,所以创建的两个,大家可根据实际情况创建
消费者one:
package com.zewei.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class ConsumerOne {
// 使用JmsListener配置消费者监听的队列,其中one是接收到的消息
@JmsListener(destination = "one.queue")
public void send(String one) {
//这里写你需要执行的代码
System.out.println("消费者1=========="+one);
}
}
消费者two:
package com.zewei.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class ConsumerOne {
// 使用JmsListener配置消费者监听的队列,其中two是接收到的消息
@JmsListener(destination = "two.queue")
public void send(String two) {
//这里写你需要执行的代码
System.out.println("消费者2=========="+two);
}
}
将消息保存到队列
@Autowired
private Producer producer;
//将消息1保存到MQ中
producer.delaySend(Producer.ONEQUEUE, "Send_queue_one", 3000);
//将消息2保存到MQ中
producer.delaySend(Producer.TWOQUEUE, "Send_queue_two", 5000);
在这里ActiveMQ消息延时推送使用结束