【RabbitMQ——简单队列】CopySend message1 : helloSend message2 : helloSend message3 : hello [*] Waiting for messages. To exit press CTRL+C [x] Received 'hello' [x] Received 'hello' [x] Received 'hello'当我们启动生产者之后查看RabbitMQ管理后台可以看到有一条消息正在等待被消费 。

文章插图
当我们启动消费者之后再次查看,可以看到积压的一条消息已经被消费 。

文章插图
总结#
- 队列声明queueDeclare的参数:第一个参数表示队列名称、第二个参数为是否持久化(true表示是,队列将在服务器重启时生存)、第三个参数为是否是独占队列(创建者可以使用的私有队列,断开后自动删除)、第四个参数为当所有消费者客户端连接断开时是否自动删除队列、第五个参数为队列的其他参数 。
- basicConsume的第二个参数autoAck: 应答模式,true:自动应答,即消费者获取到消息,该消息就会从队列中删除掉,false:手动应答,当从队列中取出消息后,需要程序员手动调用方法应答,如果没有应答,该消息还会再放进队列中,就会出现该消息一直没有被消费掉的现象 。
- 这种简单队列的模式,系统会为每个队列隐式地绑定一个默认交换机,交换机名称为" (AMQP default)",类型为直连 direct,当你手动创建一个队列时,系统会自动将这个队列绑定到一个名称为空的 Direct 类型的交换机上,绑定的路由键 routing key 与队列名称相同,相当于channel.queueBind(queue:"QUEUE_NAME", exchange:"(AMQP default)“, routingKey:"QUEUE_NAME");虽然实例没有显式声明交换机,但是当路由键和队列名称一样时,就会将消息发送到这个默认的交换机中 。这种方式比较简单,但是无法满足复杂的业务需求,所以通常在生产环境中很少使用这种方式 。
- The default exchange is implicitly bound to every queue, with a routing key equal to the queue name. It is not possible to explicitly bind to, or unbind from the default exchange. It also cannot be deleted.默认交换机隐式绑定到每个队列,其中路由键等于队列名称 。不可能显式绑定到,或从缺省交换中解除绑定 。它也不能被删除 。? ——引自 RabbitMQ 官方文档?
Copy<dependency><groupId>com.rabbitmq</groupId><artifactId>amqp-client</artifactId><version>5.6.0</version></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>2.1.5.RELEASE</version></dependency>spring 配置文件Copy<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="http://www.springframework.org/schema/rabbithttps://www.springframework.org/schema/rabbit/spring-rabbit.xsdhttp://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><rabbit:connection-factory id="connectionFactory" host="localhost" virtual-host="/"username="guest" password="guest"/><rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/><rabbit:admin connection-factory="connectionFactory"/><rabbit:queue name="MY-QUEUE"/></beans>使用测试Copyimport org.springframework.amqp.core.AmqpTemplate;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("spring/rabbit-context.xml");AmqpTemplate amqpTemplate = app.getBean(AmqpTemplate.class);amqpTemplate.convertAndSend("MY-QUEUE", "Item");String msg = (String) amqpTemplate.receiveAndConvert("MY-QUEUE");System.out.println(msg);}}参考方法Copy/** * Convert a Java object to an Amqp {@link Message} and send it to a specific exchange * with a specific routing key. * * @param exchange the name of the exchange * @param routingKey the routing key * @param message a message to send * @throws AmqpException if there is a problem */void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;/*** Receive a message if there is one from a specific queue and convert it to a Java* object. Returns immediately, possibly with a null value.** @param queueName the name of the queue to poll* @return a message or null if there is none waiting* @throws AmqpException if there is a problem*/@NullableObject receiveAndConvert(String queueName) throws AmqpException;
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 音响和音箱有什么区别呢——声拓电子音爵士
- 连衣裙|简单两步解决发型“脏乱差”畅醒发蜡发胶简单上手分享
- 加解密修行——No.1
- MySQL 全文索引实现一个简单版搜索引擎
- 异地领取住房公积金只需简单一步操作就可以到账
- Linux技巧:find 命令简单入门介绍和问题点解析
- 给花换盆的详细教程,操作简单,完全不伤大白根
- Linux技巧:awk 命令简单入门介绍
- 花草茶简单祛痘有妙招,有什么功效作用
- 中国邮政信用卡申请需要什么资料
