使用MQ
Java获取MQ连接对象
SpringBoot Pom 文件
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.0modelVersion>
groupId>com.examplegroupId>
artifactId>demo02-mqartifactId>
version>0.0.1-SNAPSHOTversion>
name>demo02-mqname>
description>demo02-mqdescription>
properties>
java.version>1.8java.version>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
spring-boot.version>2.3.6.RELEASEspring-boot.version>
properties>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starterartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-amqpartifactId>
dependency>
dependencies>
dependencyManagement>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-dependenciesartifactId>
version>${spring-boot.version}version>
type>pomtype>
服务器托管 scope>importscope>
dependency>
dependencies>
dependencyManagement>
build>
plugins>
plugin>
groupId>org.apache.maven.pluginsgroupId>
artifactId>maven-compiler-pluginartifactId>
version>3.8.1version>
configuration>
source>1.8source>
target>1.8target>
encoding>UTF-8encoding>
configuration>
plugin>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
version>${spring-boot.version}version>
configuration>
mainClass>com.example.demo02.mq.Demo02MqApplicationmainClass>
skip>trueskip>
configuration>
executions>
execution>
id>repackageid>
goals>
goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
connection Util 类
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
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 二十四种设计模式与六大设计原则(三):【装饰模式、迭代器模式、组合模式、观察者模式、责任链模式、访问者模式】的定义、举例说明、核心思想、适用场景和优缺点
接上次博客:二十四种设计模式与六大设计原则(二):【门面模式、适配器模式、模板方法模式、建造者模式、桥梁模式、命令模式】的定义、举例说明、核心思想、适用场景和优缺点-CSDN博客 目录 装饰模式【Decorator Pattern】 定义 举例说明 核心思想 …