我正在尝试为一次性活动构建一个骆驼独立应用程序 - 使用camel-main
. 我已经成功地为基本路由构建了 XML DSL。
现在作为流程的一部分,我想调用一个自定义的 java 处理器。
根据https://camel.apache.org/manual/processor.html#_referring_to_beans_using_class_syntax,它表明这应该可以使用 #class 约定。
我在路由文件中添加了以下内容:
它似乎对我不起作用。这有什么先决条件吗?返回的错误是:
Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> process[ref:#class:org.myorg.camel.MyProcessor] <<< in route: Route(route1)[From[timer://foo?fixedRate=true&period=60000] ... because of No bean could be found in the registry for: #class:org.myorg.camel.MyProcessor of type: org.apache.camel.Processor
at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:240)
这是我的示例代码: https ://github.com/chiragsanghavi/camel-experiments/blob/main/processor
路线:
<route>
<from uri="timer://foo?fixedRate=true&period=60000"/>
<setBody>
<simple>Hello</simple>
</setBody>
<process ref="#class:org.myorg.camel.MyProcessor"/>
<to uri="log:hello"/>
</route>
处理器
package org.myorg.camel;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyProcessor implements Processor {
private static final Logger logger = LoggerFactory.getLogger(MyProcessor.class);
public MyProcessor()
{
}
@Override
public void process(Exchange exchange) {
logger.trace("Processing for "+exchange.getExchangeId());
String payload = exchange.getIn().getBody(String.class);
// do something with the payload and/or exchange here
exchange.getIn().setBody(payload+"X");
exchange.getIn().setHeader("NewHeader","SampleValue");
}
}