Search in sources :

Example 6 with ClientConnection

use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.

the class ClientTransactionManagerServiceImpl method connect.

public ClientConnection connect() throws Exception {
    ClientInvocationServiceImpl invocationService = (ClientInvocationServiceImpl) client.getInvocationService();
    long startTimeMillis = System.currentTimeMillis();
    long invocationTimeoutMillis = invocationService.getInvocationTimeoutMillis();
    ClientConfig clientConfig = client.getClientConfig();
    boolean smartRouting = clientConfig.getNetworkConfig().isSmartRouting();
    while (client.getLifecycleService().isRunning()) {
        try {
            ClientConnection connection = client.getConnectionManager().getRandomConnection();
            if (connection == null) {
                throw throwException(smartRouting);
            }
            return connection;
        } catch (Exception e) {
            if (e instanceof HazelcastClientOfflineException) {
                throw e;
            }
            if (System.currentTimeMillis() - startTimeMillis > invocationTimeoutMillis) {
                throw newOperationTimeoutException(e, invocationTimeoutMillis, startTimeMillis);
            }
        }
        Thread.sleep(invocationService.getInvocationRetryPauseMillis());
    }
    throw new HazelcastClientNotActiveException();
}
Also used : HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) TransactionException(com.hazelcast.transaction.TransactionException) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) HazelcastClientNotActiveException(com.hazelcast.client.HazelcastClientNotActiveException)

Example 7 with ClientConnection

use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.

the class ClientListenerServiceImpl method handleEventMessageOnCallingThread.

public void handleEventMessageOnCallingThread(ClientMessage clientMessage) {
    long correlationId = clientMessage.getCorrelationId();
    ClientConnection connection = (ClientConnection) clientMessage.getConnection();
    EventHandler eventHandler = connection.getEventHandler(correlationId);
    if (eventHandler == null) {
        if (logger.isFineEnabled()) {
            logger.fine("No eventHandler for callId: " + correlationId + ", event: " + clientMessage);
        }
        return;
    }
    eventHandler.handle(clientMessage);
}
Also used : EventHandler(com.hazelcast.client.impl.spi.EventHandler) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection)

Example 8 with ClientConnection

use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.

the class TcpClientConnectionManager method getRandomConnection.

@Override
public ClientConnection getRandomConnection() {
    // Try getting the connection from the load balancer, if smart routing is enabled
    if (isSmartRoutingEnabled) {
        Member member = loadBalancer.next();
        // Failed to get a member
        ClientConnection connection = member != null ? activeConnections.get(member.getUuid()) : null;
        if (connection != null) {
            return connection;
        }
    }
    // Otherwise iterate over connections and return the first one
    for (Map.Entry<UUID, TcpClientConnection> connectionEntry : activeConnections.entrySet()) {
        return connectionEntry.getValue();
    }
    // Failed to get a connection
    return null;
}
Also used : ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 9 with ClientConnection

use of com.hazelcast.client.impl.connection.ClientConnection 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 10 with ClientConnection

use of com.hazelcast.client.impl.connection.ClientConnection in project hazelcast by hazelcast.

the class ClientInvocation method getPermissionToNotify.

boolean getPermissionToNotify(long responseCorrelationId) {
    ClientConnection conn = this.sentConnection;
    if (conn == null) {
        // we don't need to take action
        return false;
    }
    long requestCorrelationId = clientMessage.getCorrelationId();
    if (responseCorrelationId != requestCorrelationId) {
        // we should not notify
        return false;
    }
    // otherwise another thread is handling it, we don't need to notify anymore
    return SENT_CONNECTION.compareAndSet(this, conn, null);
}
Also used : ClientConnection(com.hazelcast.client.impl.connection.ClientConnection)

Aggregations

ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)15 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 Test (org.junit.Test)5 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 EventHandler (com.hazelcast.client.impl.spi.EventHandler)3 Map (java.util.Map)3 UUID (java.util.UUID)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)2 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)2 Member (com.hazelcast.cluster.Member)2 Connection (com.hazelcast.internal.nio.Connection)2 QueryId (com.hazelcast.sql.impl.QueryId)2 Nonnull (javax.annotation.Nonnull)2 HazelcastClientOfflineException (com.hazelcast.client.HazelcastClientOfflineException)1 ClientConfig (com.hazelcast.client.config.ClientConfig)1 SqlExecuteCodec (com.hazelcast.client.impl.protocol.codec.SqlExecuteCodec)1 SqlFetchCodec (com.hazelcast.client.impl.protocol.codec.SqlFetchCodec)1