A new feature that has been recently added to the upcoming CometD 2.1.0 release is
the support for annotated services, both on server side and on client side.

Services are the heart of a CometD application, because allow to write business logic that is executed when a message
arrives on a particular channel.
For example, in chat applications when a new member joins a chat room, the chat room’s members list is broadcasted to all
existing chat room members.

This is how an annotated service looks like:

@Service("echoService")
public class EchoService
{
@Inject
private BayeuxServer bayeux;
@Session
private ServerSession serverSession;

@PostConstruct
public void init()
{
System.out.println("Echo Service Initialized");
}

@Listener("/echo")
public void echo(ServerSession remote, ServerMessage.Mutable message)
{
String channel = message.getChannel();
Object data = message.getData();
remote.deliver(serverSession, channel, data, null);
}
}

Annotations are great because they carry a semantic meaning to their annotation target.
Even if you have not written a CometD service before, the code above is pretty straightforward, and it is easy
to understand that the echo() method is invoked when a message arrives on the "/echo" channel.

Annotated services also reduce the amount of boilerplate code, especially on client side, where it is typical
to add listeners and subscribers as anonymous inner classes.

Compare:

bayeuxClient.getChannel(Channel.META_CONNECT).addListener(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
// Connect handling...
}
});

with:

@Listener(Channel.META_CONNECT)
public void metaConnect(Message connect)
{
// Connect handling...
}

I find the second version much more readable, and since code is much more read than written, this is a fine improvement.

Lastly, CometD annotated services leverage the standard annotations for dependency injection and lifecycle management,
and therefore integrate nicely with Spring 3.x.

Take a look at the annotated services documentation
and to the CometD 2 Spring integration for details.

Enjoy !

CometD 2 Annotated Services