Search code examples
javaservercometdbayeux

Is it possible to create 2 or more BayeuxServer instances in the same server


I have a jetty server running cometd. I would like to have 2 instances of Bayeux server at the same time running under different servlet paths.

Reason is I need to configure one of the instances with few extensions while the other should have no extension.

Is this achievable in some way?

UPDATE Just tried with recommendation from sbordet from a new project started using primer section from the docu. I used Cometd 5. Result is that BayeuxServer is created only once even when configured twice in web.xml because CometDServlet which is in charge of initialising BayeuxServer has some singleton-like code:

this._bayeux = (BayeuxServerImpl)this.getServletContext().getAttribute("org.cometd.bayeux");
      if (this._bayeux == null) {
        export = true;
        this._bayeux = this.newBayeuxServer();....

So once the server is created it seems to be stored on the Servlet Context attribute and on subsequent calls seems to be retrieved from there. This impede me to have two different configured servers as I expected.


Solution

  • Yes it is possible to have two (or more) different BayeuxServer instances on the same server, under different Servlet mappings.

    You just need to configure two CometDServlets in web.xml, or configure two CometDHandlers (in CometD 8) under different mappings as children of a PathMappingsHandler.

    On the client, you need to create two client instances pointing to the different URIs, depending on what BayeuxServer you want to hit.

    Note that the BayeuxServers will be separated: each will have its own sessions, channels, subscribers, listeners, etc.

    The two BayeuxServers can communicate server-to-server using a Java CometD client.

    UPDATE

    By default, the BayeuxServer instance is stored as a Servlet context attribute, which does not allow two different instances to be created.

    However, being this a special case, it should not be difficult to write a Servlet similar to CometDServlet that creates an instance without looking at the Servlet context.

    I agree that this should be provided by CometD. I filed https://github.com/cometd/cometd/issues/1782 to fix this issue.