RabbitMQ-发送消息basicPublish

it2026-01-11  9

basicPublish

channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN , message.getBytes());

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 交换器名称

routingKey 路由键

props 有14个成员

public static class BasicProperties extends com.rabbitmq.client.impl.AMQBasicProperties { private String contentType; //消息类型如(text/plain) private String contentEncoding; //编码 private Map<String,Object> headers; //header private Integer deliveryMode; //消息的投递模式 private Integer priority; //优先级 private String correlationId; private String replyTo; private String expiration; //过期时间 private String messageId; private Date timestamp; private String type; private String userId; private String appId; private String clusterId;

body 消息体,payload真正需要发送的消息

mandatory true时,交换器无法根据自动的类型和路由键找到一个符合条件的队列,那么RabbitMq会调用Basic.Ruturn命令将消息返回给生产都,为false时,出现上述情况消息被直接丢弃

immediate true,如果交换器在消息路由到队列时发现没有任何消费者,那么 这个消息将不会存和队列,当与路由匹配的所有队列都没有消费者时,会Basic.Return返回给生产者3.0去掉了immediate 参数 immediate和mandatory 都是消息传递过程中,不可达目的地是,将消息返回给生产者的功能

 

最新回复(0)