use of com.hazelcast.core.DistributedObjectListener in project hazelcast by hazelcast.
the class TestCacheManager method testCacheNames.
@Test
public void testCacheNames() {
// create a test instance, to reproduce the behavior described in the GitHub issue
// https://github.com/hazelcast/hazelcast/issues/492
final String testMap = "test-map";
final CountDownLatch distributionSignal = new CountDownLatch(1);
instance.addDistributedObjectListener(new DistributedObjectListener() {
@Override
public void distributedObjectCreated(DistributedObjectEvent event) {
DistributedObject distributedObject = event.getDistributedObject();
if (distributedObject instanceof IMap) {
IMap<?, ?> map = (IMap) distributedObject;
if (testMap.equals(map.getName())) {
distributionSignal.countDown();
}
}
}
@Override
public void distributedObjectDestroyed(DistributedObjectEvent event) {
}
});
HazelcastInstance testInstance = Hazelcast.newHazelcastInstance();
testInstance.getMap(testMap);
// be sure that test-map is distributed
HazelcastTestSupport.assertOpenEventually(distributionSignal);
Collection<String> test = cacheManager.getCacheNames();
assertContains(test, testMap);
testInstance.shutdown();
}
use of com.hazelcast.core.DistributedObjectListener in project hazelcast by hazelcast.
the class QueueEvictionTest method testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted.
@Test
public void testQueueEviction_whenTtlIsZero_thenListenersAreNeverthelessExecuted() throws Exception {
String queueName = randomString();
Config config = new Config();
config.getQueueConfig(queueName).setEmptyQueueTtl(0);
HazelcastInstance hz = createHazelcastInstance(config);
final CountDownLatch latch = new CountDownLatch(2);
hz.addDistributedObjectListener(new DistributedObjectListener() {
public void distributedObjectCreated(DistributedObjectEvent event) {
latch.countDown();
}
public void distributedObjectDestroyed(DistributedObjectEvent event) {
latch.countDown();
}
});
IQueue<Object> queue = hz.getQueue(queueName);
assertTrue(queue.offer("item"));
assertEquals("item", queue.poll());
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
use of com.hazelcast.core.DistributedObjectListener in project hazelcast by hazelcast.
the class HazelcastOSGiInstanceTest method addDistributedObjectListenerCalledSuccessfullyOverOSGiInstance.
@Test
public void addDistributedObjectListenerCalledSuccessfullyOverOSGiInstance() {
DistributedObjectListener mockDistributedObjectListener = mock(DistributedObjectListener.class);
HazelcastInstance mockHazelcastInstance = mock(HazelcastInstance.class);
HazelcastOSGiInstance hazelcastOSGiInstance = HazelcastOSGiTestUtil.createHazelcastOSGiInstance(mockHazelcastInstance);
when(mockHazelcastInstance.addDistributedObjectListener(mockDistributedObjectListener)).thenReturn("my-registration-id");
assertEquals("my-registration-id", hazelcastOSGiInstance.addDistributedObjectListener(mockDistributedObjectListener));
verify(mockHazelcastInstance).addDistributedObjectListener(mockDistributedObjectListener);
}
use of com.hazelcast.core.DistributedObjectListener in project hazelcast by hazelcast.
the class Node method initializeListeners.
@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity" })
private void initializeListeners(Config config) {
for (final ListenerConfig listenerCfg : config.getListenerConfigs()) {
Object listener = listenerCfg.getImplementation();
if (listener == null) {
try {
listener = ClassLoaderUtil.newInstance(configClassLoader, listenerCfg.getClassName());
} catch (Exception e) {
logger.severe(e);
}
}
if (listener instanceof HazelcastInstanceAware) {
((HazelcastInstanceAware) listener).setHazelcastInstance(hazelcastInstance);
}
boolean known = false;
if (listener instanceof DistributedObjectListener) {
final ProxyServiceImpl proxyService = (ProxyServiceImpl) nodeEngine.getProxyService();
proxyService.addProxyListener((DistributedObjectListener) listener);
known = true;
}
if (listener instanceof MembershipListener) {
clusterService.addMembershipListener((MembershipListener) listener);
known = true;
}
if (listener instanceof MigrationListener) {
partitionService.addMigrationListener((MigrationListener) listener);
known = true;
}
if (listener instanceof PartitionLostListener) {
partitionService.addPartitionLostListener((PartitionLostListener) listener);
known = true;
}
if (listener instanceof LifecycleListener) {
hazelcastInstance.lifecycleService.addLifecycleListener((LifecycleListener) listener);
known = true;
}
if (listener instanceof ClientListener) {
String serviceName = ClientEngineImpl.SERVICE_NAME;
nodeEngine.getEventService().registerLocalListener(serviceName, serviceName, listener);
known = true;
}
if (listener instanceof InternalMigrationListener) {
final InternalPartitionServiceImpl partitionService = (InternalPartitionServiceImpl) nodeEngine.getPartitionService();
partitionService.setInternalMigrationListener((InternalMigrationListener) listener);
known = true;
}
if (nodeExtension.registerListener(listener)) {
known = true;
}
if (listener != null && !known) {
final String error = "Unknown listener type: " + listener.getClass();
Throwable t = new IllegalArgumentException(error);
logger.warning(error, t);
}
}
}
use of com.hazelcast.core.DistributedObjectListener in project hazelcast by hazelcast.
the class QueueListenerTest method testListener_withEvictionViaTTL.
@Test
public void testListener_withEvictionViaTTL() throws Exception {
Config config = new Config();
config.getQueueConfig("queueWithTTL").setEmptyQueueTtl(0);
HazelcastInstance hz = createHazelcastInstance(config);
final CountDownLatch latch = new CountDownLatch(2);
hz.addDistributedObjectListener(new DistributedObjectListener() {
@Override
public void distributedObjectCreated(DistributedObjectEvent event) {
latch.countDown();
}
@Override
public void distributedObjectDestroyed(DistributedObjectEvent event) {
latch.countDown();
}
});
IQueue<Object> queue = hz.getQueue("queueWithTTL");
queue.offer("item");
queue.poll();
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Aggregations