import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class QueueUtil { private static Logger logger = LoggerFactory.getLogger(QueueUtil.class); private static Connection connection = null; private static String QUEUE_NAME = null; static { if (connection == null) { initConnection(); } } private QueueUtil() { } private static void initConnection() { try { ConnectionFactory factory = new ConnectionFactory(); String host = PropertiesUtil.RABBITMQ_HOST; logger.info("RabbitMQ Server IP :{}", host); factory.setHost(host); int port = PropertiesUtil.RABBITMQ_PORT; logger.info("RabbitMQ Server Port :{}", port); factory.setPort(port); String username = PropertiesUtil.RABBITMQ_USERNAME; factory.setUsername(username); String password = PropertiesUtil.RABBITMQ_PASSWORD; factory.setPassword(password); QUEUE_NAME = PropertiesUtil.RABBITMQ_QUEUE_NAME; logger.info("RabbitMQ Server queue name :{}", QUEUE_NAME); connection = factory.newConnection(); } catch (Exception e) { logger.error("队列出错",e); } } public static Connection getConnection() { if (connection == null) { initConnection(); } return connection; } public static String getQueueName() { return QUEUE_NAME; } }