RabbitMq异步调用必须等到订户返回响应

时间:2018-03-30 03:30:00

标签: java rabbitmq spring-rabbitmq

我正在使用Rabbitmq在Publisher和订阅者之间进行通信。我的发布者正在将消息推送到Queue,订阅者正在执行某些过程并将响应返回给发布者。我的问题是订阅者需要一些时间来执行进程,但发布者不会等到它收到订阅者的响应。以下是我的代码。 出版商:

public void publishToDirectExchangeRPCStyle(String msg) {
        requestRoutingKey = Configuratins.requestRoutingKey;
        /*Integer integer = SECURE_RANDOM.nextInt();*/
        SampleRequestMessage sampleRequestMessage = new SampleRequestMessage(msg);
        System.out.println("Sending out message on direct directExchange:" + sampleRequestMessage);

        AsyncRabbitTemplate.RabbitConverterFuture<SampleResponseMessage> sampleResponseMessageRabbitConverterFuture = asyncRabbitTemplate
                        .convertSendAndReceive("spring-boot-rabbitmq-examples.async_rpc", "rpc_request", sampleRequestMessage);


        sampleResponseMessageRabbitConverterFuture.addCallback(
                        sampleResponseMessage ->{
                                        System.out.println("Response for request message:" + sampleRequestMessage + " is:" + sampleResponseMessage);
                                        try {
                                            mailClient.sendMail("Script Sucess",sampleResponseMessage.getMessage(), "");
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                        }
                        , failure ->{
                                        System.out.println(failure.getMessage()+"Failure section");

                                       /* try {
                                            mailClient.sendMail("Script Failure","1", "");
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }*/
                        }
        );



    }


}

订户:

@EnableRabbit
public class Subscriber {

    @Autowired
    public Processor processor;
    @RabbitHandler
    @RabbitListener(containerFactory = "simpleMessageListenerContainerFactory", queues = "requestQueueRPC")
    public SampleResponseMessage subscribeToRequestQueue(@Payload SampleRequestMessage sampleRequestMessage, Message message) throws InterruptedException {
        System.out.println("Received message :" + message);
        int exitvalue=1;
        try {
            exitvalue=processor.executeProcess("FL");
            System.out.println("Exit value is:" +exitvalue);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        /*return new SampleResponseMessage(sampleRequestMessage.getMessage());*/
        return new SampleResponseMessage(new Integer(exitvalue).toString());
    }
}

发布商 sampleResponseMessageRabbitConverterFuture.addCallback(                         sampleResponseMessage - &gt; {不等待它收到回复。我们有什么方法可以让它等到它得到回复之后呢?

0 个答案:

没有答案