我有一个工作应用程序,它从属性文件中指定的目录中读取并写入另一个目录。以下是用于读写的入站和出站通道适配器。
<file:inbound-channel-adapter id="inputFileChannelAdapter" channel="fileIn" directory="${dir.monitor}"
auto-startup="false" prevent-duplicates="false" filename-pattern="*.done" >
<int:poller id="inputFilePoller" time-unit="SECONDS" fixed-delay="1" max-messages-per-poll="100" />
</file:inbound-channel-adapter>
<int:channel id="finishedFileChannel"/>
<file:outbound-channel-adapter id="finishedDataFiles" delete-source-files="true" auto-create-directory="false"
directory="${dir.finished}" channel="finishedFileChannel" />
我需要允许在应用程序运行时更改目录。我创建了一个控制总线,允许我停止轮询服务并使用以下代码更改输入目录:
SourcePollingChannelAdapter adapter = context.getBean("inputFileChannelAdapter", SourcePollingChannelAdapter.class);
FileReadingMessageSource source = context.getBean("inputFileChannelAdapter.source", FileReadingMessageSource.class);
File monitorFileDir = new File("C:\newInput");
source.setDirectory( monitorFileDir );
但是,我无法弄清楚如何为出站通道适配器完成相同的操作。我尝试获取out bound channnel适配器的引用,然后创建一个新的EventDivenConsumer和FileWritingMessageHandler并将其重新分配给引用,但它没有工作,我觉得我正在使用该解决方案走错路。任何建议将不胜感激。
答案 0 :(得分:0)
如果您希望将其基于消息,则可以使用directory-expression="@someBean.whereTo()"
或"@someBean.whereTo(#root)"
。
对于更新版本的框架,您可以使用bean名称finishedDataFiles.handler
获取对处理程序的引用,但是,正如@Artem所说,目录是final
。
答案 1 :(得分:0)
对于FileReadingMessageSource
你做的更正。
对于FileWritingMessageHandler
,您不能这样做,因为directory
已转换为private final Expression destinationDirectoryExpression;
。
因此,我们如何克服您的change dir a runtime
用例
<bean id="targetDir" class="java.util.concurrent.atomic.AtomicReference">
<constructor-arg value="${dir.finished}"/>
</bean>
<file:outbound-channel-adapter directory-expression="@targetDir.get()"/>
有了这个,你总是可以在运行时从那个bean ApplicationContext
中检索并更改目标目录的值:
AtomicReference<String> targetDir = (AtomicReference<String>) context.getBean("targetDir", AtomicReference.class);
targetDir.set("/new/target/dir");