Removing Listeners¶
To stop receiving messages listeners can be removed using the following API:
Removing a specific listener object¶
The object must be the same as the one added with the method which adds the listener:
//channel is received as a result of the subscribe() method
channel.removeMessageListener( AsyncCallback<?> listener );
Removing a specific listener for a selector¶
The listener object must be the same as the one added with the method which adds the listener:
//channel is received as a result of the subscribe() method
channel.removeMessageListeners( String selector, AsyncCallback<?> listener );
Removing all message listeners for a selector¶
//channel is received as a result of the subscribe() method
channel.removeMessageListeners( String selector );
Remove all listeners for a channel¶
channel.removeAllMessageListeners();
Example¶
The following example declares a messaging listener, adds and then removes it. The example as shown does not provide any subscription functionality as the listener is removed right after it is added, however, it clearly demonstrates how a listener can be removed from a channel:
// subscribe to a channel
final Channel channel = Backendless.Messaging.subscribe( "MyChannel" );
// declare a listener
AsyncCallback<String> callback = new AsyncCallback<String>()
{
@Override
public void handleResponse( String stringMessage )
{
System.out.println( "got string message " + stringMessage );
}
@Override
public void handleFault( BackendlessFault fault )
{
System.out.println( "got error " + fault );
}
};
// add the listener to the channel
channel.addMessageListener( callback );
// the following call removes the listener
channel.removeMessageListener( callback );