美文网首页
ActiveMQ环境搭建及性能测试

ActiveMQ环境搭建及性能测试

作者: Gallrax | 来源:发表于2018-03-04 18:40 被阅读0次

    虚拟机环境

    cpu:E5-1650 v2 分配2个物理核心 每个核心2两个线程
    内存:4G

    环境搭建

    >wget http://www.apache.org/dyn/closer.cgi?filename=/activemq/5.15.3/apache-activemq-5.15.3-bin.tar.gz&action=download
    >tar -zxvf apache-activemq-5.15.3-bin.tar.gz
    >./activemq start
    

    性能测试

    pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    

    application.yml

    spring:
      activemq:
        broker-url: tcp://192.168.66.6:61616
        user: admin
        password: admin
    # 若想activemq启用topic,需开启此项
      jms:
        pub-sub-domain: true
    

    provider

    package com.activemq.client;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.stereotype.Component;
    
    import javax.jms.Queue;
    import javax.jms.Topic;
    
    @Component
    public class ActiveMQClient {
    
        @Autowired
        private JmsTemplate jmsTemplate;
        @Autowired
        private Queue queue;
        @Autowired
        private Topic topic;
    
        public void send(String message) {
            //默认不注入Queue和Topic时,使用Queue
    //        jmsTemplate.convertAndSend("testMQ", message);
            //1.测试queue性能
    //        jmsTemplate.convertAndSend(queue, message);
            //2.测试topic性能
            jmsTemplate.convertAndSend(topic, message);
        }
    
    }
    
    

    consumer

    package com.activemq.server;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    
    import javax.jms.Queue;
    
    @Component
    public class ActiveMQServer {
    
    //    @JmsListener(destination = "testMQ")
    //    @JmsListener(destination = "testMQ-queue")
        @JmsListener(destination = "testMQ-topic")
        public void receive(String message) {
            System.out.println(" now : "+ System.currentTimeMillis());
            System.out.println(" activeMQ receive : " + message);
        }
    }
    
    

    测试类

    package com.test;
    
    import com.Application;
    import com.activemq.client.ActiveMQClient;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.util.StopWatch;
    
    import java.io.IOException;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    public class ActiveMQTest {
    
        @Autowired
        private ActiveMQClient activeMQClient;
    
        /*
            queue
            ActiveMQ 生产消费同时启动   生产10000条数据:89000ms
            ActiveMQ 只生产不消费启动   生产10000条数据:84000ms
            ActiveMQ 只消费不生产启动   消费10000条数据:14471ms
            topic
            ActiveMQ 生产消费同时启动   生产10000条数据:80605ms
            ActiveMQ 只生产不消费启动   生产10000条数据:78867ms
            ActiveMQ 只消费不生产启动   消费10000条数据:无法设置
         */
        @Test
        public void test01() {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            for (int i = 0; i < 10000; i++) {
                activeMQClient.send("send : " + i);
            }
            stopWatch.stop();
            System.out.println(" send 10000 共耗时 : " + stopWatch.getTotalTimeMillis());
        }
    
        @Test
        public void test02() throws IOException {
            //项目启动之后,Queue消费者则自动消费队列数据,根据消费者提供时间进行计算
            System.in.read();
        }
    
    }
    
    

    项目地址

    gitee:https://gitee.com/gallrax/TestMQ
    github:https://github.com/gallrax/TestMQ
    

    相关文章

      网友评论

          本文标题:ActiveMQ环境搭建及性能测试

          本文链接:https://www.haomeiwen.com/subject/lykqfftx.html