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

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

问题描述

我有一个假设场景:假设我有一个 Apache Camel websocket 服务器,并且我允许许多 websocket 连接.每个客户端连接都需要与一个 ClientID 相关联.ClientID 是通过新连接通过 InitConnection json 消息获得的,其中 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();

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

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