??一些时效性强的需求,需要等待consumer完成业务后返回结果,则需要进行应答处理。
发送:
//发送消息返回消费端的回应@Overridepublic <T> T sendSMSandReceive(SMSBean sms) throws Exception{
Message message=ackRabbitTemplate.getMessageConverter().toMessage(sms,null);CorrelationData correlationData= new CorrelationData(message.getMessageProperties().getMessageId());smsConfig.saveSMS(sms,correlationData.getId());return (T)(ackRabbitTemplate.convertSendAndReceive(SMSConstants.SMS_EXCHANGE_DIRECT,SMSConstants.SMS_KEY_DIRECT4,message,correlationData));}
接收:
@RabbitListener(bindings = @QueueBinding(value=@Queue(SMSConstants.SMS_QUEUE_DIRECT4),exchange = @Exchange(value = SMSConstants.SMS_EXCHANGE_DIRECT,type = ExchangeTypes.DIRECT),key = SMSConstants.SMS_KEY_DIRECT))@RabbitHandler//应答protected JSONData<Object> onReceived4(Message message, SMSBean sms, Channel channel) throws IOException {
log.info("=========onReceived4已接收,返回数据===========");SMSUtil.sleep(5000);JSONData<Object> ret=JSONData.JSONDataFromResult(String.format("%s,消费端已接收%s",new Date().toString(),sms));return ret;}
代码链接,觉得有用的话请点个Star: https://gitee.com/tigera15/jms-samples/tree/master