pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
获取连接
package utils; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import org.apache.log4j.Logger; public class ConnectionUtil { private static Logger logger = Logger.getLogger(ConnectionUtil.class); public static Connection getConnection() { try { Connection connection = null; //定义一个连接工厂 ConnectionFactory factory = new ConnectionFactory(); //设置服务端地址(域名地址/ip) factory.setHost("192.168.131.128"); //设置服务器端口号 factory.setPort(5672); //设置虚拟主机(相当于数据库中的库) factory.setVirtualHost("/"); //设置用户名 factory.setUsername("guest"); //设置密码 factory.setPassword("guest"); connection = factory.newConnection(); return connection; } catch (Exception e) { return null; } } }
生产者
package message; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import utils.ConnectionUtil; import java.io.IOException; import java.util.concurrent.TimeoutException; public class Send { //队列名称 private static final String QUEUE_NAME = "test_simple_queue"; public static void main(String[] args) { try { //获取连接 Connection connection = ConnectionUtil.getConnection(); //从连接中获取一个通道 Channel channel = connection.createChannel(); //声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "This is simple queue"; //发送消息 channel.basicPublish("", QUEUE_NAME, null, message.getBytes("utf-8")); System.out.println("[send]:" + message); channel.close(); connection.close(); } catch (IOException | TimeoutException e) { e.printStackTrace(); } } }
消费者
package message; import com.rabbitmq.client.*; import utils.ConnectionUtil; import java.io.IOException; public class Receive { //队列名称 private static final String QUEUE_NAME = "test_simple_queue"; public static void main(String[] args) { try { //获取连接 Connection connection = ConnectionUtil.getConnection(); //从连接中获取一个通道 Channel channel = connection.createChannel(); //声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); //定义消费者 DefaultConsumer consumer = new DefaultConsumer(channel) { //当消息到达时执行回调方法 @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "utf-8"); System.out.println("[Receive]:" + message); } }; //监听队列 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (IOException | ShutdownSignalException | ConsumerCancelledException e) { e.printStackTrace(); } } }