Websocket连接上基于Apache Camel内容的路由 [英] Apache Camel Content Based Routing on Websocket Connections

查看:360
本文介绍了Websocket连接上基于Apache Camel内容的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个假设的场景:假设我有一个Apache Camel websocket服务器,并且允许许多websocket连接.每个客户端连接都需要与一个ClientID相关联.新的连接通过InitConnection json消息获取ClientID,其中ClientID是消息的成员.问题是:是否可以让骆驼将websocket实例与ClientID相关联,以执行基于内容的路由?

I have a hypothetical scenario: let’s pretend I have an Apache Camel websocket server and I’m allowing many websocket connections. Each client connection will need to be associated with a ClientID. The ClientID is obtained by a new connection via an InitConnection json message where a ClientID is a member of the message. The question is: is it possible to have camel associate a websocket instance with a ClientID in order to perform content based routing?

推荐答案

是的,有可能.您可以通过以下方法检索每个客户端的UUID:

yes, It is possible. you can retrieve a UUID of each client by below method:

from("direct:Consumer1")
    .process(new Processor() {
     public void process(Exchange exchange) throws Exception {
       Map<String, Object> headers=exchange.getIn().getHeaders();
    //you can get a unique connection key from the exchange header.
    //store this key somewhere, to send messages to particular client.
    String uniqueConnectionKey=headers.get("websocket.connectionKey").toString();
          //you can get message from the client like below.
          String dataFromClient=exchange.getIn().getBody().toString();

   }
}).end();

您需要将此唯一密钥与您的客户端ID映射,以便您可以使用此UUID向特定的客户端发送消息.

you need to map this unique key with ur client id, so that u can send messages to particular client using this UUID.

 CamelContext camelContext=new DefaultCamelContext();
   ProducerTemplate template=camelContext.createProducerTemplate();
   template.sendBodyAndHeader("direct:Producer1", {message}, "connectionKey",    {connectionkey});

direct:Producer1:生产者端点名称.

direct:Producer1 : producer endpoint name.

connectionkey:一个唯一的连接密钥,您将从websocket使用者中的交换头获得.

connectionkey : a unique connection key, which you will get from the exchange header in websocket consumer.

message:发送到websocket端点的消息.

message : message to the websocket endpoint.

这是生产者路线.

from("direct:Producer1").
      //we will use this connectionKey for uniquely identifying each connection from the client.
      setHeader(WebsocketConstants.CONNECTION_KEY, header("connectionKey")).
      to("websocket://{host}:{port}/camel-websocket?sendToAll=false").end();

这篇关于Websocket连接上基于Apache Camel内容的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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