package com.cetcnav.config.rabbitmq;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectRabbitConfig {
@Bean
public Queue QXBDWarningMsgQueue() {
return new Queue("QXBD_WARNING_MSG_QUEUE", true);
}
@Bean
DirectExchange QXBDWarningMsgExchange() {
return new DirectExchange("QXBD_WARNING_MSG_QUEUE_EXCHANGE", true, false);
}
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(QXBDWarningMsgQueue()).to(QXBDWarningMsgExchange()).with("QXBD_WARNING_MSG_QUEUE_ROUTING");
}
}
package com.cetcnav.rabbitmqListener;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cetcnav.cache.SkyMirrorDataCache;
import com.cetcnav.domain.DI.DIData;
import com.cetcnav.domain.skyMirrorData.SkyMirrorData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.text.ParseException;
@Component
public class DirectReceiver {
private static Logger log = LoggerFactory.getLogger(DirectReceiver.class);
@Value("${skymirror.pushUrl}")
private String PUSH_URL;
@Value("${skymirror.pushBatchUrl}")
private String PUSH_BATCH_URL;
@RabbitListener(queues = "QXBD_WARNING_MSG_QUEUE")
public void receiveMessage(String message) throws ParseException {
log.info("queue.consumer 收到1条message:{}", message);
try {
dealMessage(message);
} catch (Exception e) {
log.error(ExceptionUtil.getMessage(e), e);
}
}
private void dealMessage(String message) {
SkyMirrorData skyMirrorData = JSON.parseObject(message, SkyMirrorData.class);
SkyMirrorDataCache.addSkyMirrorDataCache(skyMirrorData);
}
}
package com.cetcnav.rabbitmqService;
import com.alibaba.fastjson.JSONObject;
import com.cetcnav.domain.skyMirrorData.SkyMirrorData;
import com.cetcnav.domain.skyMirrorData.SkyMirrorData_old;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitmqService {
@Autowired
private RabbitTemplate rabbitTemplate;
public boolean sendDirectMsg(SkyMirrorData skyMirrorData) {
rabbitTemplate.convertAndSend("QXBD_WARNING_MSG_QUEUE_EXCHANGE", "QXBD_WARNING_MSG_QUEUE_ROUTING",
JSONObject.toJSONString(skyMirrorData));
return true;
}
}