Skip navigation.

CometD 2 Java Server Inherited Services

CometD Java Server API: Inherited Services

A CometD inherited service is a Java class that extends the CometD class org.cometd.server.AbstractService, that specifies the Bayeux channels the service is interested to, and that adheres to the contract required by the AbstractService class:

public class EchoService extends AbstractService                                  (1)
{
    public EchoService(BayeuxServer bayeuxServer)                                 (2)
    {
        super(bayeuxServer, "echo");                                              (3)
        addService("/echo", "processEcho");                                       (4)
    }

    public void processEcho(ServerSession remote, Map<String, Object> data) (5)
    {
        remote.deliver(getServerSession(), "/echo", data, null);                  (6)
    }
}

This is a simple echo service that returns the message sent by the remote client on channel "/echo" to the remote client itself.

Note the following:

  • In (1) we extend from org.cometd.server.AbstractService
  • In (2) we create a constructor that takes a org.cometd.bayeux.server.BayeuxServer object
  • In (3) we call the superclass constructor, passing the BayeuxServer object and an arbitrary name of the service, in this case "echo"
  • In (4) we subscribe to channel "/echo", and we specify the name of a method that must be called when a message arrives to that channel
  • In (5) we define a method with the same name specified in (4), and with an appropriate signature (see below)
  • In (6) we use the org.cometd.bayeux.server.ServerSession API to echo the message back to that particular client

The contract that the BayeuxService class requires for callback methods is that the methods must have one of the following signatures:

// Obtains the remote session object and the message object
public void processEcho(ServerSession remote, Message message)

// Obtains the remote session object and the message's data object
// (additional message information, such as the channel or the id is lost)
public void processEcho(ServerSession remote, Map<String, Object> data)

// Obtains the remote session object, the channel name, the message object and the message id
public void processEcho(ServerSession remote, String channelName, Message message, String messageId)

// Obtains the remote session object, the channel name, the message's data object and the message id
public void processEcho(ServerSession remote, String channelName, Map<String, Object> data, String messageId)

Note that the channel name specified in the subscribe() method can be a wildcard, for example:

public class BaseballTeamService extends AbstractService
{
    public BaseballTeamService(BayeuxServer bayeux)
    {
        super(bayeux, "baseballTeam");
        addService("/baseball/team/*", "processBaseballTeam");
    }

    public void processBaseballTeam(ServerSession remote, String channelName, Map<String, Object> data, String messageId)
    {
        // Upon receiving a message on channel /baseball/team/*, forward to channel /events/baseball/team/*
        getBayeux().getChannel("/events" + channelName).publish(getServerSession(), data, null);
    }
}

Note also how in the first example we used ServerSession.deliver() to send a message to a particular remote client, while in the second we used ServerChannel.publish() to send a message to anyone who subscribed to channel "/events/baseball/team/*".

Once you have written your Bayeux services it is time to setup them in your web application, plain style or Spring style.