如何使用标头值动态写入Camel中的其他文件夹?

时间:2019-03-29 04:06:00

标签: apache-camel spring-camel

我需要将文件传输到多个目标系统。 这些目标系统是在源系统的文件名中定义的。

例如:

SLPNPM_ID0001_T20190329000000.DAT -> target directory : /SLPNPM/
SLPNP1_ID0001_T20190329000001.DAT -> target directory : /SLPNP1/
SLPNP2_ID0002_T20190329000002.DAT -> target directory : /SLPNP2/
...                                   ...

因此,我实现了以下代码。

CamelContext.xml

    <route id="_route1">
      <from id="_from1" uri="direct:source"/>
      <bean id="_setTarget" ref="setTarget"/>
      <to id="_to1" uri="file:C:/{${header.targetNeId}}/outputFolder"/>
    </route>
    <bean id="setTarget" class="com.ktds.openmzn.sample.SetTarget"/>

SetTarget.java

    public class SetTarget implements Processor {
     Logger logger = LoggerFactory.getLogger(SetTarget.class);
     @Override
     public void process(Exchange exchange) throws Exception {

    String target_neid = exchange.getIn().getHeader("CamelFileName").toString().substring(0, 6);
        //SLPNPM_FGIDRO01_ID0001_T20190312000500.DAT
        logger.info("Target Ne Id ::: " + target_neid);
        exchange.getIn().setHeader("targetNeId", target_neid);
     }

    }

错误日志

Caused by: java.lang.IllegalArgumentException: Invalid directory: C:/{${header.targetNeId}}/outputFolder. Dynamic expressions with ${ } placeholders is not allowed. Use the fileName option to set the dynamic expression.
at org.apache.camel.component.file.FileComponent.buildFileEndpoint(FileComponent.java:57) ~[camel-core-2.23.1.jar:2.23.1]
at org.apache.camel.component.file.GenericFileComponent.createEndpoint(GenericFileComponent.java:56) ~[camel-core-2.23.1.jar:2.23.1]
at org.apache.camel.component.file.GenericFileComponent.createEndpoint(GenericFileComponent.java:38) ~[camel-core-2.23.1.jar:2.23.1]
at org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:126) ~[camel-core-2.23.1.jar:2.23.1]
at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:706) ~[camel-core-2.23.1.jar:2.23.1]
... 39 common frames omitted

如果我想满足自己的业务需求,该如何处理?

0 个答案:

没有答案