Search in sources :

Example 1 with ListenerMessageCodec

use of com.hazelcast.client.spi.impl.ListenerMessageCodec in project hazelcast by hazelcast.

the class ClientSmartListenerService method deregisterListener.

@Override
public boolean deregisterListener(final String userRegistrationId) {
    Future<Boolean> future = registrationExecutor.submit(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            ClientRegistrationKey key = new ClientRegistrationKey(userRegistrationId);
            Map<Connection, ClientEventRegistration> registrationMap = registrations.get(key);
            if (registrationMap == null) {
                return false;
            }
            boolean successful = true;
            for (ClientEventRegistration registration : registrationMap.values()) {
                Connection subscriber = registration.getSubscriber();
                try {
                    ListenerMessageCodec listenerMessageCodec = registration.getCodec();
                    String serverRegistrationId = registration.getServerRegistrationId();
                    ClientMessage request = listenerMessageCodec.encodeRemoveRequest(serverRegistrationId);
                    new ClientInvocation(client, request, subscriber).invoke().get();
                    removeEventHandler(registration.getCallId());
                    registrationMap.remove(subscriber);
                } catch (Exception e) {
                    successful = false;
                    logger.warning("Deregistration of listener with id " + userRegistrationId + " has failed to address " + subscriber.getEndPoint(), e);
                }
            }
            if (successful) {
                registrations.remove(key);
            }
            return successful;
        }
    });
    try {
        return future.get();
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : ClientConnection(com.hazelcast.client.connection.nio.ClientConnection) Connection(com.hazelcast.nio.Connection) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) ListenerMessageCodec(com.hazelcast.client.spi.impl.ListenerMessageCodec) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ListenerMessageCodec

use of com.hazelcast.client.spi.impl.ListenerMessageCodec in project hazelcast by hazelcast.

the class ClientNearCacheInvalidationTest method registerInvalidationListener.

private void registerInvalidationListener(AtomicInteger counter) {
    EventHandler handler = new NearCacheRepairingHandler(counter);
    ListenerMessageCodec listenerCodec = createInvalidationListenerCodec();
    final HazelcastClientInstanceImpl clientInstance = testContext.client.client;
    clientInstance.getListenerService().registerListener(listenerCodec, handler);
}
Also used : ListenerMessageCodec(com.hazelcast.client.spi.impl.ListenerMessageCodec) EventHandler(com.hazelcast.client.spi.EventHandler) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl)

Example 3 with ListenerMessageCodec

use of com.hazelcast.client.spi.impl.ListenerMessageCodec in project hazelcast by hazelcast.

the class AbstractClientInternalCacheProxy method registerInvalidationListener.

private void registerInvalidationListener() {
    if (nearCache == null || !nearCache.isInvalidatedOnChange()) {
        return;
    }
    ListenerMessageCodec listenerCodec = createInvalidationListenerCodec();
    ClientListenerService listenerService = clientContext.getListenerService();
    EventHandler eventHandler = createInvalidationEventHandler();
    nearCacheMembershipRegistrationId = listenerService.registerListener(listenerCodec, eventHandler);
}
Also used : ClientListenerService(com.hazelcast.client.spi.ClientListenerService) ListenerMessageCodec(com.hazelcast.client.spi.impl.ListenerMessageCodec) EventHandler(com.hazelcast.client.spi.EventHandler)

Example 4 with ListenerMessageCodec

use of com.hazelcast.client.spi.impl.ListenerMessageCodec in project hazelcast by hazelcast.

the class ClientSmartListenerService method invoke.

private void invoke(ClientRegistrationKey registrationKey, Connection connection) throws Exception {
    //This method should only be called from registrationExecutor
    assert (Thread.currentThread().getName().contains("eventRegistration"));
    Map<Connection, ClientEventRegistration> registrationMap = registrations.get(registrationKey);
    if (registrationMap.containsKey(connection)) {
        return;
    }
    ListenerMessageCodec codec = registrationKey.getCodec();
    ClientMessage request = codec.encodeAddRequest(true);
    EventHandler handler = registrationKey.getHandler();
    handler.beforeListenerRegister();
    ClientInvocation invocation = new ClientInvocation(client, request, connection);
    invocation.setEventHandler(handler);
    ClientInvocationFuture future = invocation.invokeUrgent();
    ClientMessage clientMessage;
    try {
        clientMessage = future.get();
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e, Exception.class);
    }
    String serverRegistrationId = codec.decodeAddResponse(clientMessage);
    handler.onListenerRegister();
    long correlationId = request.getCorrelationId();
    ClientEventRegistration registration = new ClientEventRegistration(serverRegistrationId, correlationId, connection, codec);
    registrationMap.put(connection, registration);
}
Also used : ListenerMessageCodec(com.hazelcast.client.spi.impl.ListenerMessageCodec) ClientConnection(com.hazelcast.client.connection.nio.ClientConnection) Connection(com.hazelcast.nio.Connection) EventHandler(com.hazelcast.client.spi.EventHandler) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 5 with ListenerMessageCodec

use of com.hazelcast.client.spi.impl.ListenerMessageCodec in project hazelcast by hazelcast.

the class ClientMapProxy method addEntryListenerInternal.

private String addEntryListenerInternal(ListenerAdapter listenerAdaptor, Predicate<K, V> predicate, K key, final boolean includeValue) {
    final int listenerFlags = setAndGetListenerFlags(listenerAdaptor);
    final Data keyData = toData(key);
    final Data predicateData = toData(predicate);
    EventHandler<ClientMessage> handler = createHandler(listenerAdaptor);
    ListenerMessageCodec codec = createEntryListenerToKeyWithPredicateCodec(includeValue, listenerFlags, keyData, predicateData);
    return registerListener(codec, handler);
}
Also used : ListenerMessageCodec(com.hazelcast.client.spi.impl.ListenerMessageCodec) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Aggregations

ListenerMessageCodec (com.hazelcast.client.spi.impl.ListenerMessageCodec)5 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 EventHandler (com.hazelcast.client.spi.EventHandler)3 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)2 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)2 HazelcastException (com.hazelcast.core.HazelcastException)2 Connection (com.hazelcast.nio.Connection)2 IOException (java.io.IOException)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)1 ClientListenerService (com.hazelcast.client.spi.ClientListenerService)1 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)1 Data (com.hazelcast.nio.serialization.Data)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1