|
@@ -0,0 +1,72 @@
|
|
|
|
|
+package com.usoftchina.saas.transfers.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.usoftchina.saas.transfers.po.MessageInfo;
|
|
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
|
|
+import org.springframework.amqp.rabbit.support.CorrelationData;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author: guq
|
|
|
|
|
+ * @create: 2019-01-06 22:04
|
|
|
|
|
+ **/
|
|
|
|
|
+@Service
|
|
|
|
|
+public class SendService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RabbitTemplate rabbitTemplate;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //发送消息方法调用: 构建自定义对象消息
|
|
|
|
|
+ public void sendMessage(MessageInfo info) {
|
|
|
|
|
+ // 通过实现 ConfirmCallback 接口,消息发送到 Broker 后触发回调,确认消息是否到达 Broker 服务器,也就是只确认是否正确到达 Exchange 中
|
|
|
|
|
+ rabbitTemplate.setConfirmCallback(confirmCallback);
|
|
|
|
|
+ //消息唯一ID
|
|
|
|
|
+ CorrelationData correlationData = new CorrelationData(info.getMsgId());
|
|
|
|
|
+ rabbitTemplate.convertAndSend("test", "abc.eqw", info, correlationData);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //发送消息方法调用: 构建自定义对象消息
|
|
|
|
|
+ public void sendDelayMessage(MessageInfo info) {
|
|
|
|
|
+ // 通过实现 ConfirmCallback 接口,消息发送到 Broker 后触发回调,确认消息是否到达 Broker 服务器,也就是只确认是否正确到达 Exchange 中
|
|
|
|
|
+ rabbitTemplate.setConfirmCallback(delayConfirmCallback);
|
|
|
|
|
+ //消息唯一ID
|
|
|
|
|
+ CorrelationData correlationData = new CorrelationData(info.getMsgId());
|
|
|
|
|
+ rabbitTemplate.convertAndSend("test", "abc.eqw", info, correlationData);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //回调函数: confirm确认
|
|
|
|
|
+ final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void confirm(CorrelationData correlationData, boolean confirm, String cause) {
|
|
|
|
|
+ System.err.println("correlationData: " + correlationData);
|
|
|
|
|
+ String messageId = correlationData.getId();
|
|
|
|
|
+ if (confirm) {
|
|
|
|
|
+ System.out.println("发射成功");
|
|
|
|
|
+ //如果confirm返回成功 则进行更新
|
|
|
|
|
+ // brokerMessageLogMapper.changeBrokerMessageLogStatus(messageId, Constants.ORDER_SEND_SUCCESS, new Date());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //失败则进行具体的后续操作:重试 或者补偿等手段
|
|
|
|
|
+ System.err.println("发送失败,原因:" + cause);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ //回调函数: confirm确认
|
|
|
|
|
+ final RabbitTemplate.ConfirmCallback delayConfirmCallback = new RabbitTemplate.ConfirmCallback() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void confirm(CorrelationData correlationData, boolean confirm, String cause) {
|
|
|
|
|
+ System.err.println("correlationData: " + correlationData);
|
|
|
|
|
+ String messageId = correlationData.getId();
|
|
|
|
|
+ if (confirm) {
|
|
|
|
|
+ System.out.println("发射成功");
|
|
|
|
|
+ //如果confirm返回成功 则进行更新
|
|
|
|
|
+ // brokerMessageLogMapper.changeBrokerMessageLogStatus(messageId, Constants.ORDER_SEND_SUCCESS, new Date());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //失败则进行具体的后续操作:重试 或者补偿等手段
|
|
|
|
|
+ System.err.println("发送失败,原因:" + cause);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+}
|