Search in sources :

Example 1 with ClientListener

use of com.hazelcast.core.ClientListener in project hazelcast by hazelcast.

the class ClientServiceTest method testClientListener_fromConfig.

@Test
public void testClientListener_fromConfig() {
    Config config = new Config();
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    ListenerConfig listenerConfig = new ListenerConfig(new ClientListener() {

        @Override
        public void clientConnected(Client client) {
            connectedLatch.countDown();
        }

        @Override
        public void clientDisconnected(Client client) {
            disconnectedLatch.countDown();
        }
    });
    config.addListenerConfig(listenerConfig);
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance(config);
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    client.shutdown();
    assertOpenEventually(connectedLatch);
    assertOpenEventually(disconnectedLatch);
    instance.shutdown();
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ListenerConfig(com.hazelcast.config.ListenerConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) Client(com.hazelcast.core.Client) ClientListener(com.hazelcast.core.ClientListener) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with ClientListener

use of com.hazelcast.core.ClientListener in project hazelcast by hazelcast.

the class ClientServiceTest method testRemoveClientListener_whenListenerAlreadyRemoved.

@Test(timeout = 120000)
public void testRemoveClientListener_whenListenerAlreadyRemoved() {
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    ClientService clientService = instance.getClientService();
    ClientListener clientListener = mock(ClientListener.class);
    String id = clientService.addClientListener(clientListener);
    // first time remove
    assertTrue(clientService.removeClientListener(id));
    // second time remove
    assertFalse(clientService.removeClientListener(id));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientService(com.hazelcast.core.ClientService) ClientListener(com.hazelcast.core.ClientListener) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with ClientListener

use of com.hazelcast.core.ClientListener in project hazelcast by hazelcast.

the class Node method initializeListeners.

@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity" })
private void initializeListeners(Config config) {
    for (final ListenerConfig listenerCfg : config.getListenerConfigs()) {
        Object listener = listenerCfg.getImplementation();
        if (listener == null) {
            try {
                listener = ClassLoaderUtil.newInstance(configClassLoader, listenerCfg.getClassName());
            } catch (Exception e) {
                logger.severe(e);
            }
        }
        if (listener instanceof HazelcastInstanceAware) {
            ((HazelcastInstanceAware) listener).setHazelcastInstance(hazelcastInstance);
        }
        boolean known = false;
        if (listener instanceof DistributedObjectListener) {
            final ProxyServiceImpl proxyService = (ProxyServiceImpl) nodeEngine.getProxyService();
            proxyService.addProxyListener((DistributedObjectListener) listener);
            known = true;
        }
        if (listener instanceof MembershipListener) {
            clusterService.addMembershipListener((MembershipListener) listener);
            known = true;
        }
        if (listener instanceof MigrationListener) {
            partitionService.addMigrationListener((MigrationListener) listener);
            known = true;
        }
        if (listener instanceof PartitionLostListener) {
            partitionService.addPartitionLostListener((PartitionLostListener) listener);
            known = true;
        }
        if (listener instanceof LifecycleListener) {
            hazelcastInstance.lifecycleService.addLifecycleListener((LifecycleListener) listener);
            known = true;
        }
        if (listener instanceof ClientListener) {
            String serviceName = ClientEngineImpl.SERVICE_NAME;
            nodeEngine.getEventService().registerLocalListener(serviceName, serviceName, listener);
            known = true;
        }
        if (listener instanceof InternalMigrationListener) {
            final InternalPartitionServiceImpl partitionService = (InternalPartitionServiceImpl) nodeEngine.getPartitionService();
            partitionService.setInternalMigrationListener((InternalMigrationListener) listener);
            known = true;
        }
        if (nodeExtension.registerListener(listener)) {
            known = true;
        }
        if (listener != null && !known) {
            final String error = "Unknown listener type: " + listener.getClass();
            Throwable t = new IllegalArgumentException(error);
            logger.warning(error, t);
        }
    }
}
Also used : InternalPartitionServiceImpl(com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl) InternalMigrationListener(com.hazelcast.internal.partition.impl.InternalMigrationListener) LifecycleListener(com.hazelcast.core.LifecycleListener) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) ProxyServiceImpl(com.hazelcast.spi.impl.proxyservice.impl.ProxyServiceImpl) ClientListener(com.hazelcast.core.ClientListener) ListenerConfig(com.hazelcast.config.ListenerConfig) PartitionLostListener(com.hazelcast.partition.PartitionLostListener) MembershipListener(com.hazelcast.core.MembershipListener) MigrationListener(com.hazelcast.core.MigrationListener) InternalMigrationListener(com.hazelcast.internal.partition.impl.InternalMigrationListener)

Example 4 with ClientListener

use of com.hazelcast.core.ClientListener in project hazelcast by hazelcast.

the class ClientServiceTest method testClientListener.

@Test(timeout = 120000)
public void testClientListener() throws InterruptedException {
    final HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    final ClientService clientService = instance.getClientService();
    final CountDownLatch latchAdd = new CountDownLatch(2);
    final CountDownLatch latchRemove = new CountDownLatch(2);
    final AtomicInteger totalAdd = new AtomicInteger(0);
    final ClientListener clientListener = new ClientListener() {

        @Override
        public void clientConnected(Client client) {
            totalAdd.incrementAndGet();
            latchAdd.countDown();
        }

        @Override
        public void clientDisconnected(Client client) {
            latchRemove.countDown();
        }
    };
    final String id = clientService.addClientListener(clientListener);
    final HazelcastInstance client1 = hazelcastFactory.newHazelcastClient();
    final HazelcastInstance client2 = hazelcastFactory.newHazelcastClient();
    client1.getLifecycleService().shutdown();
    client2.getLifecycleService().shutdown();
    assertOpenEventually(latchAdd);
    assertOpenEventually(latchRemove);
    assertTrue(clientService.removeClientListener(id));
    assertFalse(clientService.removeClientListener("foo"));
    assertEquals(0, clientService.getConnectedClients().size());
    hazelcastFactory.newHazelcastClient();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, clientService.getConnectedClients().size());
        }
    });
    assertEquals(2, totalAdd.get());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientService(com.hazelcast.core.ClientService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertTask(com.hazelcast.test.AssertTask) CountDownLatch(java.util.concurrent.CountDownLatch) Client(com.hazelcast.core.Client) ClientListener(com.hazelcast.core.ClientListener) UnknownHostException(java.net.UnknownHostException) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ClientListener

use of com.hazelcast.core.ClientListener in project hazelcast by hazelcast.

the class ClientServiceTest method testClientListener_withShuttingDownOwnerMember.

@Test
public void testClientListener_withShuttingDownOwnerMember() {
    Config config = new Config();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger atomicInteger = new AtomicInteger();
    ListenerConfig listenerConfig = new ListenerConfig(new ClientListener() {

        @Override
        public void clientConnected(Client client) {
            atomicInteger.incrementAndGet();
            latch.countDown();
        }

        @Override
        public void clientDisconnected(Client client) {
        }
    });
    config.addListenerConfig(listenerConfig);
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    //first member is owner connection
    hazelcastFactory.newHazelcastClient();
    hazelcastFactory.newHazelcastInstance(config);
    //make sure connected to second one before proceeding
    assertOpenEventually(latch);
    //when first node is dead, client selects second one as owner
    instance.shutdown();
    //Testing that shutting down first member does not cause any client connected/disconnected event
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, atomicInteger.get());
        }
    }, 4);
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListenerConfig(com.hazelcast.config.ListenerConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) AssertTask(com.hazelcast.test.AssertTask) CountDownLatch(java.util.concurrent.CountDownLatch) Client(com.hazelcast.core.Client) ClientListener(com.hazelcast.core.ClientListener) UnknownHostException(java.net.UnknownHostException) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ClientListener (com.hazelcast.core.ClientListener)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 NightlyTest (com.hazelcast.test.annotation.NightlyTest)5 ParallelTest (com.hazelcast.test.annotation.ParallelTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 Test (org.junit.Test)5 ListenerConfig (com.hazelcast.config.ListenerConfig)4 Client (com.hazelcast.core.Client)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 Config (com.hazelcast.config.Config)3 AssertTask (com.hazelcast.test.AssertTask)3 UnknownHostException (java.net.UnknownHostException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ClientService (com.hazelcast.core.ClientService)2 DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 LifecycleListener (com.hazelcast.core.LifecycleListener)1 MembershipListener (com.hazelcast.core.MembershipListener)1 MigrationListener (com.hazelcast.core.MigrationListener)1