1.引入相应的坐标
<!--rabbitmq依赖 --> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.4.1</version> </dependency>
2.本次测试,使用默认的交换机,也就是一对一生产消费模式。
2.1 生产者
Producer(生产者)端步骤:
(1):创建ConnectionFactory,并且设置一些参数,比如hostname,portNumber等等
(2):利用ConnectionFactory创建一个Connection连接
(3):利用Connection创建一个Channel通道
(4):创建queue并且和Channel进行绑定
(5):创建消息,并且发送到队列中
生产者代码:
package com.ghg.vue.controller; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; /** * 使用rabbit 生产者 */ public class RqbbitProducer { //队列名称 private static final String QUEUE_NAME = "Queue"; public static void main(String[] args) { //创建连接工厂 ConnectionFactory connectionFactory = null; //建立到代理服务器到连接 Connection connection = null; //获得通道 Channel channel = null; try { connectionFactory = new ConnectionFactory(); //设置用户名和密码 connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); //设置RabbitMq地址 connectionFactory.setHost("localhost"); //建立到代理服务器的连接 connection = connectionFactory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME,false,false,false,null); String message = "hello world"; channel.basicPublish("",QUEUE_NAME,null,message.getBytes("UTF-8")); System.out.println("发送消息"+message+"到队列"+QUEUE_NAME+"成功"); } catch (Exception e) { e.printStackTrace(); }finally { try { channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2.2 消费者:
Consumer(消费者)端步骤:
(1):创建ConnectionFactory,并且设置一些参数,比如hostname,portNumber等等
(2):利用ConnectionFactory创建一个Connection连接
(3):利用Connection创建一个Channel通道
(4):将queue和Channel进行绑定,注意这里的queue名字要和前面producer创建的queue一致
(5):创建消费者Consumer来接收消息,同时将消费者和queue进行绑定
消费者代码:
package com.ghg.vue.controller; import com.rabbitmq.client.*; import java.io.IOException; /** * @date 2020/4/29 */ public class RabbitConsumer { //队列名称 private static final String QUEUE_NAME = "Queue"; public static void main(String[] args) { //创建连接工厂 ConnectionFactory connectionFactory = null; //建立到代理服务器到连接 Connection connection = null; //获得通道 Channel channel = null; try { connectionFactory = new ConnectionFactory(); //设置用户名和密码 connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); //设置RabbitMq地址 connectionFactory.setHost("localhost"); //建立到代理服务器的连接 connection = connectionFactory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); //声明一个消费者 DefaultConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); //循环获取消息 while (true) { QueueingConsumer.Delivery delivery = ((QueueingConsumer) consumer).nextDelivery(); String message = new String(delivery.getBody()); System.out.println("接收message:"+message+" from "+QUEUE_NAME); } } catch (Exception e) { e.printStackTrace(); }finally { try { channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3.效果展示
先启动消费者,生产者发送消息如下:
消费者消费如下: