简体   繁体   English

从 Spring AMQP Rabbit Consumer 捕获异常

[英]Catching exceptions from Spring AMQP Rabbit Consumer

I' m trying to create a RPC service using this tutorial .我正在尝试使用本教程创建 RPC 服务。

I would like to catch exceptions.我想捕捉异常。 Currently when doJob is throws an exception the producer gets timeout exception and the exception isn't logged on console on server.当前,当doJob引发异常时,生产者会收到超时异常,并且该异常未记录在服务器上的控制台上。

I tried with container.setErrorHandler , but no result.我尝试使用container.setErrorHandler ,但没有结果。 How can I catch exceptions both in producer and consumer?如何在生产者和消费者中捕获异常?

I mean我是说

public String doJob(int clientId) {
    String uuid = UUID.randomUUID().toString();
    log.info("UUID {} generated on node {} upon request from client {}", uuid, nodeId, clientId);
    return uuid;
}

can return Exceptions.可以返回异常。 How can I catch them in this part:我怎样才能在这部分抓住它们:

        String payload = proxy.doJob(clientId);
        log.info("Client {} received payload: {}", clientId, payload);
        Thread.sleep(2000);

You're using a request/response pattern with asynchronous messaging.您正在使用带有异步消息传递的请求/响应模式。 The producer and consumer are therefore logically separated.因此,生产者和消费者在逻辑上是分开的。 This separation is explicit because it has numerous benefits.这种分离是明确的,因为它有很多好处。 It is a fundamental tenet of asynchronous messaging.这是异步消息传递的基本原则。

Exceptions thrown from the consumer do not propagate back through the message broker to the producer who sent the message.消费者抛出的异常不会通过消息代理传播回发送消息的生产者。 That would defeat the logical separation of clients.这将破坏客户的逻辑分离。

The timeout exception on the producer indicates that something went wrong with the consumer which prevented it from returning a response.生产者的超时异常表明消费者出现问题,导致其无法返回响应。 Due to the logical separation of producers and consumers the producer will never know why the timeout exception happened.由于生产者和消费者的逻辑分离,生产者永远不会知道为什么会发生超时异常。 Again, this is by design.同样,这是设计使然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM