依赖包:
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>4.12</version> 6 <scope>test</scope> 7 </dependency> 8 9 <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --> 10 <dependency> 11 <groupId>com.rabbitmq</groupId> 12 <artifactId>amqp-client</artifactId> 13 <version>5.1.2</version> 14 </dependency> 15 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop --> 16 <dependency> 17 <groupId>org.slf4j</groupId> 18 <artifactId>slf4j-nop</artifactId> 19 <version>1.7.25</version> 20 </dependency> 21 22 </dependencies>
源码:
1 package org.study.utils; 2 3 import com.rabbitmq.client.Connection; 4 import com.rabbitmq.client.ConnectionFactory; 5 6 import java.io.IOException; 7 import java.util.concurrent.TimeoutException; 8 9 /** 10 * 获取rabbitmq连接 11 */ 12 public class ConnectionUtils { 13 public static Connection getConnection() throws IOException, TimeoutException { 14 ConnectionFactory factory = new ConnectionFactory(); 15 factory.setHost("10.15.1.26"); 16 factory.setPort(5672); 17 factory.setVirtualHost("/test_host"); 18 factory.setUsername("admin"); 19 factory.setPassword("admin"); 20 Connection connection = factory.newConnection(); 21 return connection; 22 } 23 }