Search in sources :

Example 1 with ItemListener

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

the class QueueProxySupport method initialize.

@Override
public void initialize() {
    final NodeEngine nodeEngine = getNodeEngine();
    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) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware)

Example 2 with ItemListener

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

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

the class ListAddListenerMessageTask method call.

@Override
protected Object call() {
    ClientEndpoint endpoint = getEndpoint();
    Data partitionKey = serializationService.toData(parameters.name);
    ItemListener listener = createItemListener(endpoint, partitionKey);
    EventService eventService = clientEngine.getEventService();
    CollectionEventFilter filter = new CollectionEventFilter(parameters.includeValue);
    EventRegistration registration;
    if (parameters.localOnly) {
        registration = eventService.registerLocalListener(getServiceName(), parameters.name, filter, listener);
    } else {
        registration = eventService.registerListener(getServiceName(), parameters.name, filter, listener);
    }
    String registrationId = registration.getId();
    endpoint.addListenerDestroyAction(getServiceName(), parameters.name, registrationId);
    return registrationId;
}
Also used : CollectionEventFilter(com.hazelcast.collection.impl.collection.CollectionEventFilter) EventRegistration(com.hazelcast.spi.EventRegistration) Data(com.hazelcast.nio.serialization.Data) ItemListener(com.hazelcast.core.ItemListener) EventService(com.hazelcast.spi.EventService) ClientEndpoint(com.hazelcast.client.ClientEndpoint)

Example 4 with ItemListener

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

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

the class SetAddListenerMessageTask method call.

@Override
protected Object call() {
    ClientEndpoint endpoint = getEndpoint();
    Data partitionKey = serializationService.toData(parameters.name);
    ItemListener listener = createItemListener(endpoint, partitionKey);
    EventService eventService = clientEngine.getEventService();
    CollectionEventFilter filter = new CollectionEventFilter(parameters.includeValue);
    EventRegistration registration;
    if (parameters.localOnly) {
        registration = eventService.registerLocalListener(getServiceName(), parameters.name, filter, listener);
    } else {
        registration = eventService.registerListener(getServiceName(), parameters.name, filter, listener);
    }
    String registrationId = registration.getId();
    endpoint.addListenerDestroyAction(getServiceName(), parameters.name, registrationId);
    return registrationId;
}
Also used : CollectionEventFilter(com.hazelcast.collection.impl.collection.CollectionEventFilter) EventRegistration(com.hazelcast.spi.EventRegistration) Data(com.hazelcast.nio.serialization.Data) ItemListener(com.hazelcast.core.ItemListener) EventService(com.hazelcast.spi.EventService) ClientEndpoint(com.hazelcast.client.ClientEndpoint)

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