Apache Camel Content Based Routing on Websocket Connections

∥☆過路亽.° 提交于 2019-12-11 10:16:05

问题


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?


回答1:


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();

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 : producer endpoint name.

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

message : message to the websocket endpoint.

Edit : here is the producer route.

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();


来源:https://stackoverflow.com/questions/35485523/apache-camel-content-based-routing-on-websocket-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!