簡體   English   中英

Apache camel發送一條簡單的消息

[英]Apache camel send a simple message

我有一個簡單的使用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發送簡單的消息?

MyApp_A不發送任何消息。 您需要向直接端點發送消息以啟動路由。

您還可以直接更改為計時器組件,使其每X秒觸發一次等。

根據要求添加了最新評論:

是的,直接路線也在運行。 它只是為了直接發送消息,你需要使用Camel來做到這一點。 direct是一個內部Camel組件,用於在其端點(路由)之間發送消息。 要向其發送消息,您可以使用生產者模板。 請參閱“Camel in Action”一書中的第7章第7.7節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM