英文:
Implement counter variable in camel route
问题
以下是翻译好的内容:
我正在尝试从数据库获取员工详细信息列表,并使用REST API将其推送到外部系统。我已经能够为上述流程创建一个路由。
此外,我想要知道已创建和失败的记录数量。因此,我通过将属性设置为值0来创建了一个计数器,然后递增属性值。当我尝试递增属性值时,它始终与我初始化的值相同。
from("direct:test")
.setBody(constant("select name as name, location as location, mobile as mobile from employee"))
.to("jdbc:testdb")
.process(exchange -> {
// 将员工详细信息列表转换的自定义逻辑
})
.split(body())
.setProperty("successrec", simple("0"))
.setProperty("failurerec", simple("0"))
.doTry()
.setProperty("successrec", simple("${property.successrec++}"))
.to("http://test/upload/employee")
.doCatch(Exception.class)
.process(exchange -> {
Integer failureRecords = exchange.getProperty("failurerec", Integer.class);
exchange.setProperty("failurerec", failureRecords++);
});
我甚至尝试使用处理器来设置和获取属性值,但它没有起作用。是否有任何方法可以对成功和失败的记录进行计数?
英文:
I am trying to get the List of employee details From Database and Push it in to External System using REST API. I am able to create a route for the above flow .
Also, I want to know the count of created and failure records. So i created a counter by setting property with value 0 and then increment the property value . When i try to increment the property value it always the same as what i initialized.
from("direct:test")
.setBody(constant("select name as name, location as location,mobile as mobile from employee"))
.to("jdbc:testdb")
.process(exchange ->{
// Custom Logic which will convert list of Employee Details
})
.split(body())
.setProperty("successrec", simple("0"))
.setProperty("failurerec", simple("0"))
.doTry()
.setProperty("successrec", simple("${property.successrec++}"))
.to("http://test/upload/employee")
.doCatch((Exception.class)).process( exchange ->{
Integer failureRecords=exchange.getProperty("failurerec",Integer.class);
exchange.setProperty("failurerec", failureRecords++);
});
I tried even using processor to set and get the property value, But it didn't worked . Is there any way i can have a counter over success and failure records.
答案1
得分: 3
属性是局部于交换的,因此您可能需要使用路由策略来注入全局计数器。
创建路由策略:
class MyRoutePolicy extends RoutePolicySupport {
private AtomicInteger counter = new AtomicInteger();
@Override
public void onExchangeBegin(Route route, Exchange exchange) {
exchange.setProperty("counter", counter);
}
public int getCounter() {
return counter.get();
}
};
将策略与路由关联:
MyRoutePolicy policy = new MyRoutePolicy();
from("direct:test")
.routePolicy(policy)
...
这是可能的解决方案之一,但根据您的需求,您也可以使用全局计数器,这可能更简单。
英文:
Properties are local to an exchange so you may need to use a Route Policy to inject a global counter.
Create a route policy:
class MyRoutePolicy extends RoutePolicySupport {
private AtomicInteger counter = new AtomicInteger();
@Override
public void onExchangeBegin(Route route, Exchange exchange) {
exchange.setProperty("counter", counter);
}
public int getCounter() {
return counter.get();
}
};
Associate the policy to the route:
MyRoutePolicy policy = new MyRoutePolicy();
from("direct:test")
.routePolicy(policy)
...
This is one of the possible solutions but you may also using a global counter that depending on your needs, may be simpler.
答案2
得分: 0
使用这个策略的工作方式:
public class MyRoutePolicy extends RoutePolicySupport {
private AtomicInteger counter = new AtomicInteger();
@Override
public void onExchangeBegin(Route route, Exchange exchange) {
exchange.setProperty("counter", counter.incrementAndGet());
}
public int getCounter() {
return counter.get();
}
}
英文:
With this policy works:
public class MyRoutePolicy extends RoutePolicySupport {
private AtomicInteger counter = new AtomicInteger();
@Override
public void onExchangeBegin(Route route, Exchange exchange) {
exchange.setProperty("counter", counter.incrementAndGet());
}
public int getCounter() {
return counter.get();
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论