主要介绍exchangeDeclare方法的一些参数及重载,可以参考下。
转载地址:https://www.jianshu.com/p/73c142b120f7
创建一个交换器 :
ConnectionFactory connectionFactory = new ConnectionFactory();
/**
* 设置ip
* port
* 用户名
* 密码
*/
connectionFactory.setHost(IP_ADDRESS);
connectionFactory.setPort(PORT);
connectionFactory.setUsername(USER_NAME);
connectionFactory.setPassword(PASSWORD);
/**
* 创建连接
*/
Connection connection = connectionFactory.newConnection();
/**
* 创建信道
*/
Channel channel = connection.createChannel();
/**
* 创建一个type=direct 持久化的 非自动删除的交换器
*/
channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);
exchangeDeclare方法
exchangeDeclare有很多的方法重载:
Exchange.DeclareOk exchangeDeclare(String exchange, String type) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange, String type, boolean durable, boolean autoDelete,Map arguments) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete,Map arguments) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange,String type,boolean durable,boolean autoDelete, boolean internal,Map arguments) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange,BuiltinExchangeType type,boolean durable,boolean autoDelete,boolean internal,Map arguments) throws IOException;
exchange: 交换器名称
type : 交换器类型 DIRECT("direct"), FANOUT("fanout"), TOPIC("topic"), HEADERS("headers");
durable: 是否持久化,durable设置为true表示持久化,反之是非持久化,持久化的可以将交换器存盘,在服务器重启的时候不会丢失信息.
autoDelete是否自动删除,设置为TRUE则表是自动删除,自删除的前提是至少有一个队列或者交换器与这交换器绑定,之后所有与这个交换器绑定的队列或者交换器都与此解绑,一般都设置为fase
internal 是否内置,如果设置 为true,则表示是内置的交换器,客户端程序无法直接发送消息到这个交换器中,只能通过交换器路由到交换器的方式
arguments:其它一些结构化参数比如:alternate-exchange
BuiltinExchangeType:
package com.rabbitmq.client;
/**
* Enum for built-in exchange types.
*/
public enum BuiltinExchangeType {
DIRECT("direct"), FANOUT("fanout"), TOPIC("topic"), HEADERS("headers");
private final String type;
BuiltinExchangeType(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
师出同门 exchangeDeclareNoWait:
void exchangeDeclareNoWait(String exchange,String type,boolean durable,boolean autoDelete,boolean internal,Map arguments) throws IOException;
void exchangeDeclareNoWait(String exchange,BuiltinExchangeType type, boolean durable,boolean autoDelete,boolean internal,Map arguments) throws IOException;
实现 :
@Override
public void exchangeDeclareNoWait(String exchange,String type,boolean durable,boolean autoDelete,boolean internal,Map arguments) throws IOException {
transmit(new AMQCommand(new Exchange.Declare.Builder().exchange(exchange).type(type).durable(durable).autoDelete(autoDelete).internal(internal).arguments(arguments).passive(false) .nowait(true).build()));
}
比exchangeDeclare多设置了个nowait参数,
不需要服务器返回,这个方法返回是void,而普通的exchangeDeclare方法返返回值是Exchange.DeclareOk
意思是在客户商端声明一个交换器之后,需要等待服务器返回(服务器会返回Exchange.DeclareOk)
exchangeDeclareNoWait在声明一个交换器后,实际上服务器并没有完成交换器的创建,此时客户端接着使用这个交换器,就会发生异常,不建议使用个这方法
exchangeDeclarePassive:
Exchange.DeclareOk exchangeDeclarePassive(String name) throws IOException;
主要 用来检测相应的交换器是否存在,如果存在就正常返回,不存就是抛出异常404 channel exception,同是channel也会被关闭
exchangeDelete:
Exchange.DeleteOk exchangeDelete(String exchange, boolean ifUnused) throws IOException;
Exchange.DeleteOk exchangeDelete(String exchange) throws IOException;
void exchangeDeleteNoWait(String exchange, boolean ifUnused) throws IOException;
exchange交换器的名称
ifUnused同来设置是否在交换器没有被使用的情况下删除,true,则只有在此交换器没有被使用的情况下才删除,false,则无论如何这个交换器都要被删除