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

        }
    }
}
相关推荐
小编码上说38 分钟前
LSH(局部敏感哈希)分桶,海量数据下的相似性搜索解决方案
java·spring boot·缓存·langchain4j·lsh·局部敏感哈希·ai调用优化
计算机_毕业设计1 小时前
java-springboot数字藏品系统 基于 SpringBoot 的区块链数字艺术品交易平台 Java 微服务架构下的加密藏品展示与拍卖系统计算机毕业设计
java·spring boot·课程设计
中冕—霍格沃兹软件开发测试1 小时前
区块链交易最终一致性测试的核心挑战与实践框架
微服务·架构·单元测试·区块链·集成测试·旅游
dovens2 小时前
SpringBoot集成MQTT客户端
java·spring boot·后端
❀͜͡傀儡师2 小时前
Spring Boot 集成 RocksDB 实战:打造高性能 KV 存储加速层
java·spring boot·后端·rocksdb
NaMM CHIN2 小时前
Spring Boot + Spring AI快速体验
人工智能·spring boot·spring
ATCH IERV3 小时前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
钰衡大师5 小时前
Activiti 7 工作流技术文档
java·数据库·spring boot
测试员周周5 小时前
【AI测试系统】第5篇:AI 编码工具抛硬币?我们用 LangGraph 做了个“确定性+AI”的测试系统(附自愈架构)
人工智能·python·功能测试·测试工具·架构·langchain·单元测试
Ruci ALYS5 小时前
SpringBoot Maven快速上手
spring boot·后端·maven