简体   繁体   English

Apache camel发送一条简单的消息

[英]Apache camel send a simple message

I have a simply camel MINA server using the JAVA DSL, and I am running like the example documented here: 我有一个简单的使用JAVA DSL的驼峰MINA服务器,我的运行方式与此处记录的示例类似:

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). 我正在尝试创建一个托管在“mina:tcp:// localhost:9991”(又名MyApp_B)的示例应用程序,该应用程序向托管在“mina:tcp:// localhost:9990”(也称为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. 我想要的是在标题中发送一个包含字符串的简单消息(这是“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);
    }

}

Bean used by MyApp_A: 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);
    }
}

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? 有没有更好的方法使用Camel发送简单的消息?

MyApp_A does NOT send any messages. MyApp_A不发送任何消息。 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. 您还可以直接更改为计时器组件,使其每X秒触发一次等。

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. 它只是为了直接发送消息,你需要使用Camel来做到这一点。 direct is an internal Camel component for sending messages between its endpoint (routes). direct是一个内部Camel组件,用于在其端点(路由)之间发送消息。 To send a message to it, you can use the producer template. 要向其发送消息,您可以使用生产者模板。 See chapter 7, section 7.7 in the Camel in Action book. 请参阅“Camel in Action”一书中的第7章第7.7节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM