Search in sources :

Example 6 with MessageListener

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());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Message(com.hazelcast.core.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with MessageListener

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));
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Message(com.hazelcast.core.Message) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with MessageListener

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));
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with MessageListener

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));
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with MessageListener

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));
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

MessageListener (com.hazelcast.core.MessageListener)14 Message (com.hazelcast.core.Message)11 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Test (org.junit.Test)9 ITopic (com.hazelcast.core.ITopic)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 NightlyTest (com.hazelcast.test.annotation.NightlyTest)5 Config (com.hazelcast.config.Config)3 ListenerConfig (com.hazelcast.config.ListenerConfig)3 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)2 Member (com.hazelcast.core.Member)2 NodeEngine (com.hazelcast.spi.NodeEngine)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 ReliableMessageListener (com.hazelcast.topic.ReliableMessageListener)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HazelcastException (com.hazelcast.core.HazelcastException)1 MemberImpl (com.hazelcast.instance.MemberImpl)1