使用 Camel 的 ProducerTemplate#sendBody() 发送 POJO 需要什么
What is needed to send a POJO with Camel's ProducerTemplate#sendBody()
我正在尝试使用 ProducerTemplate#sendBody() 发送 POJO,但出现以下错误:
org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke
method: [searchId] on app.FsiRequest due to:
java.lang.IndexOutOfBoundsException: Key: searchId not found in bean:
app.FsiRequest@5c2d65cf of type: app.FsiRequest using OGNL path [[searchId]]
at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:119) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:135) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.model.language.ExpressionDefinition.evaluate(ExpressionDefinition.java:127) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.model.language.ExpressionDefinition.evaluate(ExpressionDefinition.java:119) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.builder.ExpressionBuilder.evaluate(ExpressionBuilder.java:1004) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.builder.SimpleBuilder.evaluate(SimpleBuilder.java:92) ~[camel-core-2.22.1.jar:2.22.1]
我发送的class(简体):
public class FsiRequest {
public String getSearchId() {
return searchId;
}
public void setSearchId(String searchId) {
this.searchId = searchId;
}
private String searchId;
public FsiRequest(Map<String, String> request) {
searchId = request.get("searchId");
}
}
这是我的调用:
private final ForkJoinPool routeExecutorPool = new ForkJoinPool(1024);
@Override
public void configure() {
from("servlet://" + SEARCH_REQUEST)
.process(exchange -> {
FsiRequest request = createRequestMap(exchange);
sendRequestToAllProviderRoutes(exchange, request);
})
.transform()
.constant("OK");
}
private void sendRequestToAllProviderRoutes(Exchange exchange, FsiRequest request) {
try {
ProducerTemplate tmpl = exchange.getContext().createProducerTemplate();
routeExecutorPool.execute(() -> getRoutes(exchange).parallelStream().forEach(
route -> tmpl.sendBody(route, request)
));
} catch (RejectedExecutionException | RuntimeCamelException e) {
log.error("FSI Servlet failed to send request to provider routes", e);
}
}
getRoutes()
通过过滤exchange.getContext().getRouteDefinitions()
.
获取相关路由
当我使用 HashMap<String, Object>
而不是 FsiRequest class. 时,sendBody()
工作正常
这个问题是由于我们的错误造成的。接收路由是这样的:
.setHeader(SEARCH_ID_KEY, simple("${body[searchId]}"))
切换到 body.searchId
解决了问题。
我正在尝试使用 ProducerTemplate#sendBody() 发送 POJO,但出现以下错误:
org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed to invoke
method: [searchId] on app.FsiRequest due to:
java.lang.IndexOutOfBoundsException: Key: searchId not found in bean:
app.FsiRequest@5c2d65cf of type: app.FsiRequest using OGNL path [[searchId]]
at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:119) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:135) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.model.language.ExpressionDefinition.evaluate(ExpressionDefinition.java:127) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.model.language.ExpressionDefinition.evaluate(ExpressionDefinition.java:119) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.builder.ExpressionBuilder.evaluate(ExpressionBuilder.java:1004) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.builder.SimpleBuilder.evaluate(SimpleBuilder.java:92) ~[camel-core-2.22.1.jar:2.22.1]
我发送的class(简体):
public class FsiRequest {
public String getSearchId() {
return searchId;
}
public void setSearchId(String searchId) {
this.searchId = searchId;
}
private String searchId;
public FsiRequest(Map<String, String> request) {
searchId = request.get("searchId");
}
}
这是我的调用:
private final ForkJoinPool routeExecutorPool = new ForkJoinPool(1024);
@Override
public void configure() {
from("servlet://" + SEARCH_REQUEST)
.process(exchange -> {
FsiRequest request = createRequestMap(exchange);
sendRequestToAllProviderRoutes(exchange, request);
})
.transform()
.constant("OK");
}
private void sendRequestToAllProviderRoutes(Exchange exchange, FsiRequest request) {
try {
ProducerTemplate tmpl = exchange.getContext().createProducerTemplate();
routeExecutorPool.execute(() -> getRoutes(exchange).parallelStream().forEach(
route -> tmpl.sendBody(route, request)
));
} catch (RejectedExecutionException | RuntimeCamelException e) {
log.error("FSI Servlet failed to send request to provider routes", e);
}
}
getRoutes()
通过过滤exchange.getContext().getRouteDefinitions()
.
HashMap<String, Object>
而不是 FsiRequest class. 时,sendBody()
工作正常
这个问题是由于我们的错误造成的。接收路由是这样的:
.setHeader(SEARCH_ID_KEY, simple("${body[searchId]}"))
切换到 body.searchId
解决了问题。