Search in sources :

Example 6 with ItemEvent

use of com.hazelcast.core.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, nodeEngine.getSerializationService());
    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.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) MemberImpl(com.hazelcast.instance.MemberImpl)

Example 7 with ItemEvent

use of com.hazelcast.core.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) {
        }
    };
    String 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.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)

Example 8 with ItemEvent

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

the class QueueAddListenerMessageTask method call.

@Override
protected Object call() {
    final ClientEndpoint endpoint = getEndpoint();
    final QueueService service = getService(QueueService.SERVICE_NAME);
    final Data partitionKey = serializationService.toData(parameters.name);
    ItemListener listener = 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 = QueueAddListenerCodec.encodeItemEvent(item, event.getMember().getUuid(), event.getEventType().getType());
                sendClientMessage(partitionKey, clientMessage);
            }
        }
    };
    String registrationId = service.addItemListener(parameters.name, listener, parameters.includeValue, parameters.localOnly);
    endpoint.addListenerDestroyAction(QueueService.SERVICE_NAME, parameters.name, registrationId);
    return registrationId;
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) QueueService(com.hazelcast.collection.impl.queue.QueueService) Data(com.hazelcast.nio.serialization.Data) ItemListener(com.hazelcast.core.ItemListener) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientEndpoint(com.hazelcast.client.ClientEndpoint)

Example 9 with ItemEvent

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

the class CollectionService method dispatchEvent.

@Override
public void dispatchEvent(CollectionEvent event, ItemListener listener) {
    final MemberImpl member = nodeEngine.getClusterService().getMember(event.getCaller());
    ItemEvent itemEvent = new DataAwareItemEvent(event.getName(), event.getEventType(), event.getData(), member, nodeEngine.getSerializationService());
    if (member == null) {
        if (logger.isInfoEnabled()) {
            logger.info("Dropping event " + itemEvent + " from unknown address:" + event.getCaller());
        }
        return;
    }
    if (event.getEventType().equals(ItemEventType.ADDED)) {
        listener.itemAdded(itemEvent);
    } else {
        listener.itemRemoved(itemEvent);
    }
}
Also used : ItemEvent(com.hazelcast.core.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) MemberImpl(com.hazelcast.instance.MemberImpl)

Example 10 with ItemEvent

use of com.hazelcast.core.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<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)

Aggregations

ItemEvent (com.hazelcast.core.ItemEvent)14 Test (org.junit.Test)9 ItemListener (com.hazelcast.core.ItemListener)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)6 DataAwareItemEvent (com.hazelcast.collection.impl.common.DataAwareItemEvent)5 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 Data (com.hazelcast.nio.serialization.Data)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 MemberImpl (com.hazelcast.instance.MemberImpl)2 ClientEndpoint (com.hazelcast.client.ClientEndpoint)1 QueueService (com.hazelcast.collection.impl.queue.QueueService)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1