rabbitmq实现延时队列

1.编写rabbitmq配置类

package com.rabbit.study.rabbitmq_study.rabbitmq.work;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
* @Author 
**/

@Configuration
public class RabbitmqConfig {


    /**
     * 交换机名称
     */
    public  final static String PUSH_EXCHANGE = "test_sout";

    /**
     * 队列名称
     */
    public   final static String PUSH_QUEUE = "delay_queue_push";

    /**
     * routingKey
     */
    public  final static String PUSH_ROUTING_KEY = "delay_key_push";

    /**
     * 延时队列交换机
     * 注意这里的交换机类型:CustomExchange
     *
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //属性参数 交换机名称 交换机类型 是否持久化 是否自动删除 配置参数
        return new CustomExchange(PUSH_EXCHANGE, "x-delayed-message", true, false, args);
    }


    /**
     * 延时队列
     *
     */
    @Bean
    public Queue delayQueue() {
        //属性参数 队列名称 是否持久化
        return new Queue(PUSH_QUEUE, true);
    }

    /**
     * 给延时队列绑定交换机
     *
     */
    @Bean
    public Binding cfgDelayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(PUSH_ROUTING_KEY).noargs();
    }




}

2.编写一个controller,写一个接口进行测试

package com.rabbit.study.rabbitmq_study.rabbitmq.work;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IDEA
 *
 * @author xuzhiteng
 * @date 2022/7/5 14:43
 * Description
 */
@RestController
@RequestMapping("/lazy")
public class LazyController {

    static String str = "yyyy-MM-dd HH:mm:ss";
    static SimpleDateFormat dateFormat = new SimpleDateFormat(str);


    @GetMapping("sout")
    public void sout(@RequestParam("time") String time) throws ParseException {
        boolean b = taskPush(time);
        if(b){
            System.out.println("开始:" + time);
        }


    }

    @Autowired
    private AmqpTemplate rabbitTemplate;


    //定时推送
    private boolean taskPush(String time) throws ParseException {
        Date date = dateFormat.parse(time);
        long times = date.getTime();
        long currentTime = System.currentTimeMillis();
        long taskTime = times - currentTime;
        //log.info("taskPush taskTime:{}", taskTime);
        System.out.println(taskTime);
        if (taskTime < 0) {
            //log.info("推送失败,推送时间不能早于当前时间");
            return false;
        }
        this.rabbitTemplate.convertAndSend(
                //发送至订单交换机
                //RabbitMQConfiguration.pushQueue,
                RabbitmqConfig.PUSH_EXCHANGE,
                //routingKey
                //RabbitMQConfiguration.routingKeyPush,
                RabbitmqConfig.PUSH_ROUTING_KEY
                //这里可以传对象
                , message -> {
                    //如果配置了 params.put("x-message-ttl", 5 * 1000)
                    //那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
                    message.getMessageProperties().setDelay((int) taskTime);
                    return message;
                });
        //log.info("taskPush mq data:{}", JsonUtil.toJson(pushRecord));
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值