|
|
|
@@ -8,11 +8,16 @@ import com.iformall.service.WxMsgRecordService; |
|
|
|
import com.iformall.utils.JsonUtil; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.Marker; |
|
|
|
import org.springframework.amqp.core.AmqpTemplate; |
|
|
|
import org.springframework.amqp.core.Message; |
|
|
|
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
|
|
|
import org.springframework.amqp.rabbit.support.CorrelationData; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Profile; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -22,42 +27,52 @@ import java.util.UUID; |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
@Profile(MQConfig.Impl.RABBIT_MQ) |
|
|
|
public class RabbitMqMessageProducer implements MqBaseProducer { |
|
|
|
public class RabbitMqMessageProducer implements MqBaseProducer , RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback { |
|
|
|
private final Logger log = LoggerFactory.getLogger(this.getClass()); |
|
|
|
@Autowired |
|
|
|
private AmqpTemplate rabbitTemplate; |
|
|
|
private RabbitTemplate rabbitTemplate; |
|
|
|
@Autowired |
|
|
|
private WxMsgRecordMapper wxMsgRecordMapper; |
|
|
|
@Autowired |
|
|
|
private WxMsgRecordService wxMsgRecordService; |
|
|
|
// |
|
|
|
// @Override |
|
|
|
// public void confirm(CorrelationData correlationData, boolean b, String s) { |
|
|
|
// log.info("------correlationData"+correlationData.toString()); |
|
|
|
// System.out.println(); |
|
|
|
// if(b){ |
|
|
|
// System.out.println(); |
|
|
|
// }else{ |
|
|
|
// |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
rabbitTemplate.setConfirmCallback(this); |
|
|
|
rabbitTemplate.setReturnCallback(this); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void confirm(CorrelationData correlationData, boolean ack, String cause) { |
|
|
|
log.info("confirm:: correlationData {},ack {}, cause {}", correlationData,ack,cause); |
|
|
|
System.out.println(); |
|
|
|
if(ack){ |
|
|
|
System.out.println(); |
|
|
|
}else{ |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { |
|
|
|
log.info("returnedMessage::message {} 消息发送失败,应答码:{}, 应答描述:{} ,exchange: {} ,routingKey :{}", new String(message.getBody()), replyCode, replyText, exchange, routingKey); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void sendMessage(BaseMsg data, String topic, String tags, String keys) { |
|
|
|
String tag = UUID.randomUUID().toString(); |
|
|
|
try { |
|
|
|
data.setUuid(UUID.randomUUID().toString()); |
|
|
|
data.setUuid(tag); |
|
|
|
wxMsgRecordService.save(data); |
|
|
|
|
|
|
|
//CorrelationData correlationData = new CorrelationData("16746324"); |
|
|
|
rabbitTemplate.convertAndSend(topic, JsonUtil.obj2Json(data)); |
|
|
|
|
|
|
|
CorrelationData correlationData = new CorrelationData(tag); |
|
|
|
rabbitTemplate.convertAndSend(topic, (Object) JsonUtil.obj2Json(data),correlationData); |
|
|
|
// data.setMsgStatus(EnumMsgRecordStatus.SEND_SUCC.getCode()); |
|
|
|
// wxMsgRecordMapper.update(data); |
|
|
|
log.info(data.getUuid()+"发送mq成功。"); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
log.error("RabbitMqMessageProducer: Send Message Error ", e); |
|
|
|
log.error(tag + " RabbitMqMessageProducer: Send Message Error", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |