Search in sources :

Example 11 with ItemEvent

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

the class HazelcastQueueConsumerTest method add.

@Test
@SuppressWarnings("unchecked")
public void add() throws InterruptedException {
    MockEndpoint out = getMockEndpoint("mock:added");
    out.expectedMessageCount(1);
    final ItemEvent<String> event = new ItemEvent<String>("foo", ItemEventType.ADDED, "foo", null);
    argument.getValue().itemAdded(event);
    assertMockEndpointsSatisfied(2000, TimeUnit.MILLISECONDS);
    this.checkHeaders(out.getExchanges().get(0).getIn().getHeaders(), HazelcastConstants.ADDED);
}
Also used : ItemEvent(com.hazelcast.core.ItemEvent) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 12 with ItemEvent

use of com.hazelcast.core.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.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 13 with ItemEvent

use of com.hazelcast.core.ItemEvent 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 14 with ItemEvent

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

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