Apache的骆驼发送一个简单的消息 [英] Apache camel send a simple message

查看:542
本文介绍了Apache的骆驼发送一个简单的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的使用Java DSL骆驼MINA服务器,我正在像这里记录的例子:

我想创建托管在一个示例应用程序米娜:TCP://本地主机:9991(又名MyApp_B)发送一个非常简单的消息,在托管服务器米娜:TCP://本地主机:9990 (又名MyApp_A)。

我要的是发送包含在头一个字符串(这是Hellow世界!)一个简单的消息,并在体内的地址。

 公共类MyApp_B扩展主要{    公共静态最后弦乐MINA_HOST =米娜:TCP://本地主机:9991;    公共静态无效的主要(字符串参数... args)抛出异常{
        MyApp_B主要=新MyApp_B();        main.enableHangupSupport();        main.addRouteBuilder(
                新RouteBuilder(){
                    @覆盖
                    公共无效配置()抛出异常{                        从(直接:启动)
                        .setHeader(命令,常数(的Hello World!))
                        .setBody(常数(MINA_HOST))
                        。要(米娜:TCP://本地主机:9990);
                    }
                }
                );        的System.out.println(启动骆驼MyApp_B使用Ctrl + C终止JVM \\ n。);
        main.run();
    }
}


 公共类MainApp_A {    公共静态无效的主要(字符串参数... args)抛出异常{
        主力主攻=新的Main();
        main.enableHangupSupport();
        main.addRouteBuilder(新RouteBuilder(){            @覆盖
            公共无效配置()抛出异常{
                从(米娜:TCP://本地主机:9990).bean(MyRecipientListBean.class,
                        updateServers),以(直接:调试);                从(直接:调试)。进程(新处理器(){
                    公共无效过程(外汇兑换)抛出异常{
                        的System.out.println(收到命令:+
                                。exchange.getIn()getBody());
                    }
                });            }        });
        main.run(参数);
    }}


使用MyApp_A

豆:

 公共类MyRecipientListBean {    公共最后静态字符串REMOVE_SERVER =删除;
    公共最后静态字符串ADD_SERVER =增加;    私人设置<串GT;服务器=新的HashSet<串GT;();    公共无效updateServers(@Body字符串serverURI,
            @Header(命令)字符串顺序){
        的System.out.println(=========================================== ==== \\ n);
        的System.out.println(收到+ +订单的要求,从服务器的+ serverURI +\\ n);
        的System.out.println(=========================================== ==== \\ n);        如果(order.equals(ADD_SERVER))
            servers.add(serverURI);
        否则,如果(order.equals(REMOVE_SERVER))
            servers.remove(serverURI);
    }
}

我已经这样做了code然而,在另一边的服务器似乎,没有收到任何东西。因此,我有2个问题:


  1. 难道我做错了什么?

  2. 有没有更好的办法用骆驼来发送简单的消息?


解决方案

MyApp_A不发送任何消息。您需要发送消息到端点直接启动路线。

您也可以直接更改为一个定时器组件有它触发每隔X秒等。

的要求,增加了最新评论:


  

是和直接的路线也运行。它只是发送
  消息来指导,您需要使用骆驼的事情。直接是一个
  内部组件骆驼为它的端点之间发送消息
  (路由)。要发送消息给它,你可以使用生产商的模板。
  看到骆驼第7章,第7.7节在行动的书。


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

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?

解决方案

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.

这篇关于Apache的骆驼发送一个简单的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