Search in sources :

Example 1 with HazelcastClientOfflineException

use of com.hazelcast.client.HazelcastClientOfflineException 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 2 with HazelcastClientOfflineException

use of com.hazelcast.client.HazelcastClientOfflineException in project hazelcast by hazelcast.

the class ConfiguredBehaviourTest method testReconnectModeASYNC_clusterDown_clientGetsOfflineExcption.

@Test
public void testReconnectModeASYNC_clusterDown_clientGetsOfflineExcption() {
    HazelcastInstance member1 = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance member2 = hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    clientConfig.getConnectionStrategyConfig().setReconnectMode(ASYNC);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    IMap<Object, Object> map = client.getMap(randomMapName());
    member1.shutdown();
    member2.shutdown();
    assertTrue(client.getLifecycleService().isRunning());
    for (int i = 0; i < 100; i++) {
        try {
            map.get(randomString());
            fail("map.get should throw HazelcastClientOfflineException");
        } catch (HazelcastClientOfflineException ignored) {
        }
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with HazelcastClientOfflineException

use of com.hazelcast.client.HazelcastClientOfflineException in project hazelcast by hazelcast.

the class ConfiguredBehaviourTest method testReconnectModeASYNCTwoMembers.

@Test
public void testReconnectModeASYNCTwoMembers() {
    hazelcastFactory.newInstances(getConfig(), 2);
    CountDownLatch connectedLatch = new CountDownLatch(1);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.addListenerConfig(new ListenerConfig((LifecycleListener) event -> {
        if (event.getState().equals(CLIENT_CONNECTED)) {
            connectedLatch.countDown();
        }
    }));
    clientConfig.getConnectionStrategyConfig().setReconnectMode(ASYNC);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    assertTrue(client.getLifecycleService().isRunning());
    assertOpenEventually(connectedLatch);
    IMap<Integer, Integer> map = client.getMap(randomMapName());
    map.put(1, 5);
    hazelcastFactory.shutdownAllMembers();
    HazelcastInstance[] instances = hazelcastFactory.newInstances(getConfig(), 2);
    assertTrueEventually(() -> {
        Set<Member> actualMembers = client.getCluster().getMembers();
        Set<Member> expectedMembers = instances[0].getCluster().getMembers();
        assertEquals(expectedMembers, actualMembers);
    });
    assertTrueEventually(() -> {
        try {
            map.get(1);
        } catch (HazelcastClientOfflineException e) {
            fail();
        }
    });
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) Member(com.hazelcast.cluster.Member) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with HazelcastClientOfflineException

use of com.hazelcast.client.HazelcastClientOfflineException in project hazelcast by hazelcast.

the class ClientTransactionManagerServiceImpl method throwException.

private RuntimeException throwException(boolean smartRouting) {
    ClientConfig clientConfig = client.getClientConfig();
    ClientConnectionStrategyConfig connectionStrategyConfig = clientConfig.getConnectionStrategyConfig();
    ClientConnectionStrategyConfig.ReconnectMode reconnectMode = connectionStrategyConfig.getReconnectMode();
    if (reconnectMode.equals(ClientConnectionStrategyConfig.ReconnectMode.ASYNC)) {
        throw new HazelcastClientOfflineException();
    }
    if (smartRouting) {
        Set<Member> members = client.getCluster().getMembers();
        String msg;
        if (members.isEmpty()) {
            msg = "No address was return by the LoadBalancer since there are no members in the cluster";
        } else {
            msg = "No address was return by the LoadBalancer. " + "But the cluster contains the following members:" + members;
        }
        throw new IllegalStateException(msg);
    }
    throw new IllegalStateException("No active connection is found");
}
Also used : ClientConnectionStrategyConfig(com.hazelcast.client.config.ClientConnectionStrategyConfig) StringUtil.timeToString(com.hazelcast.internal.util.StringUtil.timeToString) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientOfflineException(com.hazelcast.client.HazelcastClientOfflineException) Member(com.hazelcast.cluster.Member)

Aggregations

HazelcastClientOfflineException (com.hazelcast.client.HazelcastClientOfflineException)4 ClientConfig (com.hazelcast.client.config.ClientConfig)4 Member (com.hazelcast.cluster.Member)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 Test (org.junit.Test)2 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)1 ClientConnectionStrategyConfig (com.hazelcast.client.config.ClientConnectionStrategyConfig)1 ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)1 ListenerConfig (com.hazelcast.config.ListenerConfig)1 LifecycleListener (com.hazelcast.core.LifecycleListener)1 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)1 StringUtil.timeToString (com.hazelcast.internal.util.StringUtil.timeToString)1 TransactionException (com.hazelcast.transaction.TransactionException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1