一、连接rabbit
public Connection getConnection()
{
try
{
Connection connection = null;
//定义一个连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务端地址(域名地址/ip)
factory.setHost(host);
//设置服务器端口号
factory.setPort(port);
//设置虚拟主机(相当于数据库中的库)
factory.setVirtualHost("/");
//设置用户名
factory.setUsername(username);
//设置密码
factory.setPassword(password);
connection = factory.newConnection();
return connection;
}
catch (Exception e)
{
return null;
}
}
二、根据交换机、队列名及键值进行解绑
public void deleteMQ(String queueName , List routeKeys) throws IOException {
//如果没有连接过
if(channel == null){
//获取连接
Connection connection = this.getConnection();
//从连接中获取一个通道
channel = connection.createChannel();
}
Iterator routeKey = routeKeys.iterator();
while(routeKey.hasNext()) {
String key = (String)routeKey.next();
//解绑方法queueUnbind(队列名、交换机名称、键值)
channel.queueUnbind(queueName, "EXCHANGER", key);
}
}