Search in sources :

Example 31 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class ClientHeartbeatMonitor method monitor.

private void monitor(String memberUuid, ClientEndpointImpl clientEndpoint) {
    if (clientEndpoint.isFirstConnection() && ClientType.CPP.equals(clientEndpoint.getClientType())) {
        return;
    }
    Connection connection = clientEndpoint.getConnection();
    long lastTimePacketReceived = connection.lastReadTimeMillis();
    long timeoutInMillis = SECONDS.toMillis(heartbeatTimeoutSeconds);
    long currentTimeMillis = Clock.currentTimeMillis();
    if (lastTimePacketReceived + timeoutInMillis < currentTimeMillis) {
        if (memberUuid.equals(clientEngine.getOwnerUuid(clientEndpoint.getUuid()))) {
            String message = "Client heartbeat is timed out, closing connection to " + connection + ". Now: " + timeToString(currentTimeMillis) + ". LastTimePacketReceived: " + timeToString(lastTimePacketReceived);
            connection.close(message, null);
        }
    }
}
Also used : Connection(com.hazelcast.nio.Connection) StringUtil.timeToString(com.hazelcast.util.StringUtil.timeToString)

Example 32 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class NearCachedClientMapProxy method getConnectedServerVersion.

private int getConnectedServerVersion() {
    ClientContext clientContext = getClientContext();
    ClientClusterService clusterService = clientContext.getClusterService();
    Address ownerConnectionAddress = clusterService.getOwnerConnectionAddress();
    HazelcastClientInstanceImpl client = getClient();
    ClientConnectionManager connectionManager = client.getConnectionManager();
    Connection connection = connectionManager.getConnection(ownerConnectionAddress);
    if (connection == null) {
        logger.warning(format("No owner connection is available, " + "near cached cache %s will be started in legacy mode", name));
        return UNKNOWN_HAZELCAST_VERSION;
    }
    return ((ClientConnection) connection).getConnectedServerVersion();
}
Also used : Address(com.hazelcast.nio.Address) ClientContext(com.hazelcast.client.spi.ClientContext) ClientConnection(com.hazelcast.client.connection.nio.ClientConnection) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) ClientConnection(com.hazelcast.client.connection.nio.ClientConnection) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ClientClusterService(com.hazelcast.client.spi.ClientClusterService)

Example 33 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class ClientConnectionManagerImpl method getOrConnect.

@Override
public Connection getOrConnect(Address address, boolean asOwner) throws IOException {
    try {
        while (true) {
            Connection connection = getConnection(address, asOwner);
            if (connection != null) {
                return connection;
            }
            AuthenticationFuture firstCallback = triggerConnect(addressTranslator.translate(address), asOwner);
            connection = firstCallback.get(connectionTimeout);
            if (!asOwner) {
                return connection;
            }
            if (firstCallback.authenticatedAsOwner) {
                return connection;
            }
        }
    } catch (Throwable e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : Connection(com.hazelcast.nio.Connection)

Example 34 with Connection

use of com.hazelcast.nio.Connection in project hazelcast by hazelcast.

the class ClientNonSmartListenerService method invoke.

private ClientEventRegistration invoke(ClientRegistrationKey registrationKey) throws Exception {
    EventHandler handler = registrationKey.getHandler();
    handler.beforeListenerRegister();
    ClientMessage request = registrationKey.getCodec().encodeAddRequest(false);
    ClientInvocation invocation = new ClientInvocation(client, request);
    invocation.setEventHandler(handler);
    ClientInvocationFuture future = invocation.invoke();
    String registrationId = registrationKey.getCodec().decodeAddResponse(future.get());
    handler.onListenerRegister();
    Connection connection = future.getInvocation().getSendConnection();
    return new ClientEventRegistration(registrationId, request.getCorrelationId(), connection, registrationKey.getCodec());
}
Also used : 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) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 35 with Connection

use of com.hazelcast.nio.Connection 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)

Aggregations

Connection (com.hazelcast.nio.Connection)59 Address (com.hazelcast.nio.Address)15 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)10 Test (org.junit.Test)9 QuickTest (com.hazelcast.test.annotation.QuickTest)7 IOException (java.io.IOException)7 ClientConnectionManager (com.hazelcast.client.connection.ClientConnectionManager)6 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)6 AssertTask (com.hazelcast.test.AssertTask)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 Packet (com.hazelcast.nio.Packet)5 ClientClusterService (com.hazelcast.client.spi.ClientClusterService)4 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)4 Member (com.hazelcast.core.Member)4 Node (com.hazelcast.instance.Node)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 EventHandler (com.hazelcast.client.spi.EventHandler)2 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)2