• rabbitMq完整通信(三)---测试类


    producer下面 :

    测试类一:

    package producer;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.CountDownLatch;
    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.SpringJUnit4ClassRunner;
    import com..ClientApplication;
    import com..sender.RabbitSender;
    
    @SpringBootTest(classes = ClientApplication.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestRabbitMQ {
    
        // 并发量
        private static final int USER_NUM = 10;
        // 倒计时器,用于模拟高并发
        private static CountDownLatch cdl = new CountDownLatch(USER_NUM);
        
        @Autowired
        private RabbitSender rabbitSender;
    
        @Test
        public void testRabbit() {
            String queueName = "queue";
            String orderId = "123456";
            rabbitSender.send(queueName,orderId);
        }
        @Test
        public void testRabbitObject() {
            String queueName = "queueObject";
            Map<String,Object> user = new HashMap<String,Object>(); // 实现Serializable接口
            user.put("name", "张三");
            user.put("age", 24);
            rabbitSender.sendObject(queueName,user);
        }
        @Test
        public void testRabbitTopic() {
            String exchange = "exchange";
            String queueName = "topic.order";
            String orderId = "123456";
            rabbitSender.sendTopic(exchange,queueName,orderId);
        }
        
        
        //并发模拟
        @Test
        public void testStressRabbitTopic() throws InterruptedException {
            // 循环实例化USER_NUM个并发请求(线程)
            for (int i = 0; i < USER_NUM; i++) {
                new Thread(new UserRequst()).start();
                System.out.println("减一之前的线程:"+Thread.currentThread().getName());
                cdl.countDown();// 倒计时器减一
            }
            
            Thread.currentThread().sleep(2000);
    
        }
        
        // 内部类继承线程接口,用于模拟用户请求
            public class UserRequst implements Runnable {        
                @Override
                public void run() {
                    try {
                        System.out.println("即将等待的线程:"+Thread.currentThread().getName());
                        cdl.await();// 当前线程等待,等所以线程实例化完成后,同时停止等待后调用接口代码
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    String exchange = "exchange";
                    String queueName = "topic.order";
                    String orderId = "123456";
                    rabbitSender.sendTopic(exchange,queueName,orderId);
                }
            }
        }

    测试类二:

    package producer;
    
    import java.util.concurrent.CountDownLatch;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.web.client.RestTemplate;
    import com..ClientApplication;
    
    @SpringBootTest(classes = ClientApplication.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestInvokeRemote {
    
        RestTemplate restTemplate = new RestTemplate();
        // 并发量
        private static final int USER_NUM = 5;
        // 倒计时器,用于模拟高并发
        private static CountDownLatch cdl = new CountDownLatch(USER_NUM);
        private final String url = "http://127.0.0.1:8090/queryOrderInfo?orderId=123456";
    
        @Test
        public void testInvokeRemote() throws InterruptedException {
            // 循环实例化USER_NUM个并发请求(线程)
            for (int i = 0; i < USER_NUM; i++) {
                new Thread(new UserRequst()).start();
                cdl.countDown();// 倒计时器减一
            }
            Thread.currentThread().sleep(2000);
       }
    
        // 内部类继承线程接口,用于模拟用户请求
        public class UserRequst implements Runnable {        
            @Override
            public void run() {
                try {
                    cdl.await();// 当前线程等待,等所以线程实例化完成后,同时停止等待后调用接口代码
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String str = restTemplate.getForEntity(url, String.class).getBody();
                System.out.println(str);
            }
        }
    }
  • 相关阅读:
    Datawhale-新闻文本分类-task4-基于深度学习的文本分类1-fastText
    Datawhale-新闻文本分类-task3-机器学习分类
    (String)、toString、String.valueOf的区别
    抽象类与接口
    内部类详解
    关键字this---static---final
    Intent系列讲解---Intent简介以及相关属性
    Activity系列讲解---Activity的四大启动模式
    Activity系列讲解---Activity运行时的屏幕方向,全屏,窗体模式的设置
    Activity系列讲解---返回结果的处理
  • 原文地址:https://www.cnblogs.com/lifan12589/p/14309045.html
Copyright © 2020-2023  润新知