当前位置: 代码迷 >> 综合 >> JMS(RabbitMQ)的进阶二:发送端(producer)和接收端(consumer)进行应答
  详细解决方案

JMS(RabbitMQ)的进阶二:发送端(producer)和接收端(consumer)进行应答

热度:1   发布时间:2023-12-15 19:14:47.0

??一些时效性强的需求,需要等待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