springboot项目,@Test写法 @Before @After

某文件示例

复制代码
package cn.xxx.crm.boss;

import cn.xxxx.crm.manager.mq.rabbit.AliyunCredentialsProvider;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author zss
 * @date 2022/4/25 14:55
 * @Email:451682479@qq.com
 */
public class MqTest {

    public static final String HOST = "qwer.mq-amqp.cn-hangzhou-111111-a.aliyuncs.com";
    public static final String resourceOwnerId = "qwert";
    public static final String USERNAME = "aaaaa";
    public static final String PASSWORD = "xxxx";
    public static final String V_HOST = "DevCrmHost1";
    public static final int PORT = 5672;

    private ConnectionFactory factory;
    private Connection connection;

    @Before
    public void before() {
        factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);
        factory.setVirtualHost(V_HOST);
        factory.setUsername(USERNAME);
        factory.setPassword(PASSWORD);

        AliyunCredentialsProvider credentialsProvider = new AliyunCredentialsProvider(
            USERNAME, PASSWORD, resourceOwnerId);
        factory.setCredentialsProvider(credentialsProvider);
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(5000);
        // 基于网络环境合理设置超时时间。
        factory.setConnectionTimeout(30 * 1000);
        factory.setHandshakeTimeout(30 * 1000);
        factory.setShutdownTimeout(0);

    }


    @After
    public void after() throws IOException {
        if (null != connection) {
            connection.close();
        }
    }


    @Test
    public void test() throws IOException, TimeoutException {
        connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("test-direct-exchange", "direct", true, false, false, null);
        channel.queueDeclare("test-queue1", true, false, false, new HashMap<String, Object>());
        channel.queueBind("test-queue1", "test-direct-exchange", "aaaa");
        // 开始发送消息。
        for (int i = 0; i < 100; i++) {
            // ${ExchangeName}必须在消息队列RabbitMQ版控制台上已存在,并且Exchange的类型与控制台上的类型一致。
            // BindingKey根据业务需求填入相应的BindingKey。
            AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().messageId(UUID.randomUUID().toString())
                .build();
            channel.basicPublish("test-direct-exchange", "aaaa", true, props,
                ("消息发送Body" + i).getBytes(StandardCharsets.UTF_8));

            String a = "消息发送Body" + i;
            System.out.println(a);

        }
    }
}
相关推荐
Coder_Boy_11 小时前
技术交流总结:分布式、数据库、Spring及SpringBoot核心知识点梳理(实现参考)
数据库·spring boot·分布式·spring·架构
想不明白的过度思考者11 小时前
Spring Boot 实战:MyBatis 操作数据库(上)
java·数据库·spring boot·mysql·mybatis
rfidunion1 天前
springboot+VUE+部署(12。Nginx和前端配置遇到的问题)
前端·vue.js·spring boot
FYKJ_20101 天前
springboot大学校园论坛管理系统--附源码42669
java·javascript·spring boot·python·spark·django·php
QQ24391971 天前
语言在线考试与学习交流网页平台信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·spring boot·sql·学习·java-ee
毕设源码-小云学姐1 天前
计算机毕业设计springboot医疗档案管理系统 基于 SpringBoot 的电子医疗档案管理系统的设计与实现 SpringBoot 框架下的医疗档案信息化管理系统开发
spring boot·后端·课程设计
想不明白的过度思考者1 天前
JavaEE进阶 ——【SpringBoot 快速上手】从环境搭建到HelloWorld实战
java·spring boot·spring·java-ee
那我掉的头发算什么1 天前
【SpringBoot】一篇文章讲清楚拦截器所有知识
java·spring boot·后端·spring
百锦再1 天前
Java中的反射机制详解:从原理到实践的全面剖析
java·开发语言·jvm·spring boot·struts·spring cloud·kafka
树码小子1 天前
图书管理系统(5)强制登陆(后端实现)
spring boot·mybatis·图书管理系统