Apache camel发送一条简单的消息

时间:2013-11-08 18:23:42

标签: java message apache-camel mina

我有一个使用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);
    }
}

我已经完成了这段代码,但是,另一方的服务器似乎没有收到任何东西。因此,我有两个问题:

  1. 我做错了什么?
  2. 使用Camel发送简单消息是否有更好的方法?

1 个答案:

答案 0 :(得分:2)

MyApp_A不发送任何消息。您需要向直接端点发送消息以启动路由。

您也可以直接更改为计时器组件,使其每X秒触发一次等。

根据要求添加了最新评论:

  

是的,直接路线也在运行。它只是发送一个   消息指示,你需要使用Camel。直接是一个   内部Camel组件,用于在其端点之间发送消息   (路线)。要向其发送消息,您可以使用生产者模板。   参见Camel in Action一书中的第7章第7.7节。