我不确定我对 errorHandler 和 returnExceptions 的理解是否正确。
但这是我的目标:我从 App_A 发送了一条消息,使用 @RabbitListener 在 App_B 中接收消息。
我假设如果APP_B在处理消息过程中出现业务异常,通过在@RabbitListener上以正确的方式设置errorHandler和returnExceptions可以让异常返回到App_A。
我理解正确吗?
如果我是对的,如何以正确的方式使用它?
使用我的代码,我在 APP_A 中什么也得不到。
这是我在 APP_B 中的代码
错误处理程序:
@Component(value = "errorHandler")
public class ErrorHandler implements RabbitListenerErrorHandler {
@Override
public Object handleError(Message arg0, org.springframework.messaging.Message<?> arg1,
ListenerExecutionFailedException arg2) throws ListenerExecutionFailedException {
throw new ListenerExecutionFailedException("msg", arg2, null);
}
}
Run Code Online (Sandbox Code Playgroud)
兔子监听器:
@RabbitListener(
bindings = @QueueBinding(
value = @Queue(value = "MRO.updateBaseInfo.queue", durable = "true"),
exchange = @Exchange(name = "MRO_Exchange", type = ExchangeTypes.DIRECT, durable = "true"),
key = "baseInfoUpdate"
),
// errorHandler = "errorHandler",
returnExceptions = "true"
)
public void receiveLocationChangeMessage(String message){
BaseUpdateMessage newBaseInfo = JSON.parseObject(message, BaseUpdateMessage.class);
dao.upDateBaseInfo(newBaseInfo);
}
Run Code Online (Sandbox Code Playgroud)
和APP_A中的代码
@Component
public class MessageSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void editBaseInfo(BaseUpdateMessage message)throws Exception {
//and i am not sure set RemoteInvocationAwareMessageConverterAdapter in this way is right
rabbitTemplate.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
rabbitTemplate.convertAndSend("MRO_Exchange", "baseInfoUpdate", JSON.toJSONString(message));
}
}
Run Code Online (Sandbox Code Playgroud)
我对三点很困惑:
1)我必须同时使用errorHandler和returnExceptions吗?我认为 errorHandler 类似于后处理器,可以让我自定义异常。如果我不需要自定义异常,我可以只设置 returnExceptions 而不使用 errorHandler 吗?
2)用@RabbitListener注释的方法应该返回一些东西还是void就可以了?
3)在发送方(我的情况是APP_A),是否有任何特定的配置来捕获异常?
我的工作环境:
春季启动2.1.0
docker 上的rabbitMQ服务器3.7.8
1)不,你不需要错误处理程序,除非你想增强异常。
2)如果该方法返回void;发送者最终将等待超时以获取永远不会到达的回复,以防万一可能引发异常;这可能不是对资源的良好利用。最好总是发送回复,以释放发布商方面的时间。
3)只是RemoteInvocationAwareMessageConverterAdapter.
这是一个例子:
@SpringBootApplication
public class So53846303Application {
public static void main(String[] args) {
SpringApplication.run(So53846303Application.class, args);
}
@RabbitListener(queues = "foo", returnExceptions = "true")
public String listen(String in) {
throw new RuntimeException("foo");
}
@Bean
public ApplicationRunner runner(RabbitTemplate template) {
template.setMessageConverter(new RemoteInvocationAwareMessageConverterAdapter());
return args -> {
try {
template.convertSendAndReceive("foo", "bar");
}
catch (Exception e) {
e.printStackTrace();
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
和
org.springframework.amqp.AmqpRemoteException: java.lang.RuntimeException: foo
at org.springframework.amqp.support.converter.RemoteInvocationAwareMessageConverterAdapter.fromMessage(RemoteInvocationAwareMessageConverterAdapter.java:74)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:1500)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:1433)
at org.springframework.amqp.rabbit.core.RabbitTemplate.convertSendAndReceive(RabbitTemplate.java:1425)
at com.example.So53846303Application.lambda$0(So53846303Application.java:28)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:794)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.example.So53846303Application.main(So53846303Application.java:15)
Caused by: java.lang.RuntimeException: foo
at com.example.So53846303Application.listen(So53846303Application.java:20)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,有一个本地异常org.springframework.amqp.AmqpRemoteException,其原因是远程服务器上抛出的实际异常。
| 归档时间: |
|
| 查看次数: |
4676 次 |
| 最近记录: |