赞
踩
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo02-mq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo02-mq</name>
<description>demo02-mq</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.6.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.demo02.mq.Demo02MqApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.example.demo02.mq.util;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author Allen
* 4/10/2024 7:25 PM
* @version 1.0
* @description: MQ连接工具类
*
*/
public class ConnectionUtils {
//为什么使用静态代码块初始化连接工厂?
//因为连接工厂只需要初始化一次,所以使用静态代码块初始化
private static ConnectionFactory connectionFactory;
static {
// 创建连接工厂
connectionFactory = new ConnectionFactory();
//mq服务主机地址
connectionFactory.setHost("*********");
//连接端口
connectionFactory.setPort(40991);
connectionFactory.setVirtualHost("/my240410");
//设置用户名
connectionFactory.setUsername("allen");
//设置密码
connectionFactory.setPassword("123456");
}
public static Connection getConnection() {
try {
//返回连接 通过工厂获取连接
return connectionFactory.newConnection();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
@SpringBootTest
class Demo02MqApplicationTests {
@Test
void contextLoads() {
Connection connection = ConnectionUtils.getConnection();
System.out.println(connection);
}
}
结果:
amqp://allen@************//my240410
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。