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));
}
use of com.hazelcast.core.MessageListener in project hazelcast by hazelcast.
the class TopicTest method removeMessageListener.
@Test
@Repeat(10)
public void removeMessageListener() throws InterruptedException {
String topicName = "removeMessageListener" + generateRandomString(5);
try {
HazelcastInstance instance = createHazelcastInstance();
ITopic<String> topic = instance.getTopic(topicName);
final AtomicInteger onMessageCount = new AtomicInteger(0);
final CountDownLatch onMessageInvoked = new CountDownLatch(1);
MessageListener<String> messageListener = new MessageListener<String>() {
public void onMessage(Message<String> msg) {
onMessageCount.incrementAndGet();
onMessageInvoked.countDown();
}
};
final String message = "message_" + messageListener.hashCode() + "_";
final String id = topic.addMessageListener(messageListener);
topic.publish(message + "1");
onMessageInvoked.await();
assertTrue(topic.removeMessageListener(id));
topic.publish(message + "2");
assertTrueEventually(new AssertTask() {
@Override
public void run() {
assertEquals(1, onMessageCount.get());
}
});
} finally {
shutdownNodeFactory();
}
}
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));
}
Aggregations