我的 Apache Camel 处理器不工作
My Apache Camel Processor is Not Working
我正在尝试使用名为 Processor
的 Apache Camel
界面,但 运行 遇到了一些困难。我希望消息 1) 被发送到 JBoss Fuse 应用服务器中的 ActiveMQ 队列,2) 由 Camel 处理器处理,然后 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
中启动主要方法
SLF4J:无法加载 class "org.slf4j.impl.StaticLoggerBinder"。
SLF4J:默认为无操作 (NOP) 记录器实现
SLF4J:有关详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。
主要完成。
创建路由不会导致它 运行 -- 你仍然需要一个 运行ning CamelContext,并且需要向它传递一条消息才能开始。首先尝试让它工作,只需为您的处理器使用匿名内部 class:
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");
}
一旦成功,添加一个单独的 class 来实现处理器并使用它代替匿名内部 class。
试试这个,
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();
}
我正在尝试使用名为 Processor
的 Apache Camel
界面,但 运行 遇到了一些困难。我希望消息 1) 被发送到 JBoss Fuse 应用服务器中的 ActiveMQ 队列,2) 由 Camel 处理器处理,然后 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
中启动主要方法SLF4J:无法加载 class "org.slf4j.impl.StaticLoggerBinder"。 SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。
主要完成。
创建路由不会导致它 运行 -- 你仍然需要一个 运行ning CamelContext,并且需要向它传递一条消息才能开始。首先尝试让它工作,只需为您的处理器使用匿名内部 class:
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");
}
一旦成功,添加一个单独的 class 来实现处理器并使用它代替匿名内部 class。
试试这个,
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();
}