Search in sources :

Example 1 with ListenerMessageCodec

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

the class ClientCacheClearTest method registerInvalidationListener.

private void registerInvalidationListener(EventHandler handler, String nameWithPrefix) {
    ListenerMessageCodec listenerCodec = createInvalidationListenerCodec(nameWithPrefix);
    HazelcastClientProxy hzClient = (HazelcastClientProxy) client;
    final HazelcastClientInstanceImpl clientInstance = hzClient.client;
    clientInstance.getListenerService().registerListener(listenerCodec, handler);
}
Also used : ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy)

Example 2 with ListenerMessageCodec

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

the class ClientMapProxy method addEntryListenerInternal.

private UUID addEntryListenerInternal(@Nonnull ListenerAdapter<IMapEvent> listenerAdaptor, @Nonnull Predicate<K, V> predicate, @Nullable K key, boolean includeValue) {
    int listenerFlags = setAndGetListenerFlags(listenerAdaptor);
    Data keyData = toData(key);
    Data predicateData = toData(predicate);
    EventHandler<ClientMessage> handler = new ClientMapToKeyWithPredicateEventHandler(listenerAdaptor);
    ListenerMessageCodec codec = createEntryListenerToKeyWithPredicateCodec(includeValue, listenerFlags, keyData, predicateData);
    return registerListener(codec, handler);
}
Also used : ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) Data(com.hazelcast.internal.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 3 with ListenerMessageCodec

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

the class ClientListenerServiceImpl method deregisterListenerInternal.

private Boolean deregisterListenerInternal(@Nullable UUID userRegistrationId) {
    // This method should only be called from registrationExecutor
    assert (Thread.currentThread().getName().contains("eventRegistration"));
    ClientListenerRegistration listenerRegistration = registrations.remove(userRegistrationId);
    if (listenerRegistration == null) {
        return false;
    }
    Map<Connection, ClientConnectionRegistration> registrations = listenerRegistration.getConnectionRegistrations();
    CompletableFuture[] futures = new CompletableFuture[registrations.size()];
    int i = 0;
    for (Map.Entry<Connection, ClientConnectionRegistration> entry : registrations.entrySet()) {
        ClientConnectionRegistration registration = entry.getValue();
        ClientConnection subscriber = (ClientConnection) entry.getKey();
        // remove local handler
        subscriber.removeEventHandler(registration.getCallId());
        // the rest is for deleting remote registration
        ListenerMessageCodec listenerMessageCodec = listenerRegistration.getCodec();
        UUID serverRegistrationId = registration.getServerRegistrationId();
        ClientMessage request = listenerMessageCodec.encodeRemoveRequest(serverRegistrationId);
        if (request == null) {
            futures[i++] = CompletableFuture.completedFuture(null);
            continue;
        }
        ClientInvocation clientInvocation = new ClientInvocation(client, request, null, subscriber);
        clientInvocation.setInvocationTimeoutMillis(Long.MAX_VALUE);
        futures[i++] = clientInvocation.invokeUrgent().exceptionally(throwable -> {
            if (!(throwable instanceof HazelcastClientNotActiveException || throwable instanceof IOException || throwable instanceof TargetDisconnectedException)) {
                logger.warning("Deregistration of listener with ID " + userRegistrationId + " has failed for address " + subscriber.getRemoteAddress(), throwable);
            }
            return null;
        });
    }
    CompletableFuture.allOf(futures).join();
    return true;
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) MetricsRegistry(com.hazelcast.internal.metrics.MetricsRegistry) MANDATORY(com.hazelcast.internal.metrics.ProbeLevel.MANDATORY) ClientExecutionServiceImpl(com.hazelcast.client.impl.spi.impl.ClientExecutionServiceImpl) CompletableFuture(java.util.concurrent.CompletableFuture) ClientListenerService(com.hazelcast.client.impl.spi.ClientListenerService) EmptyStatement(com.hazelcast.internal.util.EmptyStatement) EventHandler(com.hazelcast.client.impl.spi.EventHandler) SingleExecutorThreadFactory(com.hazelcast.internal.util.executor.SingleExecutorThreadFactory) ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) Future(java.util.concurrent.Future) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClientProperty(com.hazelcast.client.properties.ClientProperty) ILogger(com.hazelcast.logging.ILogger) CLIENT_METRIC_LISTENER_SERVICE_EVENTS_PROCESSED(com.hazelcast.internal.metrics.MetricDescriptorConstants.CLIENT_METRIC_LISTENER_SERVICE_EVENTS_PROCESSED) Map(java.util.Map) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) UuidUtil(com.hazelcast.internal.util.UuidUtil) ThreadFactory(java.util.concurrent.ThreadFactory) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) ConnectionListener(com.hazelcast.internal.nio.ConnectionListener) HazelcastException(com.hazelcast.core.HazelcastException) Connection(com.hazelcast.internal.nio.Connection) Probe(com.hazelcast.internal.metrics.Probe) StripedRunnable(com.hazelcast.internal.util.executor.StripedRunnable) Collection(java.util.Collection) HazelcastProperties(com.hazelcast.spi.properties.HazelcastProperties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExceptionUtil(com.hazelcast.internal.util.ExceptionUtil) CLIENT_METRIC_LISTENER_SERVICE_EVENT_QUEUE_SIZE(com.hazelcast.internal.metrics.MetricDescriptorConstants.CLIENT_METRIC_LISTENER_SERVICE_EVENT_QUEUE_SIZE) CLIENT_PREFIX_LISTENERS(com.hazelcast.internal.metrics.MetricDescriptorConstants.CLIENT_PREFIX_LISTENERS) IOException(java.io.IOException) UUID(java.util.UUID) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) Executors(java.util.concurrent.Executors) StaticMetricsProvider(com.hazelcast.internal.metrics.StaticMetricsProvider) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture) StripedExecutor(com.hazelcast.internal.util.executor.StripedExecutor) Collections(java.util.Collections) ClientConnectionManager(com.hazelcast.client.impl.connection.ClientConnectionManager) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) Connection(com.hazelcast.internal.nio.Connection) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) IOException(java.io.IOException) ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) CompletableFuture(java.util.concurrent.CompletableFuture) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with ListenerMessageCodec

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

