在消息中获取与ExchangePattern ID和BodyType等交换相关的属性

时间:2015-07-07 09:05:05

标签: apache-camel

我是骆驼新手。我试图从一个位置读取文件,对文件内容进行一些更改并将文件发送到另一个位置。我试图在文件中添加与交换相关的信息,如下所示:

Exchange[ExchangePattern: InOnly, BodyType: String, Body:
Test Message from custom processor:
Text from input file.

以上样本中的第一行未来。到目前为止,我编写了以下代码:

的applicationContext.xml:

<bean id="customProcessor" class="com.javacodegeeks.camel.CustomProcessor"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
     <from uri="file:C:\\inputdir\\" />     
     <process ref="customProcessor"/>            
     <to uri="file:C:\\outputdir\\" />
</route>

CustomProcessor.java:

public class CustomProcessor implements Processor {

public void process(Exchange exchange) throws Exception {
    String msgBody = exchange.getIn().getBody(String.class);
    exchange.getIn().setBody("Test Message from custom processor: " + msgBody);
}

}

主要课程:

public static final void main(String[] args) throws Exception {
    ApplicationContext appContext = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    CamelContext camelContext = SpringCamelContext.springCamelContext(
            appContext, false);
    try {
        camelContext.start();
        Thread.sleep(200000);
    } finally {
        camelContext.stop();
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Exchange的方法getProperties()访问Camel Exchange的所有属性,即返回Map的exchange.getProperties(),因此使用此方法可以获取信息并使用CustomProcessor Camel处理器将其保存到文件中。 / p>

有关详细信息,请查看: http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html