Is there a way to start the file:inbound-channel-adapter through code?

天大地大妈咪最大 提交于 2020-01-05 12:16:41

问题


I have a situation where a particular file is to be copied from a location to another. The polling is not required as the action will be deliberately triggered. Also the directory from which the file is to be picked up is decided at run time.

I can have a configuration as follows:

<int-file:inbound-channel-adapter id="filesIn" directory="@outPathBean.getPath()" channel="abc" filter="compositeFilter" >
    <int:poller id="poller" fixed-delay="5000" />

</int-file:inbound-channel-adapter>
<int:channel id="abc"/>

<int-file:outbound-channel-adapter channel="abc" id="filesOut"
    directory-expression="file:${paths.root}"
    delete-source-files="true" filename-generator="fileNameGenerator" />

filenamegenerator and composite filter classes are configured as well.

I am new to spring. Please point me in the right direction!!


回答1:


You can use a FireOnceTrigger as discussed in this answer and start/stop the adapter as needed.

To get a reference to the adapter (a SourcePollingChannelAdapter), inject (or @Autowire etc.) it as a Lifecycle bean (start()/stop() etc).

Or you can do the whole thing programmatically using a FileReadingMessageSource, and discussed in this answer.




回答2:


Sample for start/stop Adapter.incase its useful.

SourcePollingChannelAdapter sourcePollingChannelAdapter = (SourcePollingChannelAdapter) context
                .getBean("filesIn");  //adapter id in the bean configuration
        // Stop
        if (sourcePollingChannelAdapter.isRunning()) {
            sourcePollingChannelAdapter.stop();
        }
        // Set Cron Expression if required when start or use any triggers
        CronTrigger cronTrigger = new CronTrigger("* * * * * ?");
        sourcePollingChannelAdapter.setTrigger(cronTrigger);

        // Start
        if (!sourcePollingChannelAdapter.isRunning()) {
            sourcePollingChannelAdapter.start();
        }


来源:https://stackoverflow.com/questions/26708039/is-there-a-way-to-start-the-fileinbound-channel-adapter-through-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!