Search in sources :

Example 1 with MessageListener

use of com.hazelcast.core.MessageListener 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 2 with MessageListener

use of com.hazelcast.core.MessageListener 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 3 with MessageListener

use of com.hazelcast.core.MessageListener 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)

Example 4 with MessageListener

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

the class ClientReliableTopicTest method testRemoveListener.

@Test
public void testRemoveListener() {
    ITopic topic = client.getReliableTopic(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) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with MessageListener

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

the class AllTest method loadTopicOperations.

private List<Runnable> loadTopicOperations() {
    ITopic topic = hazelcast.getTopic("myTopic");
    topic.addMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            messagesReceived.incrementAndGet();
        }
    });
    List<Runnable> operations = new ArrayList<Runnable>();
    addOperation(operations, new Runnable() {

        public void run() {
            ITopic topic = hazelcast.getTopic("myTopic");
            topic.publish(String.valueOf(random.nextInt(100000000)));
            messagesSend.incrementAndGet();
        }
    }, 10);
    return operations;
}
Also used : ITopic(com.hazelcast.core.ITopic) Message(com.hazelcast.core.Message) MessageListener(com.hazelcast.core.MessageListener) ArrayList(java.util.ArrayList)

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