使用spring集成入站通道适配器时重新下载消息

时间:2017-10-27 10:04:13

标签: java spring spring-boot spring-integration javamail

我正在阅读邮件时使用弹出邮件入站通道适配器阅读邮件我正在相应频道的服务激活器中执行一些数据库操作。我的要求是,如果任何db操作失败,适配器应该再次读取相同的消息。

邮件配置:

@Bean
public DirectChannel inputChannel() {
    return new DirectChannel();
}

@Bean
public IntegrationFlow pop3MailFlow() {



    String url = "[url]";
    return IntegrationFlows
            .from(Mail.pop3InboundAdapter(url)
                    .javaMailProperties(p -> p.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory")),e -> e.autoStartup(true)
                    .poller(Pollers.fixedDelay(2000).transactionSynchronizationFactory(transactionSynchronizationFactory())))
            .channel(inputChannel())
            .handle(inboundEmailProcessor(),"messageProcess")
            .get();
}

@Bean
public InboundEmailProcessor inboundEmailProcessor() {
    return new InboundEmailProcessor();
}

@Bean
public TransactionSynchronizationFactory transactionSynchronizationFactory() {
    TransactionSynchronizationFactory synchronizationFactory = new DefaultTransactionSynchronizationFactory(expressionEvaluatingTransactionSynchronizationProcessor());
    return synchronizationFactory;
}

@Bean
public ExpressionEvaluatingTransactionSynchronizationProcessor expressionEvaluatingTransactionSynchronizationProcessor() {
    ExpressionEvaluatingTransactionSynchronizationProcessor processor = new ExpressionEvaluatingTransactionSynchronizationProcessor();
    ExpressionParser parser = new SpelExpressionParser();
    processor.setAfterRollbackExpression(parser.parseExpression("new com.muraai.ex.config.Exp().process(payload)"));
    return processor;

}

public class InboundEmailProcessor {


    @Autowired
    AttachmentsRepository attachmentsRepository;

    @Transactional(rollbackFor = Exception.class)
    public void messageProcess() {
        // some db operations
        // if it fails the same message should be read again
    }
}

我认为这样可行,但不起作用。有没有办法达到我的要求

public class Exp {

    public void process(MimeMessage message) throws MessagingException {
        message.setFlag(Flags.Flag.SEEN, false);
    }
}

1 个答案:

答案 0 :(得分:0)

你需要IMAP;使用POP3,服务器始终将它们标记为已读。

您可以向轮询器的建议链添加spring-retry拦截器建议和/或将失败的消息发送到错误通道。

可以针对重试次数,退出政策等配置重试建议。