[英]My Apache Camel Processor is Not Working
我正在嘗試使用稱為Processor
的Apache Camel
接口,並且遇到了一些困難。 我希望消息發送到1)發送到JBoss Fuse應用程序服務器中的ActiveMQ隊列,2)由駱駝處理器處理,然后3)發送到源代碼中指定的其他隊列。 現在發生的是主打印中的SOP語句和“日志記錄”中的一些錯誤消息,但是沒有任何內容從程序發送到隊列。
這是我的代碼:
/* create a Camel processor */
package foo;
import org.apache.camel.Processor;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
public class MyOwnProcessor implements Processor {
//main
public static void main(String[] args) {
System.out.println("Starting main method in MyOwnProcessor.java");
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("QueueA").processRef("MyOwnProcessor").to("QueueB");
}
};
System.out.println("main is done.");
} //end main
public void process(Exchange exchange) {
System.out.println("Hello the process was executed.");
String s = exchange.getIn().getBody(String.class);
exchange.getIn().setBody("The body of the message is: " + s);
} //end process method
} //end class
這是當前輸出:
在MyOwnProcessor.java中啟動main方法
SLF4J:無法加載類“ org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默認為不操作(NOP)記錄器實現SLF4J:有關更多詳細信息,請參見http://www.slf4j.org/codes.html#StaticLoggerBinder 。
主要完成。
嘗試這個,
public static void main(String args[]) throws Exception {
// create CamelContext
CamelContext context = new DefaultCamelContext();
// connect to embedded ActiveMQ JMS broker
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://localhost:61616");
context.addComponent("jms",
JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("jms:queue:QueueA")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
String s = exchange.getIn().getBody(String.class);
System.out.println("The body of the message is: " + s);
}
}).to("jms:queue:QueueB");
}
});
// start the route and let it do its work
context.start();
Thread.sleep(10000);
// stop the CamelContext
context.stop();
}
創建路由不會使它運行-您仍然需要運行的CamelContext,並且需要向其傳遞一條消息以使事情開始。 嘗試首先使它開始工作,僅對Processor使用匿名內部類:
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("direct:source").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("Success!");
}
});
}
};
context.addRoutes(builder);
ProducerTemplate template = context.createProducerTemplate();
context.start();
template.sendBody("direct:source", "test");
}
一旦可行,添加一個單獨的實現Processor的類,並使用它代替匿名內部類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.