1
votes

I've got the following camel route which listens for messages on an ActiveMQ topic and immediately sends them to all connected web socket clients. This is working fine, but the connection to the topic is made as soon as the route builder is initialised.

from("activemq:topic:mytopic").routeId("routeid").to("websocket://test?sendToAll=true");

What I need is to only connect to the topic when one or more clients are connected to the web socket. Once there are no more connections I want to stop listening on the topic. Is this possible?

1

1 Answers

1
votes

According to me there is no proper way to do this. The only way this can be achieved is override Jetty WebSocket code. Once you override Jetty Websocket code you get the flexibility to write your own custom code in open and close websocket.

  • Maintain a List for all websocket clients in open websocket. Check for close websocket and remove it from the list to know how many are connected or disconnected. Or keep a counter on open and close websocket.
  • Once all websocket clients get closed suspend the route so that your messages stay in the topic or queue.
  • If any client gets connected to websocket, resume the route so that the messages reach the particular client connected.