ActiveMQ 延时消息 实现 订单定时取消

ActiveMQ 延时消息 实现 订单定时取消

ActiveMQ 版本 5.15.15

  1. 修改activemq的配置文件, 添加schedulerSupport=“true”
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true">
  1. cd到安装目录的bin文件中 重启MQ
关闭命令:./activemq stop
启动命令: ./activemq start
重启命令: ./activemq restart
  1. 核心代码
//The time in milliseconds that a message will wait before being scheduled to be delivered by the broker
private void sendMessage(Destination destination, final String message) {
          
   
        Map<String, Object> headers = new HashMap<>();
        //设置延时多长时间, 单位毫秒
        headers.put(ScheduledMessage.AMQ_SCHEDULED_DELAY, 1000 * 60);
        jmsMessagingTemplate.convertAndSend(destination, message, headers);
    }
经验分享 程序员 微信小程序 职场和发展