Spring tailing multiple file tails

I am writing a spring integration application that should contain several files (maybe as many as 100). I use OSDelegatingFileTailingMessageProducer as the source of the message, which is the beginning of a pipeline that includes several filters and channels.

Capturing a single file works fine with this pipeline with an XML configuration file for channels and transformers, but reducing many of these files would mean multiplying this XML configuration, which is not good programming practice in my eyes.

I assume that I will have to build these pipelines inside Java, programmatically constructing the spring application context. Are there any other options?

EDIT:

Perhaps using BeanFactoryPostProcessor is the way to go: https://stackoverflow.com/a/165189/

+5
source share
2 answers

I think it would be easier to create message producer programs and link them to the same outputChannel . There is no need to create a Spring application context every time. Just get the feed out of context (e.g. @AutoWired ) and set outputChannel .

The adapters surveyed are a bit more complicated, but in this case, each tail adapter is a simple single bean.

Just remember to call afterPropertiesSet() and start() after setting the properties.

However, if you need a unique downstream stream for each liner, you can use a technique similar to the dynamic ftp example with parameterized application contexts.

+4
source

In the end, I did not register instances of OSDelegatingFileTailingMessageProducer with the Spring application context, because there was no need, as suggested by Gary. Instead, I used ApplicationListener and registered it in my Spring context. Then I created tailers inside the onApplicationEvent (...) method. Here is the minimal version:

 public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired @Qualifier("outputChannel") private SubscribableChannel outputChannel; @Override public void onApplicationEvent(ContextRefreshedEvent event) { OSDelegatingFileTailingMessageProducer tailer = new OSDelegatingFileTailingMessageProducer(); tailer.setOutputChannel(outputChannel); tailer.setFile(new File("/file/to/tail.txt")); tailer.setOptions("-f -n 0"); tailer.afterPropertiesSet(); tailer.start(); } } 

EDIT:

In addition, we did not use OSDelegatingFileTailingMessageProducer, but the one that was from apache, since the tail command behaves differently in different versions of Unix. At first glance, we were not able to identify differences in performance.

0
source

All Articles