gpt4 book ai didi

Spring AMQP 恢复器不适用于 SimpleMessageListenerContainer

转载 作者:行者123 更新时间:2023-12-04 10:42:33 27 4
gpt4 key购买 nike

我正在尝试在 spring amqp 中实现恢复。我已经使用下面的代码来实现相同的

RetryOperationsInterceptor retryInterceptorBuilder =RetryInterceptorBuilder.stateless()
.maxAttempts(5)
.recoverer(new CustomRejectAndDontRequeueRecoverer())
.build();

container.setAdviceChain(new RetryOperationsInterceptor[]{retryInterceptorBuilder});

上面的容器是 SimpleMessageListenerContainer的实例.现在我在我的一个接收器中扔 ClassCastException .
public class CustomRejectAndDontRequeueRecoverer implements MessageRecoverer {


private static Logger logger = //created some logger instance.

//Overriding the method to do custom task when the retries are exhausted, like insert in database.
@Override
public void recover(Message message, Throwable cause) {
logger.error("The recovery method is called","","");
throw new RuntimeException(cause);
}

}

请指出我正确的方向。

更新 :

在我的 CustomMessagingPostProcessor 中抛出了一些异常。如果在 onMessage() 方法中抛出异常,我的 RetryOperationsInterceptor 只会重试消息。添加 CustomMessagingPostProcessor 的定义:-

public class MTMessagingPostProcessor implements MessagePostProcessor{



/**
* {@inheritDoc}
*/
@Override
public Message postProcessMessage(Message message) {
logger.xdebug("Inside MTMessagingPostProcessor",

//Throwing exception to show that some exception can be thrown in original code and I want to requeue messages to come here for n number of times.
throw new RuntimeException("TEST");
//return message;
}


public void setTenantProvider(TenantProvider tenantProvider) {
this.tenantProvider = tenantProvider;
}


}

如果在 MTMessagingPostProcessor 中抛出异常,我想将消息重新排队 n 次,这不是由拦截器实现的,因为它在监听器的 onMessage() 方法中重试消息。

最佳答案

如果您的意思是提供 MessagePostProcessorafterReceiveMessagePostProcessors容器属性,这些后处理器在建议链的范围之外被调用(重试拦截器)。

您不能“重试”此类后处理器引发的异常。

重试拦截器仅适用于监听器本身。

关于Spring AMQP 恢复器不适用于 SimpleMessageListenerContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59857407/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com