延时队列demo

 
package com.jst.park.rabbit;

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

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSON;
import com.jst.park.common.constant.MqCodeConstant;
import com.jst.park.common.util.ConfigUtil;
import com.jst.park.model.MqMsg;
import com.jst.park.service.car.BrandCarService;
import com.jst.prodution.base.bean.BaseBean;


/**
 * 类名: CarBrandInfoQueueConfig</br>
 * 作者: lin
 * 描述: 车辆品牌信息获取(获取易车网http://shenzhen.bitauto.com/) 
 * 创建时间: 2018年8月9日下午3:33:42
 * 版权及版本: Copyright(C)2018 jst版权所有
 */
//@Configuration
public class BrandCarInfoQueueConfig {
    
    
    private static Logger logger = LoggerFactory.getLogger(BrandCarInfoQueueConfig.class);    
    
     @Autowired
    private AmqpTemplate amqpTemplate;
     
     private static AmqpTemplate localAmqpTemplate;
     
     @Autowired
     BrandCarService  brandCarService ;
     
     @PostConstruct
        public void init() {
         localAmqpTemplate = this.amqpTemplate;
         //设置默认
        /* RabbitTemplate template = (RabbitTemplate) this.amqpTemplate ;
         template.setExchange(ConfigUtil.getProperty("mq.exchange.brandCarDirectExchange"));
         template.setRoutingKey(ConfigUtil.getProperty("mq.key.brandCarKey"));
         localAmqpTemplate = template ;*/
        }
     
       //延时队列定义
        @Bean
        public Queue brandCarQueue() {
                Map<String, Object> arguments = new HashMap<String, Object>() ;
                arguments.put("x-message-ttl", Long.valueOf("604800000")) ; //一天(24小时)
                //arguments.put("x-message-ttl", Long.valueOf("60000")) ; // 一分钟
                //定义死信路由
                arguments.put("x-dead-letter-exchange", ConfigUtil.getProperty("mq.exchange.brandCarDirectExchange")) ;
                arguments.put("x-dead-letter-routing-key", ConfigUtil.getProperty("mq.key.brandCarKey")+"_target") ;
                return new Queue(ConfigUtil.getProperty("mq.queue.brandCarQueue"), true, false, false, arguments) ;
        } 
        
        //定义延时队列通过死信路由转发的目标队列
        @Bean
        public Queue brandCarQueueTarget() {
                return new Queue(ConfigUtil.getProperty("mq.queue.brandCarQueue")+"_target", true, false, false) ;
        } 
        
        @Bean
        DirectExchange brandCarDirectExchange() {
            return new DirectExchange(ConfigUtil.getProperty("mq.exchange.brandCarDirectExchange"), true, false);
        }
        
        //绑定延时队列的key
        @Bean
        Binding brandCarKey(@Qualifier("brandCarQueue") Queue brandCarQueue,@Qualifier("brandCarDirectExchange") DirectExchange brandCarDirectExchange) {
            return BindingBuilder.bind(brandCarQueue).to(brandCarDirectExchange).with(ConfigUtil.getProperty("mq.key.brandCarKey"));
        }
        
       //绑定延时队列的死信队列的key
        @Bean
        Binding brandCarKeyTarget(@Qualifier("brandCarQueueTarget") Queue brandCarQueueTarget,@Qualifier("brandCarDirectExchange") DirectExchange brandCarDirectExchange) {
            return BindingBuilder.bind(brandCarQueueTarget).to(brandCarDirectExchange).with(ConfigUtil.getProperty("mq.key.brandCarKey")+"_target");
        }
     
     //监听延时队列的死信队列达到延时的效果
    //@RabbitListener(queues="${mq.queue.brandCarQueue}"+"_target")
    public void processMessage(Message message) {
        String json = new String(message.getBody()) ;
        logger.info("brandCarQueue=收到mq信息:"+json);
        MqMsg msg = JSON.parseObject(json, MqMsg.class) ;
        if (MqCodeConstant.CAR_BRAND_INFO_PULL.equals(msg.getCode())) {
            brandCarService.process(new BaseBean());
        }      
    }

    /**
     * 延时发送
     * @param orderNos  订单号,多个以逗号分开
     */
    public static void send() {
        logger.info("---brandCarQueue---604800000--------");
        MessageProperties messageProperties = new MessageProperties() ;
        messageProperties.setExpiration("604800000");
        //messageProperties.setExpiration("60000");
        MqMsg msg = new MqMsg() ;
        msg.setCode(MqCodeConstant.CAR_BRAND_INFO_PULL);
        Message message = new Message(JSON.toJSONString(msg).getBytes(), messageProperties) ;
        localAmqpTemplate.convertAndSend(ConfigUtil.getProperty("mq.exchange.brandCarDirectExchange"),ConfigUtil.getProperty("mq.key.brandCarKey"),message);
    }

    
    /**
     * 实时发送
     * @param orderNos  订单号,多个以逗号分开
     */
    public static void sendTarget() {
        logger.info("---brandCarQueue---sendTarget--------");
        MqMsg msg = new MqMsg() ;
        msg.setCode(MqCodeConstant.CAR_BRAND_INFO_PULL);
        localAmqpTemplate.convertAndSend(ConfigUtil.getProperty("mq.exchange.brandCarDirectExchange"),ConfigUtil.getProperty("mq.key.brandCarKey")+"_target",JSON.toJSONString(msg).getBytes());
    }
    
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值