exchangeDeclare
/**
* 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,
boolean internal,
Map<String, Object> arguments) throws IOException;
- exchange :交换器的名称
- type : 交换器的类型,常见的有direct,fanout,topic等
- durable :设置是否持久化。durable设置为true时表示持久化,反之非持久化.持久化可以将交换器存入磁盘,在服务器重启的时候不会丢失相关信息。
- autoDelete:设置是否自动删除。autoDelete设置为true时,则表示自动删除。自动删除的前提是至少有一个队列或者交换器与这个交换器绑定,之后,所有与这个交换器绑定的队列或者交换器都与此解绑。不能错误的理解—当与此交换器连接的客户端都断开连接时,RabbitMq会自动删除本交换器
- internal:设置是否内置的。如果设置为true,则表示是内置的交换器,客户端程序无法直接发送消息到这个交换器中,只能通过交换器路由到交换器这种方式。
- arguments:其它一些结构化的参数,比如:alternate-exchange
其它一些重载的方法
BuiltinExchangeType
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;
}
}
type可以用上面的这个代替
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, boolean autoDelete,
Map<String, Object> arguments) throws IOException;
Exchange.DeclareOk exchangeDeclare(String exchange,
BuiltinExchangeType type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments) throws IOException;
exchangeDeclareNoWait方法
/**
* Like {@link Channel#exchangeDeclare(String, String, boolean, boolean, java.util.Map)} but
* sets nowait parameter to true and returns nothing (as there will be no response from
* the server).
*
* @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 internal true if the exchange is internal, i.e. can't be directly
* published to by a client.
* @param arguments other properties (construction arguments) for the exchange
* @throws java.io.IOException if an error is encountered
*/
void exchangeDeclareNoWait(String exchange,
String type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments) throws IOException;
exchangeDeclareNoWait比exchangeDeclare方法默认多设置了一个nowait参数
.nowait(true)
transmit(new AMQCommand(new Exchange.Declare.Builder()
.exchange(exchange)
.type(type)
.durable(durable)
.autoDelete(autoDelete)
.internal(internal)
.arguments(arguments)
.passive(false)
.nowait(true)
.build()));
nowait是Exchange.Declare命令的参数
public static class Exchange {
public interface Declare extends Method {
int getTicket();
String getExchange();
String getType();
boolean getPassive();
boolean getDurable();
boolean getAutoDelete();
boolean getInternal();
boolean getNowait();
Map<String,Object> getArguments();
// Builder for instances of Exchange.Declare
public static final class Builder
{
private int ticket = 0;
private String exchange;
private String type = "direct";
private boolean passive = false;
private boolean durable = false;
private boolean autoDelete = false;
private boolean internal = false;
private boolean nowait = false;
private Map<String,Object> arguments = null;
public Builder() { }
...
意思是不需要服务器返回,返回值为void,而普通的exchangeDeclare返回的是Exchange.DeclareOk,客户端声明一交换器后,需要等待服务器的返回
nowait:在声明完一个交换器后,实际上服务器还未完成交换器的创建,那么客户端接着使用这个交换吕,必然发生异常,
exchangeDeclarePassive方法
/**
* Declare an exchange passively; that is, check if the named exchange exists.
* @param name check the existence of an exchange named this
* @throws IOException the server will raise a 404 channel exception if the named exchange does not exist.
*/
Exchange.DeclareOk exchangeDeclarePassive(String name) throws IOException;
主要有来检测相应的交换器是否存在。如果存在则正常返回;如果不存在则抛出异常:404 channel exception,同时channel也会被 关闭
删除exchangeDelete
/**
* Delete an exchange
* @see com.rabbitmq.client.AMQP.Exchange.Delete
* @see com.rabbitmq.client.AMQP.Exchange.DeleteOk
* @param exchange the name of the exchange
* @param ifUnused true to indicate that the exchange is only to be deleted if it is unused
* @return a deletion-confirm method to indicate the exchange was successfully deleted
* @throws java.io.IOException if an error is encountered
*/
Exchange.DeleteOk exchangeDelete(String exchange, boolean ifUnused) throws IOException;
- ifUnused 用来设置是否在交换器没有被使用的情况下删除。
- true:则只有在此交换器没有被使用的情况下才会被删除.
- false:无论如何这个交换器都要被删除