user676567
user676567

Reputation: 1139

CometD Subscription Listeners

I’m having a problem processing Subscription Requests from Clients and carrying out some processing based on the request. I’d like to be able to invoke a method and carry out some processing when an incoming subscription request is received on the Server. I’ve had a look at the following CometD documentation and tried the example outlined in “Subscription Configuration Support” but I’m not having much luck.

http://www.cometd.org/documentation/2.x/cometd-java/server/services/annotated

I’ve already created the Bayeux Server using a Spring Bean and I’m able to publish data to other channel names I’ve created on the Server side. Any help or additional info. on the topic would be appreciated!

The code example I’m using:

@Service("CometDSubscriptionListener")
public class CometDSubscriptionListener {

    private final String channel = "/subscription";
    private static final Logger logger = Logger.getLogger(CometDSubscriptionListener.class);    
    private Heartbeat heartbeat;

    @Inject
    private BayeuxServer bayeuxserver; 

    @Session
    private ServerSession sender;

    public CometDSubscriptionListener(BayeuxServer bayeuxserver){       
        logger.info("CometDSubscriptionListener constructor called");       
    }

    @Subscription(channel)  
    public void processClientRequest(Message message)
    {   
        logger.info("Received request from client for channel " + channel);
        PublishData();  
    }

Upvotes: 1

Views: 1710

Answers (1)

sbordet
sbordet

Reputation: 18647

Have a look at the documentation for annotated services, and also to the CometD concepts.

If I read your question correctly, you want to be able to perform some logic when clients subscribe to a channel, not when messages arrive to that channel.

You're confusing the meaning of the @Subscription annotation, so read the links above that should clarify its semantic.

To do what I understood you want to do it, you need this:

@Service
public class CometDSubscriptionListener 
{
    ...

    @Listener(Channel.META_SUBSCRIBE)  
    public void processSubscription(ServerSession remote, ServerMessage message)
    {   
        // What channel the client wants to subscribe to ?
        String channel = (String)message.get(Message.SUBSCRIPTION_FIELD);

        // Do your logic here
    }
}

Upvotes: 4

Related Questions