Search in sources :

Example 1 with ItemListener

use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.

the class QueueTestsFrom2X method testListenerLifecycle.

@Test
public void testListenerLifecycle() throws Exception {
    long sleep = 2000;
    String name = "listenerLifecycle";
    HazelcastInstance instance = createHazelcastInstance();
    IQueue<VersionedObject<Integer>> queue = instance.getQueue(name);
    try {
        CountDownLatch latch = new CountDownLatch(3);
        ItemListener<VersionedObject<Integer>> listener = new ItemListener<VersionedObject<Integer>>() {

            public void itemAdded(ItemEvent<VersionedObject<Integer>> itemEvent) {
                latch.countDown();
            }

            public void itemRemoved(ItemEvent<VersionedObject<Integer>> itemEvent) {
            }
        };
        queue.addItemListener(listener, false);
        queue.offer(new VersionedObject<>(1));
        sleep(sleep);
        queue.destroy();
        queue = instance.getQueue(name);
        UUID id = queue.addItemListener(listener, false);
        queue.offer(new VersionedObject<>(2));
        sleep(sleep);
        queue.removeItemListener(id);
        queue.offer(new VersionedObject<>(3));
        sleep(sleep);
        assertEquals(1, latch.getCount());
        latch.countDown();
        assertTrue("Remaining: " + latch.getCount(), latch.await(3, TimeUnit.SECONDS));
    } finally {
        queue.destroy();
    }
}
Also used : VersionedObject(com.hazelcast.collection.impl.queue.model.VersionedObject) ItemEvent(com.hazelcast.collection.ItemEvent) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ItemListener(com.hazelcast.collection.ItemListener) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with ItemListener

use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.

the class ClientListTest method testListener.

@Test
public void testListener() throws Exception {
    final CountDownLatch latch = new CountDownLatch(6);
    ItemListener<String> listener = new ItemListener<String>() {

        public void itemAdded(ItemEvent<String> itemEvent) {
            latch.countDown();
        }

        public void itemRemoved(ItemEvent<String> item) {
        }
    };
    UUID registrationId = list.addItemListener(listener, true);
    new Thread() {

        public void run() {
            for (int i = 0; i < 5; i++) {
                list.add("item" + i);
            }
            list.add("done");
        }
    }.start();
    assertTrue(latch.await(20, TimeUnit.SECONDS));
    list.removeItemListener(registrationId);
}
Also used : ItemEvent(com.hazelcast.collection.ItemEvent) ItemListener(com.hazelcast.collection.ItemListener) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with ItemListener

use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.

the class QueueProxySupport method initialize.

@Override
public void initialize() {
    final NodeEngine nodeEngine = getNodeEngine();
    final List<ItemListenerConfig> itemListenerConfigs = config.getItemListenerConfigs();
    for (ItemListenerConfig itemListenerConfig : itemListenerConfigs) {
        ItemListener listener = itemListenerConfig.getImplementation();
        if (listener == null && itemListenerConfig.getClassName() != null) {
            try {
                listener = ClassLoaderUtil.newInstance(nodeEngine.getConfigClassLoader(), itemListenerConfig.getClassName());
            } catch (Exception e) {
                throw rethrow(e);
            }
        }
        if (listener != null) {
            if (listener instanceof HazelcastInstanceAware) {
                ((HazelcastInstanceAware) listener).setHazelcastInstance(nodeEngine.getHazelcastInstance());
            }
            addItemListener(listener, itemListenerConfig.isIncludeValue());
        }
    }
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) ItemListener(com.hazelcast.collection.ItemListener) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware)

Example 4 with ItemListener

use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.

the class SetAddListenerMessageTask method processInternal.

@Override
protected CompletableFuture<UUID> processInternal() {
    Data partitionKey = serializationService.toData(parameters.name);
    ItemListener listener = createItemListener(endpoint, partitionKey);
    EventService eventService = clientEngine.getEventService();
    CollectionEventFilter filter = new CollectionEventFilter(parameters.includeValue);
    if (parameters.localOnly) {
        return newCompletedFuture(eventService.registerLocalListener(getServiceName(), parameters.name, filter, listener).getId());
    }
    return eventService.registerListenerAsync(getServiceName(), parameters.name, filter, listener).thenApplyAsync(EventRegistration::getId, CALLER_RUNS);
}
Also used : CollectionEventFilter(com.hazelcast.collection.impl.collection.CollectionEventFilter) EventRegistration(com.hazelcast.spi.impl.eventservice.EventRegistration) Data(com.hazelcast.internal.serialization.Data) ItemListener(com.hazelcast.collection.ItemListener) EventService(com.hazelcast.spi.impl.eventservice.EventService)

