如何通过apache骆驼路由将数据发送到netty4套接字

时间:2018-12-05 13:41:29

标签: java apache-camel netty-socketio

我有一条路线

                                                             '服务器已接收:'+ exchange.getIn()。getBody(String.class)                                        

我想使用一些Java客户端将数据发送到此套接字。我该怎么办?

1 个答案:

答案 0 :(得分:0)

“收到的服务器:” + exchange.getIn()。getBody(String.class)不是骆驼路线,它更多是打印正文内容的处理器。您需要定义一个CamelContext,使用自定义处理器添加路由

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
    public void configure() {

        from("direct:start")
        .process(new Processor() {
                      public void process(Exchange exchange) throws Exception {
                         String body = exchange.getIn().getBody(String.class);
                         System.out.println("Server received: " + body);
                    }
                  })
    }
});

ProducerTemplate template = context.createProducerTemplate();
context.start();
template.sendBody("direct:start", "Hello World");

RouteBuilder Camel doc