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);

        }
    }
}
相关推荐
while(1){yan}2 分钟前
Mybatis基础(详解)
spring boot·spring·java-ee·mybatis
sheji341628 分钟前
【开题答辩全过程】以 基于springboot的健身房管理系统为例,包含答辩的问题和答案
java·spring boot·后端
张较瘦_3 小时前
Springboot3 | 核心注解实战教程
java·spring boot
Java水解6 小时前
Spring Boot:Java开发的神奇加速器(二)
spring boot·后端
Coder_Boy_6 小时前
基于SpringAI的企业级智能教学考试:基础资源构建模块
人工智能·spring boot
Coder_Boy_7 小时前
基于SpringAI的企业级智能教学考试平台模块二:核心执行模块内容
人工智能·spring boot
独自归家的兔7 小时前
基于 Doubao-Seedream-4.5 的单张图片生成后端接口实战
java·人工智能·spring boot·后端
刘一说8 小时前
Tomcat在Spring Boot集成原理及优化应用:深度解析与实战指南
java·spring boot·tomcat
禹中一只鱼8 小时前
【SpringBoot 配置文件】
java·spring boot·后端
JavaGuide8 小时前
Spring Boot 3.3+Java 21+Spring AI+RustFS打造的智能面试平台开源啦!
spring boot·后端