Example 5 with ItemListener

use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.

the class ListenerConfigHolder method asListenerConfig.

public <T extends ListenerConfig> T asListenerConfig(SerializationService serializationService) {
    validate();
    ListenerConfig listenerConfig = null;
    if (className != null) {
        switch(listenerType) {
            case GENERIC:
                listenerConfig = new ListenerConfig(className);
                break;
            case ITEM:
                listenerConfig = new ItemListenerConfig(className, includeValue);
                break;
            case ENTRY:
                listenerConfig = new EntryListenerConfig(className, local, includeValue);
                break;
            case SPLIT_BRAIN_PROTECTION:
                listenerConfig = new SplitBrainProtectionListenerConfig(className);
                break;
            case CACHE_PARTITION_LOST:
                listenerConfig = new CachePartitionLostListenerConfig(className);
                break;
            case MAP_PARTITION_LOST:
                listenerConfig = new MapPartitionLostListenerConfig(className);
                break;
            default:
        }
    } else {
        EventListener eventListener = serializationService.toObject(listenerImplementation);
        switch(listenerType) {
            case GENERIC:
                listenerConfig = new ListenerConfig(eventListener);
                break;
            case ITEM:
                listenerConfig = new ItemListenerConfig((ItemListener) eventListener, includeValue);
                break;
            case ENTRY:
                listenerConfig = new EntryListenerConfig((MapListener) eventListener, local, includeValue);
                break;
            case SPLIT_BRAIN_PROTECTION:
                listenerConfig = new SplitBrainProtectionListenerConfig((SplitBrainProtectionListener) eventListener);
                break;
            case CACHE_PARTITION_LOST:
                listenerConfig = new CachePartitionLostListenerConfig((CachePartitionLostListener) eventListener);
                break;
            case MAP_PARTITION_LOST:
                listenerConfig = new MapPartitionLostListenerConfig((MapPartitionLostListener) eventListener);
                break;
            default:
        }
    }
    return (T) listenerConfig;
}
Also used : SplitBrainProtectionListener(com.hazelcast.splitbrainprotection.SplitBrainProtectionListener) CachePartitionLostListener(com.hazelcast.cache.impl.event.CachePartitionLostListener) MapPartitionLostListenerConfig(com.hazelcast.config.MapPartitionLostListenerConfig) CachePartitionLostListenerConfig(com.hazelcast.config.CachePartitionLostListenerConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) CachePartitionLostListenerConfig(com.hazelcast.config.CachePartitionLostListenerConfig) ListenerConfig(com.hazelcast.config.ListenerConfig) SplitBrainProtectionListenerConfig(com.hazelcast.config.SplitBrainProtectionListenerConfig) MapPartitionLostListenerConfig(com.hazelcast.config.MapPartitionLostListenerConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) SplitBrainProtectionListenerConfig(com.hazelcast.config.SplitBrainProtectionListenerConfig) MapPartitionLostListener(com.hazelcast.map.listener.MapPartitionLostListener) MapListener(com.hazelcast.map.listener.MapListener) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) ItemListener(com.hazelcast.collection.ItemListener) EventListener(java.util.EventListener)

Aggregations

ItemListener (com.hazelcast.collection.ItemListener)11 ItemEvent (com.hazelcast.collection.ItemEvent)6 Data (com.hazelcast.internal.serialization.Data)5 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 DataAwareItemEvent (com.hazelcast.collection.impl.common.DataAwareItemEvent)3 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 UUID (java.util.UUID)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 CollectionEventFilter (com.hazelcast.collection.impl.collection.CollectionEventFilter)2 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)2 NodeEngine (com.hazelcast.spi.impl.NodeEngine)2 EventRegistration (com.hazelcast.spi.impl.eventservice.EventRegistration)2 EventService (com.hazelcast.spi.impl.eventservice.EventService)2 CachePartitionLostListener (com.hazelcast.cache.impl.event.CachePartitionLostListener)1 QueueService (com.hazelcast.collection.impl.queue.QueueService)1 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)1 CachePartitionLostListenerConfig (com.hazelcast.config.CachePartitionLostListenerConfig)1