我有一个使用 JAVA DSL 的简单的camel MINA 服务器,并且我的运行方式与此处记录的示例类似:
我正在尝试创建一个托管在“mina:tcp://localhost:9991”(又名 MyApp_B)的示例应用程序,该应用程序向托管在“mina:tcp://localhost:9990”的服务器发送一条非常简单的消息(又名 MyApp_A)。
我想要发送一条简单的消息,其中包含 header 中的字符串(即“Hellow World!”)并在正文中包含地址。
public class MyApp_B extends Main{
public static final String MINA_HOST = "mina:tcp://localhost:9991";
public static void main(String... args) throws Exception {
MyApp_B main = new MyApp_B();
main.enableHangupSupport();
main.addRouteBuilder(
new RouteBuilder(){
@Override
public void configure() throws Exception {
from("direct:start")
.setHeader("order", constant("Hello World!"))
.setBody(constant(MINA_HOST))
.to("mina:tcp://localhost:9990");
}
}
);
System.out.println("Starting Camel MyApp_B. Use ctrl + c to terminate the JVM.\n");
main.run();
}
}
<小时/>
public class MainApp_A {
public static void main(String... args) throws Exception {
Main main = new Main();
main.enableHangupSupport();
main.addRouteBuilder(new RouteBuilder(){
@Override
public void configure() throws Exception {
from("mina:tcp://localhost:9990").bean(MyRecipientListBean.class,
"updateServers").to("direct:debug");
from("direct:debug").process(new Processor() {
public void process(Exchange exchange) throws Exception {
System.out.println("Received order: " +
exchange.getIn().getBody());
}
});
}
});
main.run(args);
}
}
<小时/>
MyApp_A 使用的 Bean:
public class MyRecipientListBean {
public final static String REMOVE_SERVER = "remove";
public final static String ADD_SERVER = "add";
private Set<String> servers = new HashSet<String>();
public void updateServers(@Body String serverURI,
@Header("order") String order){
System.out.println("===============================================\n");
System.out.println("Received " + order + "request from server " + serverURI + "\n");
System.out.println("===============================================\n");
if(order.equals(ADD_SERVER))
servers.add(serverURI);
else if(order.equals(REMOVE_SERVER))
servers.remove(serverURI);
}
}
我已经完成了这段代码,但是另一端的服务器似乎没有收到任何东西。因此我有两个问题:
- 我做错了什么吗?
- 是否有更好的方法使用 Camel 发送简单消息?
最佳答案
MyApp_A 不发送任何消息。您需要向直接端点发送消息才能启动路由。
您还可以直接更改为计时器组件,使其每 X 秒触发一次等。
根据要求添加了最新评论:
yes and the direct route is also running. Its just that to send a message to direct, you need to do that using Camel. direct is an internal Camel component for sending messages between its endpoint (routes). To send a message to it, you can use the producer template. See chapter 7, section 7.7 in the Camel in Action book.
关于java - Apache Camel 发送一条简单的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865952/