0

我想在 RabbitMQ 侦听器重试结束后触发一封电子邮件,如果处理过程失败仍然如此。

重试逻辑正在使用以下代码。但是一旦最大重试尝试结束,如何触发功能(电子邮件触发)。

@Bean
public SimpleMessageListenerContainer container() {
    SimpleMessageListenerContainer container =
            new SimpleMessageListenerContainer(connectionFactory());
    container.setQueues(myQueue());
    container.setDefaultRequeueRejected(false);
    Advice[] adviceArray = new Advice[]{interceptor()};
    container.setAdviceChain(adviceArray);
    return container;
}

@Bean
public IntegrationFlow inboundFlow() {
    return IntegrationFlows.from(
            Amqp.inboundAdapter(container()))
            .log()
            .handle(listenerBeanName, listenerMethodName)
            .get();
}

@Bean
RetryOperationsInterceptor interceptor() {
    return RetryInterceptorBuilder.stateless()
            .maxAttempts(retryMaxAttempts)
            .backOffOptions(initialInterval, multiplier, maxInterval)
             //.recoverer(new RejectAndDontRequeueRecoverer()) 
            .recoverer(new CustomRejectAndRecoverer())
            .build();
}

添加CustomeRecover的代码

@Service
public class CustomRejectAndRecoverer implements MessageRecoverer {
@Autowired
private EmailGateway emailgateway;

@Override
public void recover(Message message, Throwable cause) {
    // INSERT CODE HERE.... HOW TO CALL GATEWAY
    // emailgateway.sendMail(cause);
    throw new ListenerExecutionFailedException("Retry Policy Exhausted",
            new AmqpRejectAndDontRequeueException(cause), message);
} }
4

1 回答 1

1

这正是 a .recoverer()in thatRetryInterceptorBuilder的用途。

您现在在那里使用 a ,但没有任何人阻止您通过委托来RejectAndDontRequeueRecoverer实现您自己的,并使用发送电子邮件逻辑向某些人发送消息。MessageRecovererRejectAndDontRequeueRecovererMessageChannel

于 2018-06-27T18:24:52.553 回答