依赖包:
1 1 <dependencies> 2 2 <dependency> 3 3 <groupId>junit</groupId> 4 4 <artifactId>junit</artifactId> 5 5 <version>4.12</version> 6 6 <scope>test</scope> 7 7 </dependency> 8 8 9 9 <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --> 1010 <dependency> 1111 <groupId>com.rabbitmq</groupId> 1212 <artifactId>amqp-client</artifactId> 1313 <version>5.1.2</version> 1414 </dependency> 1515 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop --> 1616 <dependency> 1717 <groupId>org.slf4j</groupId> 1818 <artifactId>slf4j-nop</artifactId> 1919 <version>1.7.25</version> 2020 </dependency> 2121 2222 </dependencies>
源码:
1 1 package org.study.utils; 2 2 3 3 import com.rabbitmq.client.Connection; 4 4 import com.rabbitmq.client.ConnectionFactory; 5 5 6 6 import java.io.IOException; 7 7 import java.util.concurrent.TimeoutException; 8 8 9 9 /** 1010 * 获取rabbitmq连接 1111 */ 1212 public class ConnectionUtils { 1313 public static Connection getConnection() throws IOException, TimeoutException { 1414 ConnectionFactory factory = new ConnectionFactory(); 1515 factory.setHost("10.15.1.26"); 1616 factory.setPort(5672); 1717 factory.setVirtualHost("/test_host"); 1818 factory.setUsername("admin"); 1919 factory.setPassword("admin"); 2020 Connection connection = factory.newConnection(); 2121 return connection; 2222 } 2323 }