Exchange可以直接作为参数在route用到的方法中使用,如果route中的方法不是Exchange,Camel会根据一套规则将Exchange中的Body转换成该方法的参数类型.
这个结构里的各部分内容可以像下面的代码示例的方式进行访问:
from("file:d:/temp/inbox?delay=3000") .bean(new ExchangeManipulateBean(),"implicitConvert") .bean(new ExchangeManipulateBean(),"traverseExchange") .to("file:d:/temp/outbox"); public class ExchangeManipulateBean { private static Logger log = Logger.getLogger(ExchangeManipulateBean.class); public void implicitConvert(String msg){ log.debug("implicitConvert: " + msg); } public void traverseExchange(Exchange exchange){ log.debug("exchange.getExchangeId() " + exchange.getExchangeId()); log.debug("exchange.getPattern() " + exchange.getPattern()); log.debug("exchange.getException() " + exchange.getException()); Map<String, Object> props = exchange.getProperties(); for(String key:props.keySet()){ log.debug("props: " + key + " " + props.get(key)); } Message in = exchange.getIn();//in always null,but can be accessed without exception log.debug("exchange.getIn() " + in); log.debug("in.getMessageId() " + in.getMessageId() ); log.debug("in.getClass().getName() " + in.getClass().getName()); Map<String, Object> headers = in.getHeaders(); for(String key:headers.keySet()){ log.debug("headers: " + key + " " + headers.get(key)); } Object body = in.getBody(); log.debug("body " + body + " " + body.getClass().getName()); log.debug("in.getAttachmentNames() " + in.getAttachmentNames()); //Message out = exchange.getOut();//whenever out is touched,information in headers are lost1* implicitConvert 的方法接收的参数是String,Camel会在运行的时候根据一定的规则将"In Message"的Body转换成String,这里是将文件的内容转成String,这是由Camel内部的TypeConverter完成的.