Search in sources :

Example 16 with Message

use of com.hazelcast.core.Message 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 17 with Message

use of com.hazelcast.core.Message 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 18 with Message

use of com.hazelcast.core.Message in project hazelcast by hazelcast.

the class ReliableTopicAbstractTest method testMessageFieldSetCorrectly.

@Test
public void testMessageFieldSetCorrectly() {
    final ReliableMessageListenerMock listener = new ReliableMessageListenerMock();
    topic.addMessageListener(listener);
    final long beforePublishTime = Clock.currentTimeMillis();
    topic.publish("foo");
    final long afterPublishTime = Clock.currentTimeMillis();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, listener.messages.size());
            Message<String> message = listener.messages.get(0);
            assertEquals("foo", message.getMessageObject());
            Member localMember = local.getCluster().getLocalMember();
            assertEquals(localMember, message.getPublishingMember());
            long actualPublishTime = message.getPublishTime();
            assertTrue(actualPublishTime >= beforePublishTime);
            assertTrue(actualPublishTime <= afterPublishTime);
        }
    });
}
Also used : Message(com.hazelcast.core.Message) AssertTask(com.hazelcast.test.AssertTask) Member(com.hazelcast.core.Member) Test(org.junit.Test)

Aggregations

Message (com.hazelcast.core.Message)18 Test (org.junit.Test)15 ParallelTest (com.hazelcast.test.annotation.ParallelTest)12 QuickTest (com.hazelcast.test.annotation.QuickTest)12 MessageListener (com.hazelcast.core.MessageListener)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ITopic (com.hazelcast.core.ITopic)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 NightlyTest (com.hazelcast.test.annotation.NightlyTest)7 Config (com.hazelcast.config.Config)4 ListenerConfig (com.hazelcast.config.ListenerConfig)4 Member (com.hazelcast.core.Member)3 AssertTask (com.hazelcast.test.AssertTask)3 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 MemberImpl (com.hazelcast.instance.MemberImpl)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 ClusterService (com.hazelcast.internal.cluster.ClusterService)1 LocalTopicStatsImpl (com.hazelcast.monitor.impl.LocalTopicStatsImpl)1