消息发布端发送消息的方法
channel.basicPublish(exchangeName,routingKey,true,null, msg.getBytes());
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:交换器无法根据自身类型和路由键找到一个符合条件的队列时的处理方式
true:RabbitMQ会调用Basic.Return命令将消息返回给生产者
false:RabbitMQ会把消息直接丢弃
immediate:设置true时,如果该消息关联的队列上有消费者,则立即投递,否则这条消息不存入队列;如果与路由键匹配的所有队列都没有消费者时,该消息会通过Basic.Return返回至生产者
props:消息属性集,包含14个属性成员,如持久化、优先级、投递模式、过期时间等等
body:消息体,需要发送的消息
消息接收端发送消息的方法
channel.exchangeDeclare(exchangeName,"topic",true,false, null);
/**
* Declare an exchange.
* @see com.rabbitmq.client.AMQP.Exchange.Declare
* @see com.rabbitmq.client.AMQP.Exchange.DeclareOk
* @param exchange the name of the exchange
* @param type the exchange type
* @param durable true if we are declaring a durable exchange (the exchange will survive a server restart)
* @param autoDelete true if the server should delete the exchange when it is no longer in use
* @param arguments other properties (construction arguments) for the exchange
* @return a declaration-confirm method to indicate the exchange was successfully declared
* @throws java.io.IOException if an error is encountered
*/
Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable, boolean autoDelete,
Map<String, Object> arguments) throws IOException;
channel.queueDeclare(queueName,true,false,false,null);
/**
* Declare a queue
* @see com.rabbitmq.client.AMQP.Queue.Declare
* @see com.rabbitmq.client.AMQP.Queue.DeclareOk