路由属性和交换属性之间的骆驼区别
Camel difference between route properties and exchange properties
谁能解释一下这两个属性之间的区别?
@Override
public void setUp() throws Exception {
context = new DefaultCamelContext(new SimpleRegistry());
template = context.createProducerTemplate();
context.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
PropertiesComponent prop = context.getComponent(
"properties", PropertiesComponent.class);
prop.setLocation("classpath:test.properties");
from("direct:start")
.log("Property: ${properties:a}")
.process(new Processor() {
@Override
public void process(Exchange ex) throws Exception {
String a = ex.getProperty("a", String.class);
LOG.info("Property: " + a);
}
})
;
}
});
context.getShutdownStrategy().setTimeout(1);
context.start();
}
@Test
public void testRoute() throws Exception {
template.sendBody("direct:start", null);
}
属性文件 (test.properties
):
a = a
输出:
2015-09-03 14:38:01,740 | INFO | route1 | Property: a
2015-09-03 14:38:01,743 | INFO | CamelTest2 | Property: null
第一行来自.log("${properties:a}")
,所以可以找到。但是,String a = ex.getProperty("a", String.class);
不能。两者都是属性,指向同一个属性,对吧?
有什么区别,如何在处理器中找到 属性?
Exchange
仅在客户端收到请求时创建。这意味着您的 Camel 处理器只能访问 Message
而不能访问来自外部资源的 属性。
Exchange 的属性是消息的元信息。根据文档,
The Exchange also holds meta-data during its entire lifetime stored as
properties accessible using the various getProperty(String)
methods.
The setProperty(String, Object)
is used to store a property. For
example you can use this to store security, SLA related data or any
other information deemed useful throughout processing.
谁能解释一下这两个属性之间的区别?
@Override
public void setUp() throws Exception {
context = new DefaultCamelContext(new SimpleRegistry());
template = context.createProducerTemplate();
context.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
PropertiesComponent prop = context.getComponent(
"properties", PropertiesComponent.class);
prop.setLocation("classpath:test.properties");
from("direct:start")
.log("Property: ${properties:a}")
.process(new Processor() {
@Override
public void process(Exchange ex) throws Exception {
String a = ex.getProperty("a", String.class);
LOG.info("Property: " + a);
}
})
;
}
});
context.getShutdownStrategy().setTimeout(1);
context.start();
}
@Test
public void testRoute() throws Exception {
template.sendBody("direct:start", null);
}
属性文件 (test.properties
):
a = a
输出:
2015-09-03 14:38:01,740 | INFO | route1 | Property: a
2015-09-03 14:38:01,743 | INFO | CamelTest2 | Property: null
第一行来自.log("${properties:a}")
,所以可以找到。但是,String a = ex.getProperty("a", String.class);
不能。两者都是属性,指向同一个属性,对吧?
有什么区别,如何在处理器中找到 属性?
Exchange
仅在客户端收到请求时创建。这意味着您的 Camel 处理器只能访问 Message
而不能访问来自外部资源的 属性。
Exchange 的属性是消息的元信息。根据文档,
The Exchange also holds meta-data during its entire lifetime stored as properties accessible using the various
getProperty(String)
methods. ThesetProperty(String, Object)
is used to store a property. For example you can use this to store security, SLA related data or any other information deemed useful throughout processing.