Как добавить Spring Boot HealthIndicator в приемник Imap IntegrationFlow

Как интегрировать Spring Boot HealthIndicator с электронной почтой для опроса IntegrationFlow с imap?

Я могу получить исключения из IntegrationFlow через errorChannel, но как мне очистить исключение после того, как IntegrationFlow снова начнет работать, например, после сбоя сети.

@SpringBootApplication
public class MyApplication {

    @Bean
    IntegrationFlow mailFlow() {
        return IntegrationFlows
                .from(Mail.imapInboundAdapter(receiver()).get(),
                        e -> e.autoStartup(true)
                                .poller(Pollers.fixedRate(5000)))
                .channel(mailChannel()).get();
    }
    
    @Bean
    public ImapMailReceiver receiver() {
        String mailServerPath = format("imaps://%s:%s@%s/INBOX", mailUser,
                encode(mailPassword), mailServer);
        ImapMailReceiver result = new ImapMailReceiver(mailServerPath);
        return result;
    }

    @Bean
    DirectChannel mailChannel() {
        return new DirectChannel();
    }

    @Autowired
    @Qualifier("errorChannel")
    private PublishSubscribeChannel errorChannel;

    @Bean
    public IntegrationFlow errorHandlingFlow() {
        return IntegrationFlows.from(errorChannel).handle(message -> {
            MessagingException ex = (MessagingException) message.getPayload();
            log.error("", ex);
        }).get();
    }

    @Bean
    HealthIndicator mailReceiverHealthIndicator() {
        return () -> {
            /*
             * How to error check the imap polling ???
             */
            return Health.up().build();
        };
    }

}

person cmadsen    schedule 12.03.2021    source источник


Ответы (1)


Я бы выбрал bean AtomicReference<Exception> и установил его значение в этом errorHandlingFlow. HealthIndicator impl будет обращаться к этому AtomicReference в down(), когда он имеет значение.

PollerSpec для Mail.imapInboundAdapter() можно настроить с ReceiveMessageAdvice:

/**
 * Specify AOP {@link Advice}s for the {@code pollingTask}.
 * @param advice the {@link Advice}s to use.
 * @return the spec.
 */
public PollerSpec advice(Advice... advice) {

Его afterReceive() impl может просто очистить этот AtomicReference, так что ваш HealthIndicator вернет up().

Дело в том, что этот afterReceive() вызывается только тогда, когда invocation.proceed() не выходит из строя с исключением. и он вызывается независимо, есть ли новые сообщения для обработки или нет.

person Artem Bilan    schedule 12.03.2021