Search in sources :

Example 1 with ClientConnectionManager

use of com.hazelcast.client.connection.ClientConnectionManager in project hazelcast by hazelcast.

the class AbstractClientInternalCacheProxy method getConnectedServerVersion.

private int getConnectedServerVersion() {
    ClientContext clientContext = getContext();
    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 2 with ClientConnectionManager

use of com.hazelcast.client.connection.ClientConnectionManager in project hazelcast by hazelcast.

the class ClientConnectionTest method destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce.

@Test
public void destroyConnection_whenDestroyedMultipleTimes_thenListenerRemoveCalledOnce() {
    HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    HazelcastClientInstanceImpl clientImpl = ClientTestUtil.getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountingConnectionRemoveListener listener = new CountingConnectionRemoveListener();
    connectionManager.addConnectionListener(listener);
    final Address serverAddress = new Address(server.getCluster().getLocalMember().getSocketAddress());
    final Connection connectionToServer = connectionManager.getConnection(serverAddress);
    final CountDownLatch isConnected = new CountDownLatch(1);
    clientImpl.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                isConnected.countDown();
            }
        }
    });
    connectionToServer.close(null, null);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(isConnected.await(5, TimeUnit.SECONDS));
        }
    });
    connectionToServer.close(null, null);
    assertEquals("connection removed should be called only once", 1, listener.count.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleEvent(com.hazelcast.core.LifecycleEvent) AssertTask(com.hazelcast.test.AssertTask) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ExecutionException(java.util.concurrent.ExecutionException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with ClientConnectionManager

use of com.hazelcast.client.connection.ClientConnectionManager in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testHeartbeatStoppedEvent.

@Test
public void testHeartbeatStoppedEvent() throws InterruptedException {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {

        @Override
        public void heartbeatResumed(Connection connection) {
        }

        @Override
        public void heartbeatStopped(Connection connection) {
            countDownLatch.countDown();
        }
    });
    blockMessagesFromInstance(instance, client);
    assertOpenEventually(countDownLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) ConnectionHeartbeatListener(com.hazelcast.client.spi.impl.ConnectionHeartbeatListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with ClientConnectionManager

use of com.hazelcast.client.connection.ClientConnectionManager in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testHeartbeatResumedEvent.

@Test
public void testHeartbeatResumedEvent() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();
    // make sure client is connected to instance2
    String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    IMap<String, String> map = client.getMap(randomString());
    map.put(keyOwnedByInstance2, randomString());
    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    final ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {

        @Override
        public void heartbeatResumed(Connection connection) {
            assertEquals(instance2.getCluster().getLocalMember().getAddress(), connection.getEndPoint());
            countDownLatch.countDown();
        }

        @Override
        public void heartbeatStopped(Connection connection) {
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertNotNull(connectionManager.getConnection(instance2.getCluster().getLocalMember().getAddress()));
        }
    });
    blockMessagesFromInstance(instance2, client);
    sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
    unblockMessagesFromInstance(instance2, client);
    assertOpenEventually(countDownLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) AssertTask(com.hazelcast.test.AssertTask) Matchers.containsString(org.hamcrest.Matchers.containsString) ConnectionHeartbeatListener(com.hazelcast.client.spi.impl.ConnectionHeartbeatListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager) ExpectedException(org.junit.rules.ExpectedException) ExecutionException(java.util.concurrent.ExecutionException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ClientConnectionManager

use of com.hazelcast.client.connection.ClientConnectionManager in project hazelcast by hazelcast.

the class ClientTestSupport method unblockMessagesToInstance.

/**
     * Unblocks outgoing messages from client to given instance
     */
protected void unblockMessagesToInstance(HazelcastInstance instance, HazelcastInstance client) {
    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    Address address = instance.getCluster().getLocalMember().getAddress();
    ((TestClientRegistry.MockClientConnectionManager) connectionManager).unblockTo(address);
}
Also used : Address(com.hazelcast.nio.Address) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) ClientConnectionManager(com.hazelcast.client.connection.ClientConnectionManager)

Aggregations

ClientConnectionManager (com.hazelcast.client.connection.ClientConnectionManager)13 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)10 Address (com.hazelcast.nio.Address)10 Connection (com.hazelcast.nio.Connection)6 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 AssertTask (com.hazelcast.test.AssertTask)3 InetSocketAddress (java.net.InetSocketAddress)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClientClusterService (com.hazelcast.client.spi.ClientClusterService)2 ClientContext (com.hazelcast.client.spi.ClientContext)2 ConnectionHeartbeatListener (com.hazelcast.client.spi.impl.ConnectionHeartbeatListener)2 ExecutionException (java.util.concurrent.ExecutionException)2 ClientImpl (com.hazelcast.client.impl.ClientImpl)1 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)1 ClientMessageReadHandler (com.hazelcast.client.impl.protocol.util.ClientMessageReadHandler)1 EntryAdapter (com.hazelcast.core.EntryAdapter)1