Search in sources :

Example 11 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)

Example 12 with MessageListener

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();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Message(com.hazelcast.core.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(com.hazelcast.core.MessageListener) AssertTask(com.hazelcast.test.AssertTask) 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) Repeat(com.hazelcast.test.annotation.Repeat)

Example 13 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 14 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)

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