我正在尝试在春季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中引发了一些异常。我的RetryOperationsInterceptor仅在onMessage()方法中引发异常时才重试消息。添加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()方法中重试消息。
[如果您是要在MessagePostProcessor
容器属性中提供afterReceiveMessagePostProcessors
,则将这些后处理器称为建议链范围之外的对象(重试拦截器)。
您不能“重试”此类后处理器抛出的异常。
重试拦截器仅适用于侦听器本身。