我对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类中定义它,我可以注入ThreadPoolTaskScheduler;我也可以注入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)
有没有更好的方法来满足我的情况?
谢谢Angelo