消息中间件入门篇之 RabbitMQ 的 massage详解

Massage 消息

它是服务器和应用程序间传输的数据,我们在前面的样例代码种可以看到发送消息的样例;Channel.basicPublish 方法,它有几个重载方法

  • void basicPublish (String exchange , String routingKey, BasicProperties props ,byte[) body) throws IOException ;

  • void basicPublish(String exchange , String routingKey, boolean mandatory, BasicProperties props ,byte[) body) throws IOException ;

  • void basicPublish(String exchange , String routingKey, boolean mandatory, boolean immediate, BasicProperties props ,byte[] body) throws IOException ;

具体参数详细介绍如下:

  • exchange :交换器的名称,指明消息需要发送到哪个交换器中,如果设置为空,则默认发送到RabbitMQ 默认的交换器中
  • routingKey路由键
  • mandatory :当mandatory 参数设为true 时,交换器无法根据自身的类型和路由键找到一个符合条件的队列,那么RabbitMQ 会调用Basic.Return 命令将消息返回给生产者。当mandatory参数设置为false 时,出现上述情形,则消息直接被丢弃.
  • immediate :当imrnediate参数设为true时,类似UDP, 如果交换器在将消息路由到队列时发现队列上并不存在任何消费者,那么这条消息将不会存入队列中。当与路由键匹配的所有队列都没有消费者时,该消息会通过Basic.Return返回至生产者

props :消息的基本属性集,分别有

  1. contentType (消息类型)
  2. contentEncoding (消息编码)
  3. headers( Map<String ,Object>) (自定义的一些消息数据)
  4. deliveryMode (消息是否持久化 = 2)
  5. priority (消息优先级)
  6. correlationld (可以理解为消息的唯一ID)
  7. replyTo (消息重回队列)
  8. expiration (过期时间)
  9. messageld
  10. timestamp
  11. type
  12. userld
  13. appld
  14. clusterld

body :消息体( payload ) 需要发送的消息

猜你喜欢

转载自blog.csdn.net/qq_33709508/article/details/107550365
今日推荐