Search in sources :

Example 6 with ItemListener

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

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

the class ListAddListenerMessageTask 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 8 with ItemListener

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

Example 9 with ItemListener

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

the class QueueAddListenerMessageTask method processInternal.

@Override
protected CompletableFuture<UUID> processInternal() {
    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);
            }
        }
    };
    if (parameters.localOnly) {
        return newCompletedFuture(service.addLocalItemListener(parameters.name, listener, parameters.includeValue));
    }
    return service.addItemListenerAsync(parameters.name, listener, parameters.includeValue);
}
Also used : DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) ItemEvent(com.hazelcast.collection.ItemEvent) DataAwareItemEvent(com.hazelcast.collection.impl.common.DataAwareItemEvent) QueueService(com.hazelcast.collection.impl.queue.QueueService) Data(com.hazelcast.internal.serialization.Data) ItemListener(com.hazelcast.collection.ItemListener) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 10 with ItemListener

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

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