Java 在apachecamel中使用另一个头值

Java 在apachecamel中使用另一个头值,java,apache-camel,Java,Apache Camel,在apache驼峰路由中,如何在另一个头中使用一个头值 我尝试了以下方法,但它给了我错误 <setHeader headerName="dateFormat"> <simple resultType="java.lang.String">ddMMyyyyHHms</simple> </setHeader> <setHeader headerName="startTime"> <simple resultType="jav

在apache驼峰路由中,如何在另一个头中使用一个头值

我尝试了以下方法,但它给了我错误

<setHeader headerName="dateFormat">
  <simple resultType="java.lang.String">ddMMyyyyHHms</simple>
</setHeader>

<setHeader headerName="startTime">
  <simple resultType="java.lang.String">${date:now:${header.dateFormat}}</simple>
</setHeader>

ddmmyyyhms
${date:now:${header.dateFormat}

我这样做是因为我需要dateFormat值同时位于不同的位置。

这可能取决于您的Camel版本

从Camel 2.9开始,您可以嵌套函数,如下所示:

route.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
    <from uri="file://target/inbound"/>
    <setHeader headerName="dateFormat">
    <simple resultType="java.lang.String">ddMMyyyyHHmmss</simple>
    </setHeader>
    <setHeader headerName="startTime">
    <simple resultType="java.lang.String">${date:now:${header.dateFormat}}</simple>
    </setHeader>
    <log message="header timestamp: ${header.startTime}"/>
    <to uri="mock:end"/>
    </route>
    </camelContext>

</beans>

你能告诉我们错误吗?:)很抱歉,没有引发异常,当我获取“startTime”值时,它是空的。谢谢,它可能会帮助其他人回答您的问题。我不是这项技术的专家;)。
public class NestedTest extends CamelSpringTestSupport {

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("route.xml");
    }

    @Override
    public void setUp() throws Exception {
        deleteDirectory("target/inbound");
        super.setUp();
    }

    @Test
    public void nested() throws InterruptedException {
        getMockEndpoint("mock:end").expectedMessageCount(1);

        template.sendBodyAndHeader("file://target/inbound", 
                "hello camel", 
                Exchange.FILE_NAME, 
                "filename.in"
        );

        assertMockEndpointsSatisfied();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd
    ">

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
    <from uri="file://target/inbound"/>
    <setHeader headerName="dateFormat">
    <simple resultType="java.lang.String">ddMMyyyyHHmmss</simple>
    </setHeader>
    <setHeader headerName="startTime">
    <simple resultType="java.lang.String">${date:now:${header.dateFormat}}</simple>
    </setHeader>
    <log message="header timestamp: ${header.startTime}"/>
    <to uri="mock:end"/>
    </route>
    </camelContext>

</beans>
15:58:57 INFO  route1 - header timestamp: 07062016155857
15:58:56 INFO  MockEndpoint - Asserting: Endpoint[mock://end] is satisfied
15:58:57 INFO  NestedTest - *************************************************
15:58:57 INFO  NestedTest - Testing done: nested(camelinaction.NestedTest)
15:58:57 INFO  NestedTest - Took: 1.010 seconds (1010 millis)
15:58:57 INFO  NestedTest - *************************************************