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();
}
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);
}
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;
}
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);
}
}
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();
}
}
Aggregations