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());
}
}