package com.xiaoyuan.handle.msgNotify.utils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class RabbitMqUtils {
@Value("${spring.rabbitmq.host}")
static String host;
@Value("${spring.rabbitmq.password}")
static String password;
@Value("${spring.rabbitmq.username}")
static String username;
public static ConnectionFactory connectionFactory;
static {
connectionFactory = new ConnectionFactory();
connectionFactory.setHost(RabbitMqUtils.host);
connectionFactory.setPort(5672);
connectionFactory.setUsername(RabbitMqUtils.username);
connectionFactory.setPassword(RabbitMqUtils.password);
connectionFactory.setVirtualHost("/");
}
public static Connection getonnection() {
try {
return connectionFactory.newConnection();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭
*
* @param channel
* @param connection
* @throws Exception
*/
public static void closeChannelAndConnection(Channel channel, Connection connection) throws Exception {
if (channel != null) channel.close();
if (connection != null) connection.close();
}
}
mq的一个utils
最新推荐文章于 2024-10-01 08:06:48 发布