我有一个使用 JAVA DSL 的简单骆驼 MINA 服务器,我的运行方式与此处记录的示例一样:
我正在尝试创建一个托管在“mina:tcp://localhost:9991”(又名 MyApp_B)的示例应用程序,它向托管在“mina:tcp://localhost:9990”(又名 MyApp_A)的服务器发送一条非常简单的消息)。
我想要发送一个简单的消息,其中包含标题中的字符串(即“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 发送简单的消息?