use of com.hazelcast.collection.ItemListener 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();
}
}
use of com.hazelcast.collection.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) {
}
};
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);
}
use of com.hazelcast.collection.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 rethrow(e);
}
}
if (listener != null) {
if (listener instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) listener).setHazelcastInstance(nodeEngine.getHazelcastInstance());
}
addItemListener(listener, itemListenerConfig.isIncludeValue());
}
}
}
use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.
the class SetAddListenerMessageTask 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);
}
use of com.hazelcast.collection.ItemListener in project hazelcast by hazelcast.
the class ListenerConfigHolder method asListenerConfig.
public <T extends ListenerConfig> T asListenerConfig(SerializationService serializationService) {
validate();
ListenerConfig listenerConfig = null;
if (className != null) {
switch(listenerType) {
case GENERIC:
listenerConfig = new ListenerConfig(className);
break;
case ITEM:
listenerConfig = new ItemListenerConfig(className, includeValue);
break;
case ENTRY:
listenerConfig = new EntryListenerConfig(className, local, includeValue);
break;
case SPLIT_BRAIN_PROTECTION:
listenerConfig = new SplitBrainProtectionListenerConfig(className);
break;
case CACHE_PARTITION_LOST:
listenerConfig = new CachePartitionLostListenerConfig(className);
break;
case MAP_PARTITION_LOST:
listenerConfig = new MapPartitionLostListenerConfig(className);
break;
default:
}
} else {
EventListener eventListener = serializationService.toObject(listenerImplementation);
switch(listenerType) {
case GENERIC:
listenerConfig = new ListenerConfig(eventListener);
break;
case ITEM:
listenerConfig = new ItemListenerConfig((ItemListener) eventListener, includeValue);
break;
case ENTRY:
listenerConfig = new EntryListenerConfig((MapListener) eventListener, local, includeValue);
break;
case SPLIT_BRAIN_PROTECTION:
listenerConfig = new SplitBrainProtectionListenerConfig((SplitBrainProtectionListener) eventListener);
break;
case CACHE_PARTITION_LOST:
listenerConfig = new CachePartitionLostListenerConfig((CachePartitionLostListener) eventListener);
break;
case MAP_PARTITION_LOST:
listenerConfig = new MapPartitionLostListenerConfig((MapPartitionLostListener) eventListener);
break;
default:
}
}
return (T) listenerConfig;
}
Aggregations