Search in sources :

Example 1 with ItemEvent

use of com.hazelcast.collection.ItemEvent 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 ItemEvent

use of com.hazelcast.collection.ItemEvent 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 ItemEvent

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

the class QueueService method dispatchEvent.

@Override
public void dispatchEvent(QueueEvent event, ItemListener listener) {
    final MemberImpl member = nodeEngine.getClusterService().getMember(event.caller);
    ItemEvent itemEvent = new DataAwareItemEvent(event.name, event.eventType, event.data, member, serializationService);
    if (member == null) {
        if (logger.isInfoEnabled()) {
            logger.info("Dropping event " + itemEvent + " from unknown address:" + event.caller);
        }
        return;
    }
    if (event.eventType.equals(ItemEventType.ADDED)) {
        listener.itemAdded(itemEvent);
    } else {
        listener.itemRemoved(itemEvent);
    }
    getLocalQueueStatsImpl(event.name).incrementReceivedEvents();
}
Also used : ItemEvent(com.hazelcast.collection.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) MemberImpl(com.hazelcast.cluster.impl.MemberImpl)

Example 4 with ItemEvent

use of com.hazelcast.collection.ItemEvent 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.collection.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemListener(com.hazelcast.collection.ItemListener) Data(com.hazelcast.internal.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 5 with ItemEvent

use of com.hazelcast.collection.ItemEvent 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) {
        }
    };
    UUID 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.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)

Aggregations

ItemEvent (com.hazelcast.collection.ItemEvent)7 ItemListener (com.hazelcast.collection.ItemListener)6 DataAwareItemEvent (com.hazelcast.collection.impl.common.DataAwareItemEvent)4 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 Data (com.hazelcast.internal.serialization.Data)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 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)1 QueueService (com.hazelcast.collection.impl.queue.QueueService)1 VersionedObject (com.hazelcast.collection.impl.queue.model.VersionedObject)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1