Search in sources :

Example 6 with ItemListener

use of com.hazelcast.core.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<Integer> queue = instance.getQueue(name);
    try {
        final CountDownLatch latch = new CountDownLatch(3);
        ItemListener<Integer> listener = new ItemListener<Integer>() {

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

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

Example 7 with ItemListener

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

the class SetAddListenerMessageTask method createItemListener.

private ItemListener createItemListener(final ClientEndpoint endpoint, final Data partitionKey) {
    return new ItemListener() {

        @Override
        public void itemAdded(ItemEvent item) {
            send(item);
        }

        @Override
        public void itemRemoved(ItemEvent item) {
            send(item);
        }

        private void send(ItemEvent event) {
            if (endpoint.isAlive()) {
                if (!(event instanceof DataAwareItemEvent)) {
                    throw new IllegalArgumentException("Expecting: DataAwareItemEvent, Found: " + event.getClass().getSimpleName());
                }
                DataAwareItemEvent dataAwareItemEvent = (DataAwareItemEvent) event;
                Data item = dataAwareItemEvent.getItemData();
                ClientMessage clientMessage = SetAddListenerCodec.encodeItemEvent(item, event.getMember().getUuid(), event.getEventType().getType());
                sendClientMessage(partitionKey, clientMessage);
            }
        }
    };
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemListener(com.hazelcast.core.ItemListener) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 8 with ItemListener

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

the class AbstractCollectionProxyImpl method initialize.

@Override
public void initialize() {
    final NodeEngine nodeEngine = getNodeEngine();
    CollectionConfig config = getConfig(nodeEngine);
    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 ExceptionUtil.rethrow(e);
            }
        }
        if (listener != null) {
            if (listener instanceof HazelcastInstanceAware) {
                ((HazelcastInstanceAware) listener).setHazelcastInstance(nodeEngine.getHazelcastInstance());
            }
            addItemListener(listener, itemListenerConfig.isIncludeValue());
        }
    }
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) ItemListenerConfig(com.hazelcast.config.ItemListenerConfig) ItemListener(com.hazelcast.core.ItemListener) CollectionConfig(com.hazelcast.config.CollectionConfig) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware)

Example 9 with ItemListener

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

the class ListAddListenerMessageTask method createItemListener.

private ItemListener createItemListener(final ClientEndpoint endpoint, final Data partitionKey) {
    return new ItemListener() {

        @Override
        public void itemAdded(ItemEvent item) {
            send(item);
        }

        @Override
        public void itemRemoved(ItemEvent item) {
            send(item);
        }

        private void send(ItemEvent event) {
            if (endpoint.isAlive()) {
                if (!(event instanceof DataAwareItemEvent)) {
                    throw new IllegalArgumentException("Expecting: DataAwareItemEvent, Found: " + event.getClass().getSimpleName());
                }
                DataAwareItemEvent dataAwareItemEvent = (DataAwareItemEvent) event;
                Data item = dataAwareItemEvent.getItemData();
                ClientMessage clientMessage = ListAddListenerCodec.encodeItemEvent(item, event.getMember().getUuid(), event.getEventType().getType());
                sendClientMessage(partitionKey, clientMessage);
            }
        }
    };
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemListener(com.hazelcast.core.ItemListener) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 10 with ItemListener

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

the class ClientSetTest 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) {
        }
    };
    String registrationId = set.addItemListener(listener, true);
    new Thread() {

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

Aggregations

ItemListener (com.hazelcast.core.ItemListener)10 ItemEvent (com.hazelcast.core.ItemEvent)6 Data (com.hazelcast.nio.serialization.Data)5 ClientEndpoint (com.hazelcast.client.ClientEndpoint)3 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 DataAwareItemEvent (com.hazelcast.collection.impl.common.DataAwareItemEvent)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 CollectionEventFilter (com.hazelcast.collection.impl.collection.CollectionEventFilter)2 ItemListenerConfig (com.hazelcast.config.ItemListenerConfig)2 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)2 EventRegistration (com.hazelcast.spi.EventRegistration)2 EventService (com.hazelcast.spi.EventService)2 NodeEngine (com.hazelcast.spi.NodeEngine)2 QueueService (com.hazelcast.collection.impl.queue.QueueService)1 CollectionConfig (com.hazelcast.config.CollectionConfig)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1