Search in sources :

Example 1 with Message

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

the class TopicTest method testTopicCluster.

/**
     * Testing if topic can properly listen messages and if topic has any issue after a shutdown.
     */
@Test
public void testTopicCluster() throws InterruptedException {
    String topicName = "TestMessages" + generateRandomString(5);
    Config cfg = new Config();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(cfg);
    HazelcastInstance instance1 = instances[0];
    HazelcastInstance instance2 = instances[1];
    ITopic<String> topic1 = instance1.getTopic(topicName);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final String message = "Test" + randomString();
    topic1.addMessageListener(new MessageListener<String>() {

        public void onMessage(Message msg) {
            assertEquals(message, msg.getMessageObject());
            latch1.countDown();
        }
    });
    ITopic<String> topic2 = instance2.getTopic(topicName);
    final CountDownLatch latch2 = new CountDownLatch(2);
    topic2.addMessageListener(new MessageListener<String>() {

        public void onMessage(Message msg) {
            assertEquals(message, msg.getMessageObject());
            latch2.countDown();
        }
    });
    topic1.publish(message);
    assertOpenEventually(latch1);
    instance1.shutdown();
    topic2.publish(message);
    assertOpenEventually(latch2);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Message(com.hazelcast.core.Message) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with Message

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

the class TopicTest method testTopicStats.

@Test
public void testTopicStats() throws InterruptedException {
    String topicName = "testTopicStats" + generateRandomString(5);
    HazelcastInstance instance = createHazelcastInstance();
    ITopic<String> topic = instance.getTopic(topicName);
    final CountDownLatch latch1 = new CountDownLatch(1000);
    topic.addMessageListener(new MessageListener<String>() {

        public void onMessage(Message msg) {
            latch1.countDown();
        }
    });
    final CountDownLatch latch2 = new CountDownLatch(1000);
    topic.addMessageListener(new MessageListener<String>() {

        public void onMessage(Message msg) {
            latch2.countDown();
        }
    });
    for (int i = 0; i < 1000; i++) {
        topic.publish("sancar");
    }
    assertTrue(latch1.await(1, TimeUnit.MINUTES));
    assertTrue(latch2.await(1, TimeUnit.MINUTES));
    LocalTopicStatsImpl stats = (LocalTopicStatsImpl) topic.getLocalTopicStats();
    assertEquals(1000, stats.getPublishOperationCount());
    assertEquals(2000, stats.getReceiveOperationCount());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Message(com.hazelcast.core.Message) LocalTopicStatsImpl(com.hazelcast.monitor.impl.LocalTopicStatsImpl) 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 3 with Message

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

the class TopicTest method testTopicMultiThreading.

@Test
@Category(NightlyTest.class)
@SuppressWarnings("unchecked")
public void testTopicMultiThreading() throws Exception {
    final int nodeCount = 5;
    final int count = 1000;
    final String randomTopicName = randomString();
    Config config = new Config();
    config.getTopicConfig(randomTopicName).setGlobalOrderingEnabled(false);
    config.getTopicConfig(randomTopicName).setMultiThreadingEnabled(true);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final Set<String>[] threads = new Set[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        threads[i] = new HashSet<String>();
    }
    final CountDownLatch startLatch = new CountDownLatch(nodeCount);
    final CountDownLatch messageLatch = new CountDownLatch(nodeCount * nodeCount * count);
    final CountDownLatch publishLatch = new CountDownLatch(nodeCount * count);
    ExecutorService ex = Executors.newFixedThreadPool(nodeCount);
    for (int i = 0; i < nodeCount; i++) {
        final int finalI = i;
        ex.execute(new Runnable() {

            public void run() {
                final Set<String> thNames = threads[finalI];
                HazelcastInstance hz = instances[finalI];
                ITopic<TestMessage> topic = hz.getTopic(randomTopicName);
                topic.addMessageListener(new MessageListener<TestMessage>() {

                    public void onMessage(Message<TestMessage> message) {
                        thNames.add(Thread.currentThread().getName());
                        messageLatch.countDown();
                    }
                });
                startLatch.countDown();
                try {
                    startLatch.await(1, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
                Member localMember = hz.getCluster().getLocalMember();
                for (int j = 0; j < count; j++) {
                    topic.publish(new TestMessage(localMember, UuidUtil.newUnsecureUuidString()));
                    publishLatch.countDown();
                }
            }
        });
    }
    try {
        assertTrue(publishLatch.await(2, TimeUnit.MINUTES));
        assertTrue(messageLatch.await(5, TimeUnit.MINUTES));
        boolean passed = false;
        for (int i = 0; i < nodeCount; i++) {
            if (threads[i].size() > 1) {
                passed = true;
            }
        }
        assertTrue("All listeners received messages in single thread. Expecting more threads involved", passed);
    } finally {
        ex.shutdownNow();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ITopic(com.hazelcast.core.ITopic) 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) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ExecutorService(java.util.concurrent.ExecutorService) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) Category(org.junit.experimental.categories.Category) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with Message

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

the class TopicTest method testTopicLocalOrder.

@Test
@SuppressWarnings("unchecked")
public void testTopicLocalOrder() throws Exception {
    final int nodeCount = 5;
    final int count = 1000;
    final String randomTopicName = randomString();
    Config config = new Config();
    config.getTopicConfig(randomTopicName).setGlobalOrderingEnabled(false);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final List<TestMessage>[] messageLists = new List[nodeCount];
    for (int i = 0; i < nodeCount; i++) {
        messageLists[i] = new CopyOnWriteArrayList<TestMessage>();
    }
    final CountDownLatch startLatch = new CountDownLatch(nodeCount);
    final CountDownLatch messageLatch = new CountDownLatch(nodeCount * nodeCount * count);
    final CountDownLatch publishLatch = new CountDownLatch(nodeCount * count);
    ExecutorService ex = Executors.newFixedThreadPool(nodeCount);
    for (int i = 0; i < nodeCount; i++) {
        final int finalI = i;
        ex.execute(new Runnable() {

            public void run() {
                final List<TestMessage> messages = messageLists[finalI];
                HazelcastInstance hz = instances[finalI];
                ITopic<TestMessage> topic = hz.getTopic(randomTopicName);
                topic.addMessageListener(new MessageListener<TestMessage>() {

                    public void onMessage(Message<TestMessage> message) {
                        messages.add(message.getMessageObject());
                        messageLatch.countDown();
                    }
                });
                startLatch.countDown();
                try {
                    startLatch.await(1, TimeUnit.MINUTES);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
                Member localMember = hz.getCluster().getLocalMember();
                for (int j = 0; j < count; j++) {
                    topic.publish(new TestMessage(localMember, UuidUtil.newUnsecureUuidString()));
                    publishLatch.countDown();
                }
            }
        });
    }
    try {
        assertTrue(publishLatch.await(2, TimeUnit.MINUTES));
        assertTrue(messageLatch.await(5, TimeUnit.MINUTES));
        TestMessage[] ref = new TestMessage[messageLists[0].size()];
        messageLists[0].toArray(ref);
        Comparator<TestMessage> comparator = new Comparator<TestMessage>() {

            public int compare(TestMessage m1, TestMessage m2) {
                // sort only publisher blocks. if publishers are the same, leave them as they are
                return m1.publisher.getUuid().compareTo(m2.publisher.getUuid());
            }
        };
        Arrays.sort(ref, comparator);
        for (int i = 1; i < nodeCount; i++) {
            TestMessage[] messages = new TestMessage[messageLists[i].size()];
            messageLists[i].toArray(messages);
            Arrays.sort(messages, comparator);
            assertArrayEquals(ref, messages);
        }
    } finally {
        ex.shutdownNow();
    }
}
Also used : ITopic(com.hazelcast.core.ITopic) 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) Comparator(java.util.Comparator) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with Message

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

the class TopicService method dispatchEvent.

@Override
public void dispatchEvent(Object event, Object listener) {
    TopicEvent topicEvent = (TopicEvent) event;
    ClusterService clusterService = nodeEngine.getClusterService();
    MemberImpl member = clusterService.getMember(topicEvent.publisherAddress);
    if (member == null) {
        member = new MemberImpl(topicEvent.publisherAddress, nodeEngine.getVersion(), false);
    }
    Message message = new DataAwareMessage(topicEvent.name, topicEvent.data, topicEvent.publishTime, member, nodeEngine.getSerializationService());
    incrementReceivedMessages(topicEvent.name);
    MessageListener messageListener = (MessageListener) listener;
    messageListener.onMessage(message);
}
Also used : ClusterService(com.hazelcast.internal.cluster.ClusterService) Message(com.hazelcast.core.Message) MemberImpl(com.hazelcast.instance.MemberImpl) MessageListener(com.hazelcast.core.MessageListener)

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