public class RabbitMQUtil {
private static ConnectionFactory connectionFactory;
static {
//创建mq的连接工厂对象
connectionFactory = new ConnectionFactory();
//设置连接rabbit的主机地址
connectionFactory.setHost("192.168.17.156");
//设置连接端口
connectionFactory.setPort(5672);
//设置连接哪个虚拟主机
connectionFactory.setVirtualHost("/ems");
//设置虚拟主机的用户名和密码
connectionFactory.setUsername("wyn");
connectionFactory.setPassword("123456");
}
//定义一个获取连接
public static Connection getConnection(){
try {
//通过连接工厂获取连接
Connection connection = connectionFactory.newConnection();
return connection;
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
return null;
}
//关闭连接通道的方法
public static void closeConnectionChannel(Channel channel,Connection connection){
try {
if (channel!=null) channel.close();
if (connection!=null)connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
《消息中间件》~ Rabbit工具类 RabbitMQUtil
最新推荐文章于 2023-03-11 13:36:58 发布