Search in sources :

Example 1 with DistributedObjectListener

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

the class TestCacheManager method testCacheNames.

@Test
public void testCacheNames() {
    // create a test instance, to reproduce the behavior described in the GitHub issue
    // https://github.com/hazelcast/hazelcast/issues/492
    final String testMap = "test-map";
    final CountDownLatch distributionSignal = new CountDownLatch(1);
    instance.addDistributedObjectListener(new DistributedObjectListener() {

        @Override
        public void distributedObjectCreated(DistributedObjectEvent event) {
            DistributedObject distributedObject = event.getDistributedObject();
            if (distributedObject instanceof IMap) {
                IMap<?, ?> map = (IMap) distributedObject;
                if (testMap.equals(map.getName())) {
                    distributionSignal.countDown();
                }
            }
        }

        @Override
        public void distributedObjectDestroyed(DistributedObjectEvent event) {
        }
    });
    HazelcastInstance testInstance = Hazelcast.newHazelcastInstance();
    testInstance.getMap(testMap);
    // be sure that test-map is distributed
    HazelcastTestSupport.assertOpenEventually(distributionSignal);
    Collection<String> test = cacheManager.getCacheNames();
    assertContains(test, testMap);
    testInstance.shutdown();
}
Also used : DistributedObject(com.hazelcast.core.DistributedObject) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with DistributedObjectListener

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

the class QueueEvictionTest method testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted.

@Test
public void testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted() throws Exception {
    String queueName = randomString();
    Config config = new Config();
    config.getQueueConfig(queueName).setEmptyQueueTtl(0);
    HazelcastInstance hz = createHazelcastInstance(config);
    final CountDownLatch latch = new CountDownLatch(2);
    hz.addDistributedObjectListener(new DistributedObjectListener() {

        public void distributedObjectCreated(DistributedObjectEvent event) {
            latch.countDown();
        }

        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            latch.countDown();
        }
    });
    IQueue<Object> queue = hz.getQueue(queueName);
    assertTrue(queue.offer("item"));
    assertEquals("item", queue.poll());
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with DistributedObjectListener

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

the class HazelcastOSGiInstanceTest method addDistributedObjectListenerCalledSuccessfullyOverOSGiInstance.

@Test
public void addDistributedObjectListenerCalledSuccessfullyOverOSGiInstance() {
    DistributedObjectListener mockDistributedObjectListener = mock(DistributedObjectListener.class);
    HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
    HazelcastOSGiInstance hazelcastOSGiInstance = HazelcastOSGiTestUtil.createHazelcastOSGiInstance(mockHazelcastInstance);
    when(mockHazelcastInstance.addDistributedObjectListener(mockDistributedObjectListener)).thenReturn("my-registration-id");
    assertEquals("my-registration-id", hazelcastOSGiInstance.addDistributedObjectListener(mockDistributedObjectListener));
    verify(mockHazelcastInstance).addDistributedObjectListener(mockDistributedObjectListener);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with DistributedObjectListener

use of com.hazelcast.core.DistributedObjectListener 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 5 with DistributedObjectListener

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

the class QueueListenerTest method testListener_withEvictionViaTTL.

@Test
public void testListener_withEvictionViaTTL() throws Exception {
    Config config = new Config();
    config.getQueueConfig("queueWithTTL").setEmptyQueueTtl(0);
    HazelcastInstance hz = createHazelcastInstance(config);
    final CountDownLatch latch = new CountDownLatch(2);
    hz.addDistributedObjectListener(new DistributedObjectListener() {

        @Override
        public void distributedObjectCreated(DistributedObjectEvent event) {
            latch.countDown();
        }

        @Override
        public void distributedObjectDestroyed(DistributedObjectEvent event) {
            latch.countDown();
        }
    });
    IQueue<Object> queue = hz.getQueue("queueWithTTL");
    queue.offer("item");
    queue.poll();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) DistributedObjectEvent(com.hazelcast.core.DistributedObjectEvent) Config(com.hazelcast.config.Config) QueueConfig(com.hazelcast.config.QueueConfig) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) DistributedObjectListener(com.hazelcast.core.DistributedObjectListener) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

DistributedObjectListener (com.hazelcast.core.DistributedObjectListener)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 Test (org.junit.Test)5 DistributedObjectEvent (com.hazelcast.core.DistributedObjectEvent)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Config (com.hazelcast.config.Config)2 DistributedObject (com.hazelcast.core.DistributedObject)2 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)1 ListenerConfig (com.hazelcast.config.ListenerConfig)1 QueueConfig (com.hazelcast.config.QueueConfig)1 ClientListener (com.hazelcast.core.ClientListener)1 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)1 IMap (com.hazelcast.core.IMap)1 LifecycleListener (com.hazelcast.core.LifecycleListener)1 MembershipListener (com.hazelcast.core.MembershipListener)1 MigrationListener (com.hazelcast.core.MigrationListener)1 InternalMigrationListener (com.hazelcast.internal.partition.impl.InternalMigrationListener)1 InternalPartitionServiceImpl (com.hazelcast.internal.partition.impl.InternalPartitionServiceImpl)1