the class ClientListenerServiceImpl method invoke.

protected void invoke(ClientListenerRegistration listenerRegistration, Connection connection) throws Exception {
    // This method should only be called from registrationExecutor
    assert (Thread.currentThread().getName().contains("eventRegistration"));
    if (listenerRegistration.getConnectionRegistrations().containsKey(connection)) {
        return;
    }
    ListenerMessageCodec codec = listenerRegistration.getCodec();
    ClientMessage request = codec.encodeAddRequest(registersLocalOnly());
    EventHandler handler = listenerRegistration.getHandler();
    if (logger.isFinestEnabled()) {
        logger.finest("Register attempt of " + listenerRegistration + " to " + connection);
    }
    handler.beforeListenerRegister(connection);
    ClientInvocation invocation = new ClientInvocation(client, request, null, connection);
    invocation.setEventHandler(handler);
    ClientInvocationFuture future = invocation.invokeUrgent();
    ClientMessage clientMessage;
    try {
        clientMessage = future.get();
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e, Exception.class);
    }
    UUID serverRegistrationId = codec.decodeAddResponse(clientMessage);
    if (logger.isFinestEnabled()) {
        logger.finest("Registered " + listenerRegistration + " to " + connection);
    }
    handler.onListenerRegister(connection);
    long correlationId = request.getCorrelationId();
    ClientConnectionRegistration registration = new ClientConnectionRegistration(serverRegistrationId, correlationId);
    listenerRegistration.getConnectionRegistrations().put(connection, registration);
}
Also used : ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) EventHandler(com.hazelcast.client.impl.spi.EventHandler) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) UUID(java.util.UUID) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture)

Aggregations

ListenerMessageCodec (com.hazelcast.client.impl.spi.impl.ListenerMessageCodec)4 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)2 EventHandler (com.hazelcast.client.impl.spi.EventHandler)2 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)2 ClientInvocationFuture (com.hazelcast.client.impl.spi.impl.ClientInvocationFuture)2 HazelcastException (com.hazelcast.core.HazelcastException)2 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)2 IOException (java.io.IOException)2 UUID (java.util.UUID)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 HazelcastClientProxy (com.hazelcast.client.impl.clientside.HazelcastClientProxy)1 ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)1 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)1 ClientListenerService (com.hazelcast.client.impl.spi.ClientListenerService)1 ClientExecutionServiceImpl (com.hazelcast.client.impl.spi.impl.ClientExecutionServiceImpl)1 ClientProperty (com.hazelcast.client.properties.ClientProperty)1 CLIENT_METRIC_LISTENER_SERVICE_EVENTS_PROCESSED (com.hazelcast.internal.metrics.MetricDescriptorConstants.CLIENT_METRIC_LISTENER_SERVICE_EVENTS_PROCESSED)1 CLIENT_METRIC_LISTENER_SERVICE_EVENT_QUEUE_SIZE (com.hazelcast.internal.metrics.MetricDescriptorConstants.CLIENT_METRIC_LISTENER_SERVICE_EVENT_QUEUE_SIZE)1