阿帕奇骆驼发送一个简单的消息

2022-01-19 00:00:00 message java apache-camel mina

我有一个使用 JAVA DSL 的简单的骆驼 MINA 服务器,我的运行方式与此处记录的示例一样:

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

  • 独立运行 Camel 并让它继续运行在JAVA中
  • MINA 2 组件

我正在尝试创建一个托管在mina:tcp://localhost:9991"(又名 MyApp_B)的示例应用程序,它向托管在mina:tcp://localhost:9990"的服务器发送一条非常简单的消息(又名 MyApp_A).

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).

我想要发送一个简单的消息,该消息在标头中包含一个字符串(即Hellow World!"),并在正文中包含地址.

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.
");
        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:


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("===============================================
");
        System.out.println("Received " + order + "request from server " + serverURI + "
");
        System.out.println("===============================================
");

        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. 我做错了吗?
  2. 有没有更好的方法来使用 Camel 发送简单的消息?

推荐答案

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

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

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

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

按要求添加了最新评论:

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

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.

相关文章