Search in sources :

Example 1 with AbstractLoadBalancer

use of com.hazelcast.client.util.AbstractLoadBalancer in project hazelcast by hazelcast.

the class ClientUniSocketTxnTest method testUniSocketClient_shouldNotOpenANewConnection.

@Test
public void testUniSocketClient_shouldNotOpenANewConnection() {
    hazelcastFactory.newHazelcastInstance();
    ClientConfig config = new ClientConfig();
    config.getNetworkConfig().setRedoOperation(true);
    config.getNetworkConfig().setSmartRouting(false);
    // try to force second member to connect when exist
    config.setLoadBalancer(new AbstractLoadBalancer() {

        @Override
        public Member next() {
            Member[] members = getMembers();
            if (members == null || members.length == 0) {
                return null;
            }
            if (members.length == 2) {
                return members[1];
            }
            return members[0];
        }

        @Override
        public Member nextDataMember() {
            Member[] members = getDataMembers();
            if (members == null || members.length == 0) {
                return null;
            }
            if (members.length == 2) {
                return members[1];
            }
            return members[0];
        }

        @Override
        public boolean canGetNextDataMember() {
            return true;
        }
    });
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
    hazelcastFactory.newHazelcastInstance();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, client.getCluster().getMembers().size());
        }
    });
    ClientConnectionManager connectionManager = getHazelcastClientInstanceImpl(client).getConnectionManager();
    TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    context.commitTransaction();
    assertEquals(1, connectionManager.getActiveConnections().size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) AssertTask(com.hazelcast.test.AssertTask) ClientConfig(com.hazelcast.client.config.ClientConfig) AbstractLoadBalancer(com.hazelcast.client.util.AbstractLoadBalancer) Member(com.hazelcast.cluster.Member) ClientConnectionManager(com.hazelcast.client.impl.connection.ClientConnectionManager) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with AbstractLoadBalancer

use of com.hazelcast.client.util.AbstractLoadBalancer in project hazelcast by hazelcast.

the class ClientTxnTest method setup.

@Before
public void setup() {
    server = hazelcastFactory.newHazelcastInstance();
    final ClientConfig config = new ClientConfig();
    config.getNetworkConfig().setRedoOperation(true);
    // always start the txn on first member
    config.setLoadBalancer(new AbstractLoadBalancer() {

        @Override
        public Member next() {
            Member[] members = getMembers();
            if (members == null || members.length == 0) {
                return null;
            }
            return members[0];
        }

        @Override
        public Member nextDataMember() {
            Member[] members = getDataMembers();
            if (members == null || members.length == 0) {
                return null;
            }
            return members[0];
        }

        @Override
        public boolean canGetNextDataMember() {
            return true;
        }
    });
    client = hazelcastFactory.newHazelcastClient(config);
    hazelcastFactory.newHazelcastInstance();
}
Also used : ClientConfig(com.hazelcast.client.config.ClientConfig) AbstractLoadBalancer(com.hazelcast.client.util.AbstractLoadBalancer) Member(com.hazelcast.cluster.Member) Before(org.junit.Before)

Example 3 with AbstractLoadBalancer

use of com.hazelcast.client.util.AbstractLoadBalancer in project hazelcast by hazelcast.

the class ClientTxnUniSocketTest method testUniSocketClient_shouldNotOpenANewConnection.

@Test
public void testUniSocketClient_shouldNotOpenANewConnection() {
    hazelcastFactory.newHazelcastInstance();
    ClientConfig config = new ClientConfig();
    config.getNetworkConfig().setRedoOperation(true);
    config.getNetworkConfig().setSmartRouting(false);
    // try to force second member to connect when exist
    config.setLoadBalancer(new AbstractLoadBalancer() {

        @Override
        public Member next() {
            Member[] members = getMembers();
            if (members == null || members.length == 0) {
                return null;
            }
            if (members.length == 2) {
                return members[1];
            }
            return members[0];
        }

        @Override
        public Member nextDataMember() {
            Member[] members = getDataMembers();
            if (members == null || members.length == 0) {
                return null;
            }
            if (members.length == 2) {
                return members[1];
            }
            return members[0];
        }

        @Override
        public boolean canGetNextDataMember() {
            return true;
        }
    });
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(config);
    hazelcastFactory.newHazelcastInstance();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(2, client.getCluster().getMembers().size());
        }
    });
    ClientConnectionManager connectionManager = getHazelcastClientInstanceImpl(client).getConnectionManager();
    TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    context.commitTransaction();
    assertEquals(1, connectionManager.getActiveConnections().size());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) AssertTask(com.hazelcast.test.AssertTask) ClientConfig(com.hazelcast.client.config.ClientConfig) AbstractLoadBalancer(com.hazelcast.client.util.AbstractLoadBalancer) Member(com.hazelcast.cluster.Member) ClientConnectionManager(com.hazelcast.client.impl.connection.ClientConnectionManager) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ClientConfig (com.hazelcast.client.config.ClientConfig)3 AbstractLoadBalancer (com.hazelcast.client.util.AbstractLoadBalancer)3 Member (com.hazelcast.cluster.Member)3 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 AssertTask (com.hazelcast.test.AssertTask)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 TransactionContext (com.hazelcast.transaction.TransactionContext)2 Test (org.junit.Test)2 Before (org.junit.Before)1