更改文件上的动态目录:inbound-channel-adapter

时间:2015-02-10 07:48:44

标签: spring-integration citrus-framework

我是Spring的新手,我正在使用Citrus Framework。 我会尝试动态更改inbound-channel-adapter destination变量。此变量位于属性文件中并随时更改。

目前我正在使用AtomicReference并在java代码中更改其值

context.xml

    <bean id="targetDir" class="java.util.concurrent.atomic.AtomicReference">
        <constructor-arg value="${output.path.temp}"/>
    </bean>

    <file:inbound-channel-adapter id="fileInboundAdapter" auto-create-directory="false"
        channel="fileChannel" directory="file:@targetDir.get()" auto-startup="false"
        filename-pattern="*.xml">
        <si:poller cron="0 * * * * ?"/>
    </file:inbound-channel-adapter>

在java文件中:

SourcePollingChannelAdapter fileInboundAdapter = (SourcePollingChannelAdapter)context.getApplicationContext().getBean("fileInboundAdapter");
if (fileInboundAdapter.isRunning()) {
    fileInboundAdapter.stop();

    @SuppressWarnings("unchecked")
    AtomicReference<String> targetDir = (AtomicReference<String>)     
    context.getApplicationContext().getBean("targetDir", AtomicReference.class);
    targetDir.set(strOutPath[0]+"/"+strOutPath[1]+"/"+strOutPath[2]+"/"+strOutPath[3]+"/"); 
    fileInboundAdapter.start();
}

此解决方案不起作用......有人有任何解决方案吗?

非常感谢。

1 个答案:

答案 0 :(得分:2)

那是真的。因为AtomicReference对目标directory没有影响。

你这样做directory="file:@targetDir.get()"。它根本不正确,因为此String将尝试转换为File对象。如果你想在这里使用SpEL,它应该是这样的:

directory="#{targetDir.get()}"

没有任何file:前缀。

无论如何它没有帮助,因为SpEL仅在applicationContext strtup上进行一次评估。

由于您要在运行时更改directory,因此您应该使用服务中的FileReadingMessageSource.setDirectory。像这样:

SourcePollingChannelAdapter fileInboundAdapter = (SourcePollingChannelAdapter)context.getApplicationContext().getBean("fileInboundAdapter");
if (fileInboundAdapter.isRunning())
    fileInboundAdapter.stop();

    FileReadingMessageSource source = (FileReadingMessageSource) context.getApplicationContext().getBean("fileInboundAdapter.source");    
    source.setDirectory(new File(strOutPath[0]+"/"+strOutPath[1]+"/"+strOutPath[2]+"/"+strOutPath[3]+"/")); 
    fileInboundAdapter.start();
}

摆脱AtomicReference

从一开始,您就可以直接使用property-placeholder作为directory属性。

相关问题