Spring Integration - Dynamic MailReceiver configuration - spring
I'm pretty new to spring-integration anyway I'm using it in order to receive mails and elaborate them.
I used this spring configuration class:
#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;
}
}
So far so good.... I start my micro-service and there is this component listening on the specified mail server and mails are downloaded.
Now I have this requirement: mail server configuration (I mean the string "imap://USERNAME:PASSWORD#MAILSERVER:PORT/INBOX") must be taken from a database and it can be configurable. In any time a system administrator can change it and the mail receiver must use the new configuration.
As far as I understood I should create a new instance of MailReceiver when a new configuration is present and use it in the InboundChannelAdapter
Is there any best practice in order to do it? I found this solution: ImapMailReceiver NO STORE attempt on READ-ONLY folder (Failure) [THROTTLED];
In this solution I can inject the ThreadPoolTaskScheduler if I define it in my Configuration class; I can also inject the DirectChannel but every-time I should create a new MailReceiver and a new ImapIdleChannelAdapter without considering this WARN message I get when the
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)
Is there a better way to satisfy my scenario?
Thank you
Angelo
The best way to do this is to use the Java DSL and dynamic flow registration.
Documentation here.
That way, you can unregister the old flow and register a new one, each time the configuration changes.
It will automatically handle injecting dependencies such as the bean factory.
Related
Create multiple beans of SftpInboundFileSynchronizingMessageSource dynamically with InboundChannelAdapter
I am using spring inbound channel adapter to poll files from sftp server. Application needs to poll from multiple directories from single sftp server. Since Inbound channel adapter does not allow to poll multiple directories I tried creating multiple beans of same type with different values. Since number of directories can increase in future, I want to control it from application properties and want to register beans dynamically. My code - #Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { beanFactory.registerSingleton("sftpSessionFactory", sftpSessionFactory(host, port, user, password)); beanFactory.registerSingleton("sftpInboundFileSynchronizer", sftpInboundFileSynchronizer((SessionFactory) beanFactory.getBean("sftpSessionFactory"))); } public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory(String host, String port, String user, String password) { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost(host); factory.setPort(Integer.parseInt(port)); factory.setUser(user); factory.setPassword(password); factory.setAllowUnknownKeys(true); return new CachingSessionFactory<>(factory); } private SftpInboundFileSynchronizer sftpInboundFileSynchronizer(SessionFactory sessionFactory) { SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sessionFactory); fileSynchronizer.setDeleteRemoteFiles(true); fileSynchronizer.setPreserveTimestamp(true); fileSynchronizer.setRemoteDirectory("/mydir/subdir); fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.pdf")); return fileSynchronizer; } #Bean #InboundChannelAdapter(channel = "sftpChannel", poller = #Poller(fixedDelay = "2000")) public MessageSource<File> sftpMessageSource(String s) { SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource( (AbstractInboundFileSynchronizer<ChannelSftp.LsEntry>) applicationContext.getBean("sftpInboundFileSynchronizer")); source.setLocalDirectory(new File("/dir/subdir")); source.setAutoCreateLocalDirectory(true); source.setLocalFilter(new AcceptOnceFileListFilter<>()); source.setMaxFetchSize(Integer.parseInt(maxFetchSize)); source.setAutoCreateLocalDirectory(true); return source; } #Bean #ServiceActivator(inputChannel = "sftpChannel") public MessageHandler handler() { return message -> { LOGGER.info("Payload - {}", message.getPayload()); }; } This code works fine. But If I create sftpMessageSource dynamically, then #InboundChannelAdapter annotation won't work. Please suggest a way to dynamically create sftpMessageSource and handler beans also and add respective annotations. Update: Following Code Worked : #PostConstruct void init() { int index = 0; for (String directory : directories) { index++; int finalI = index; IntegrationFlow flow = IntegrationFlows .from(Sftp.inboundAdapter(sftpSessionFactory()) .preserveTimestamp(true) .remoteDirectory(directory) .autoCreateLocalDirectory(true) .localDirectory(new File("/" + directory)) .localFilter(new AcceptOnceFileListFilter<>()) .maxFetchSize(10) .filter(new SftpSimplePatternFileListFilter("*.pdf")) .deleteRemoteFiles(true), e -> e.id("sftpInboundAdapter" + finalI) .autoStartup(true) .poller(Pollers.fixedDelay(2000))) .handle(handler()) .get(); this.flowContext.registration(flow).register(); } } #Bean public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost(host); factory.setPort(Integer.parseInt(port)); factory.setUser(user); factory.setPassword(password); factory.setAllowUnknownKeys(true); return new CachingSessionFactory<>(factory); }
Annotations in Java are static. You can't add them at runtime for created objects. Plus the framework reads those annotation on application context startup. So, what you are looking for is just not possible with Java as language per se. You need consider to switch to Java DSL in Spring Integration to be able to use its "dynamic flows": https://docs.spring.io/spring-integration/docs/5.3.1.RELEASE/reference/html/dsl.html#java-dsl-runtime-flows. But, please, first of all study more what Java can do and what cannot.
S3PersistentAcceptOnceFileListFilter produces messages for the existing/synchronized files on application restart
has anyone come across that? basically I aim to process the file once even if the application is bounced #Bean public S3InboundFileSynchronizer s3InboundFileSynchronizer() throws Exception { S3InboundFileSynchronizer synchronizer = new S3InboundFileSynchronizer(amazonS3()); synchronizer.setDeleteRemoteFiles(false); synchronizer.setPreserveTimestamp(true); synchronizer.setRemoteDirectory(sourceBucket + "/dir/"); synchronizer.setFilter(new S3PersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "simpleMetadataStore")); return synchronizer; } private AmazonS3 amazonS3() throws Exception { return clientFactory.getClient(AmazonS3.class); } #Bean #InboundChannelAdapter(value = "s3FilesChannel", poller = #Poller(fixedDelay = "5000")) public S3InboundFileSynchronizingMessageSource s3InboundFileSynchronizingMessageSource() throws Exception { S3InboundFileSynchronizingMessageSource messageSource = new S3InboundFileSynchronizingMessageSource(s3InboundFileSynchronizer()); messageSource.setAutoCreateLocalDirectory(true); messageSource.setLocalDirectory(new File("c:/temp/")); messageSource.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), "fsSimpleMetadataStore")); return messageSource; }
Your problem that you use an in-memory SimpleMetadataStore, so after application restart you lose all the information stored there. Consider to use some persistent store implementation instead, e.g. for AWS we have a DynamoDbMetadataStore: https://github.com/spring-projects/spring-integration-aws#metadata-store-for-amazon-dynamodb
Spring Integration: how to access the returned values from last Subscriber
I'm trying to implement a SFTP File Upload of 2 Files which has to happen in a certain order - first a pdf file and after successfull upload of that an text file with meta information about the pdf. I followed the advice in this thread, but can't get it to work properly. My Spring Boot Configuration: #Bean public SessionFactory<LsEntry> sftpSessionFactory() { final DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); final Properties jschProps = new Properties(); jschProps.put("StrictHostKeyChecking", "no"); jschProps.put("PreferredAuthentications", "publickey,password"); factory.setSessionConfig(jschProps); factory.setHost(sftpHost); factory.setPort(sftpPort); factory.setUser(sftpUser); if (sftpPrivateKey != null) { factory.setPrivateKey(sftpPrivateKey); factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase); } else { factory.setPassword(sftpPasword); } factory.setAllowUnknownKeys(true); return new CachingSessionFactory<>(factory); } #Bean #BridgeTo public MessageChannel toSftpChannel() { return new PublishSubscribeChannel(); } #Bean #ServiceActivator(inputChannel = "toSftpChannel") #Order(0) public MessageHandler handler() { final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory()); handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory)); handler.setFileNameGenerator(message -> { if (message.getPayload() instanceof byte[]) { return (String) message.getHeaders().get("filename"); } else { throw new IllegalArgumentException("File expected as payload."); } }); return handler; } #ServiceActivator(inputChannel = "toSftpChannel") #Order(1) public String transferComplete(#Payload byte[] file, #Header("filename") String filename) { return "The SFTP transfer complete for file: " + filename; } #MessagingGateway public interface UploadGateway { #Gateway(requestChannel = "toSftpChannel") String upload(#Payload byte[] file, #Header("filename") String filename); } My Test Case: final String pdfStatus = uploadGateway.upload(content, documentName); log.info("Upload of {} completed, {}.", documentName, pdfStatus); From the return of the Gateway upload call i expect to get the String confirming the upload e.g. "The SFTP transfer complete for file:..." but I get the the returned content of the uploaded File in byte[]: Upload of 123456789.1.pdf completed, 37,80,68,70,45,49,46,54,13,37,-30,-29,-49,-45,13,10,50,55,53,32,48,32,111,98,106,13,60,60,47,76,105,110,101,97,114,105,122,101,100,32,49,47,76,32,50,53,52,55,49,48,47,79,32,50,55,55,47,69,32,49,49,49,55,55,55,47,78,32,49,47,84,32,50,53,52,51,53,57,47,72,32,91,32,49,49,57,55,32,53,51,55,93,62,62,13,101,110,100,111,98,106,13,32,32,32,32,32,32,32,32,32,32,32,32,13,10,52,55,49,32,48,32,111,98,106,13,60,60,47,68,101,99,111,100,101,80,97,114,109,115,60,60,47,67,111,108,117,109,110,115,32,53,47,80,114,101,100,105,99,116,111,114,32,49,50,62,62,47,70,105,108,116,101,114,47,70,108,97,116,101,68,101,99,111,100,101,47,73,68,91,60,57,66,53,49,56,54,69,70,53,66,56,66,49,50,52,49,65,56,50,49,55,50,54,56,65,65,54,52,65,57,70,54,62,60,68,52,50,68,51,55,54,53,54,65,67,48,55,54,52,65,65,53,52,66,52,57,51,50,56,52,56,68,66 etc. What am I missing?
I think #Order(0) doesn't work together with the #Bean. To fix it you should do this in that bean definition istead: final SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory()); handler.setOrder(0); See Reference Manual for more info: When using these annotations on consumer #Bean definitions, if the bean definition returns an appropriate MessageHandler (depending on the annotation type), attributes such as outputChannel, requiresReply etc, must be set on the MessageHandler #Bean definition itself. In other words: if you can use setter, you have to. We don't process annotations for this case because there is no guarantee what should get a precedence. So, to avoid such a confuse we have left for you only setters choice. UPDATE I see your problem and it is here: #Bean #BridgeTo public MessageChannel toSftpChannel() { return new PublishSubscribeChannel(); } That is confirmed by the logs: Adding {bridge:dmsSftpConfig.toSftpChannel.bridgeTo} as a subscriber to the 'toSftpChannel' channel Channel 'org.springframework.context.support.GenericApplicationContext#b3d0f7.toSftpChannel' has 3 subscriber(s). started dmsSftpConfig.toSftpChannel.bridgeTo So, you really have one more subscriber to that toSftpChannel and it is a BridgeHandler with an output to the replyChannel header. And a default order is like private volatile int order = Ordered.LOWEST_PRECEDENCE; this one becomes as a first subscriber and exactly this one returns you that byte[] just because it is a payload of request. You need to decide if you really need such a bridge. There is no workaround for the #Order though...
Spring-Boot MQTT Configuration
I have a requirement to send payload to a lot of devices whose names are picked from Database. Then, i have to send to different topics, which will be like settings/{put devicename here}. Below is the configuration i was using which i got from spring-boot reference documents. MQTTConfiguration.java #Configuration #IntegrationComponentScan public class MQTTConfiguration { #Autowired private Settings settings; #Autowired private DevMqttMessageListener messageListener; #Bean MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory clientFactory = new DefaultMqttPahoClientFactory(); clientFactory.setServerURIs(settings.getMqttBrokerUrl()); clientFactory.setUserName(settings.getMqttBrokerUser()); clientFactory.setPassword(settings.getMqttBrokerPassword()); return clientFactory; } #Bean MessageChannel mqttOutboundChannel() { return new DirectChannel(); } #Bean #ServiceActivator(inputChannel = "mqttOutboundChannel") public MessageHandler mqttOutbound() { MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("dev-client-outbound", mqttClientFactory()); messageHandler.setAsync(true); messageHandler.setDefaultTopic(settings.getMqttPublishTopic()); return messageHandler; } #MessagingGateway(defaultRequestChannel = "mqttOutboundChannel") public interface DeviceGateway { void sendToMqtt(String payload); } } Here, i am sending to only 1 topic. So i added the bean like below to send to multiple number of topics; #Bean public MqttClient mqttClient() throws MqttException { MqttClient mqttClient = new MqttClient(settings.getMqttBrokerUrl(), "dev-client-outbound"); MqttConnectOptions connOptions = new MqttConnectOptions(); connOptions.setUserName(settings.getMqttBrokerUser()); connOptions.setPassword(settings.getMqttBrokerPassword().toCharArray()); mqttClient.connect(connOptions); return mqttClient; } and i send using, try { mqttClient.publish(settings.getMqttPublishTopic()+device.getName(), mqttMessage); } catch (MqttException e) { LOGGER.error("Error While Sending Mqtt Messages", e); } Which works. But my question is, Can i achieve the same, using output channel for better performance? If yes, any help is greatly appreciated. Thank You.
MqttClient is synchronous. The MqttPahoMessageHandler uses an MqttAsyncClient and can be configured (set async to true) to not wait for the confirmation, but publish the confirmation later as an application event. If you are using your own code and sending multiple messages in a loop, it will probably be faster to use an async client, and wait for the IMqttDeliveryToken completions later.
How to read x-death header of a RabbitMQ dead-lettered message using Spring Boot?
I am trying to implement re-routing of dead-lettered messages as described in this answer. I am using Spring config. I have no idea on how to read the headers to get the original routing key and original queue. The following is my config: #Configuration public class NotifEngineRabbitMQConfig { #Bean public MessageHandler handler(){ return new MessageHandler(); } #Bean public Jackson2JsonMessageConverter messageConverter(){ return new Jackson2JsonMessageConverter(); } #Bean public MessageListenerAdapter messageListenerAdapter(){ return new MessageListenerAdapter(handler(), messageConverter()); } /** * Listens for incoming messages * Allows multiple queue to listen to * */ #Bean public SimpleMessageListenerContainer simpleMessageListenerContainer(){ SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.addQueueNames(QUEUE_TO_LISTEN_TO.split(",")); container.setMessageListener(messageListenerAdapter()); container.setConnectionFactory(rabbitConnectionFactory()); container.setDefaultRequeueRejected(false); return container; } #Bean public ConnectionFactory rabbitConnectionFactory(){ CachingConnectionFactory factory = new CachingConnectionFactory(HOST); factory.setUsername(USERNAME); factory.setPassword(PASSWORD); return factory; } }
The headers are not available using "old" style Pojo messaging (with a MessageListenerAdapter). You need to implement MessageListener which gives you access to the headers. However, you will need to invoke the converter yourself in that case and, if you are using request/reply messaging, you lose the reply mechanism within the adapter and you have to send the reply yourself. Alternatively, you can use a custom message converter and "enhance" the converted object with the header after invoking the standard converter. Consider instead using the newer style POJO messaging with #RabbitListener - it gives you access to the headers and has request/reply capability. Here's an example: #SpringBootApplication public class So37581560Application { public static void main(String[] args) { SpringApplication.run(So37581560Application.class, args); } #Bean public FooListener fooListener() { return new FooListener(); } public static class FooListener { #RabbitListener(queues="foo") public void pojoListener(String body, #Header(required = false, name = "x-death") List<String> xDeath) { System.out.println(body + ":" + (xDeath == null ? "" : xDeath)); } } } Result: Foo:[{reason=expired, count=1, exchange=, time=Thu Jun 02 08:44:19 EDT 2016, routing-keys=[bar], queue=bar}]
Gary's answer is the right one. Just a little detail, the type of xDeath is better to be ArrayList<HashMap<String,*>> instead List<String> xDeath. Then you can access any field by doing something like: xDeath.first().get("count")