Spring集成-动态MailReceiver配置

时间:2019-04-20 12:58:47

标签: spring spring-integration

无论如何,我spring-integration都是陌生人,我一直在使用它来接收邮件并对其进行详细说明。

我使用了这个spring配置类:

@Configuration
@EnableIntegration
@PropertySource(value = { "classpath:configuration.properties" }, encoding = "UTF-8", ignoreResourceNotFound = false)
public class MailReceiverConfiguration {

    private static final Log logger = LogFactory.getLog(MailReceiverConfiguration.class);

    @Autowired
    private EmailTransformerService emailTransformerService;

    // Configurazione AE
    @Bean
    public MessageChannel inboundChannelAE() {
        return new DirectChannel();
    }

    @Bean(name= {"aeProps"})
    public Properties aeProps() {
        Properties javaMailPropertiesAE = new Properties();
        javaMailPropertiesAE.put("mail.store.protocol", "imap");
        javaMailPropertiesAE.put("mail.debug", Boolean.TRUE);
        javaMailPropertiesAE.put("mail.auth.debug", Boolean.TRUE);
        javaMailPropertiesAE.put("mail.smtp.socketFactory.fallback", "false");
        javaMailPropertiesAE.put("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        return javaMailPropertiesAE;
    }
    @Bean(name="mailReceiverAE")
    public MailReceiver mailReceiverAE(@Autowired MailConfigurationBean mcb, @Autowired @Qualifier("aeProps") Properties javaMailPropertiesAE) throws Exception {

        return ConfigurationUtil.getMailReceiver("imap://USERNAME:PASSWORD@MAILSERVER:PORT/INBOX", new BigDecimal(2), javaMailPropertiesAE);
    }

    @Bean
    @InboundChannelAdapter( autoStartup = "true", 
                            channel = "inboundChannelAE", 
                            poller = {@Poller(fixedRate = "${fixed.rate.ae}", 
                            maxMessagesPerPoll = "${max.messages.per.poll.ae}") })
    public MailReceivingMessageSource pollForEmailAE(@Autowired MailReceiver mailReceiverAE) {
        MailReceivingMessageSource mrms = new MailReceivingMessageSource(mailReceiverAE);
        return mrms;
    }

    @Transformer(inputChannel = "inboundChannelAE", outputChannel = "transformerChannelAE")
    public MessageBean transformitAE( MimeMessage mailMessage ) throws Exception {
        // amministratore email inbox
        MessageBean messageBean = emailTransformerService.transformit(mailMessage);
        return messageBean;
    }

    @Splitter(inputChannel = "transformerChannelAE", outputChannel = "nullChannel")
    public List<Message<?>> splitIntoMessagesAE(final MessageBean mb) {

        final List<Message<?>> messages = new ArrayList<Message<?>>();

        for (EmailFragment emailFragment : mb.getEmailFragments()) {
            Message<?> message = MessageBuilder.withPayload(emailFragment.getData())
                    .setHeader(FileHeaders.FILENAME, emailFragment.getFilename())
                    .setHeader("directory", emailFragment.getDirectory()).build();
            messages.add(message);
        }

        return messages;
    }
}

到目前为止很好。...我启动了微服务,并且该组件正在指定的邮件服务器上侦听并下载邮件。

现在我有此要求:邮件服务器配置(我的意思是字符串"imap://USERNAME:PASSWORD@MAILSERVER:PORT/INBOX")必须从数据库中获取,并且可以进行配置。在任何时候,系统管理员都可以更改它,并且邮件接收者必须使用新配置。

据我了解,当存在新配置时,我应该创建一个MailReceiver的新实例,并在InboundChannelAdapter

中使用它。

是否有最佳实践来做到这一点?我找到了以下解决方案:ImapMailReceiver NO STORE attempt on READ-ONLY folder (Failure) [THROTTLED];

在此解决方案中,如果在Configuration类中定义了ThreadPoolTask​​Scheduler,则可以将其注入;我也可以注入DirectChannel,但是每次我应该创建一个新的MailReceiver和一个新的ImapIdleChannelAdapter时,都不要考虑

时收到的WARN消息。
ImapIdleChannelAdapter starts:
java.lang.RuntimeException: No beanfactory at org.springframework.integration.expression.ExpressionUtils.createStandardEvaluationContext(ExpressionUtils.java:79) at org.springframework.integration.mail.AbstractMailReceiver.onInit(AbstractMailReceiver.java:403)

是否有更好的方法来满足我的情况?

谢谢 安杰洛

1 个答案:

答案 0 :(得分:0)

做到这一点的最佳方法是使用Java DSL和动态流注册。

Documentation here

这样,每次配置更改时,您都可以注销旧流程并注册新流程。

它将自动处理注入依赖项,例如bean工厂。