use of com.hazelcast.core.MessageListener in project hazelcast by hazelcast.
the class TopicTest method addTwoListenerAndRemoveOne.
@Test
public void addTwoListenerAndRemoveOne() throws InterruptedException {
String topicName = "addTwoListenerAndRemoveOne" + generateRandomString(5);
HazelcastInstance instance = createHazelcastInstance();
ITopic<String> topic = instance.getTopic(topicName);
final CountDownLatch latch = new CountDownLatch(3);
final CountDownLatch cp = new CountDownLatch(2);
final AtomicInteger atomicInteger = new AtomicInteger();
final String message = "Hazelcast Rocks!";
MessageListener<String> messageListener1 = new MessageListener<String>() {
public void onMessage(Message<String> msg) {
atomicInteger.incrementAndGet();
latch.countDown();
cp.countDown();
}
};
MessageListener<String> messageListener2 = new MessageListener<String>() {
public void onMessage(Message<String> msg) {
atomicInteger.incrementAndGet();
latch.countDown();
cp.countDown();
}
};
String messageListenerId = topic.addMessageListener(messageListener1);
topic.addMessageListener(messageListener2);
topic.publish(message);
assertOpenEventually(cp);
topic.removeMessageListener(messageListenerId);
topic.publish(message);
assertOpenEventually(latch);
assertEquals(3, atomicInteger.get());
}
use of com.hazelcast.core.MessageListener in project hazelcast by hazelcast.
the class TopicTest method testConfigListenerRegistration.
@Test
public void testConfigListenerRegistration() throws InterruptedException {
String topicName = "default";
Config config = new Config();
final CountDownLatch latch = new CountDownLatch(1);
config.getTopicConfig(topicName).addMessageListenerConfig(new ListenerConfig().setImplementation(new MessageListener() {
public void onMessage(Message message) {
latch.countDown();
}
}));
HazelcastInstance instance = createHazelcastInstance(config);
instance.getTopic(topicName).publish(1);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
use of com.hazelcast.core.MessageListener in project hazelcast by hazelcast.
the class ClientReliableTopicTest method testListener.
@Test
public void testListener() throws InterruptedException {
ITopic topic = client.getReliableTopic(randomString());
int messageCount = 10;
final CountDownLatch latch = new CountDownLatch(messageCount);
MessageListener listener = new MessageListener() {
public void onMessage(Message message) {
latch.countDown();
}
};
topic.addMessageListener(listener);
for (int i = 0; i < messageCount; i++) {
topic.publish(i);
}
assertTrue(latch.await(20, TimeUnit.SECONDS));
}
use of com.hazelcast.core.MessageListener in project hazelcast by hazelcast.
the class ClientTopicTest method testListener.
@Test
public void testListener() throws InterruptedException {
ITopic topic = client.getTopic(randomString());
final CountDownLatch latch = new CountDownLatch(10);
MessageListener listener = new MessageListener() {
public void onMessage(Message message) {
latch.countDown();
}
};
topic.addMessageListener(listener);
for (int i = 0; i < 10; i++) {
topic.publish(i);
}
assertTrue(latch.await(20, TimeUnit.SECONDS));
}
use of com.hazelcast.core.MessageListener in project hazelcast by hazelcast.
the class ClientTopicTest method testRemoveListener.
@Test
public void testRemoveListener() {
ITopic topic = client.getTopic(randomString());
MessageListener listener = new MessageListener() {
public void onMessage(Message message) {
}
};
String id = topic.addMessageListener(listener);
assertTrue(topic.removeMessageListener(id));
}
Aggregations