Apache camel send a simple message

眉间皱痕 提交于 2019-12-13 06:24:21

问题


I have a simply camel MINA server using the JAVA DSL, and I am running like the example documented here:

  • Running Camel standalone and have it keep running in JAVA
  • MINA 2 Component

I am trying to create a sample application hosted at "mina:tcp://localhost:9991" (aka MyApp_B) that sends a very simple message to a server hosted at "mina:tcp://localhost:9990" (aka MyApp_A).

I want is to send a simple message containing a String in the header (which is "Hellow World!") and with the address in the body.

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);
    }

}

Bean used by MyApp_A:

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);
    }
}

I have done this code, however, the servers on the other side don't seem to receive anything. Therefore I have 2 questions:

  1. Am I doing something wrong?
  2. Is there a better way to send simple message using Camel?

回答1:


MyApp_A does NOT send any messages. You need to send a message to the direct endpoint to start the route.

You can also change direct to a timer component to have it trigger every X second etc.

Added latest comment as requested:

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.



来源:https://stackoverflow.com/questions/19865952/apache-camel-send-a-simple-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!