|
|
package com.sunyo.wlpt.message.bus.service.rabbit.utils;
|
|
|
|
|
|
import com.rabbitmq.client.AMQP;
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import com.rabbitmq.client.Connection;
|
|
|
import com.rabbitmq.client.ConnectionFactory;
|
...
|
...
|
@@ -147,9 +146,12 @@ public class RabbitUtils { |
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Channel channel = connection.createChannel();
|
|
|
AMQP.Exchange.DeclareOk declareOk = channel.exchangeDeclare(busExchange.getExchangeName(), busExchange.getExchangeType(), busExchange.getDurability(),
|
|
|
busExchange.getAutoDelete(), busExchange.getInternal(), null);
|
|
|
log.info("创建交换机的返回值<----->" + declareOk);
|
|
|
channel.exchangeDeclare(busExchange.getExchangeName(),
|
|
|
busExchange.getExchangeType(),
|
|
|
busExchange.getDurability(),
|
|
|
busExchange.getAutoDelete(),
|
|
|
busExchange.getInternal(),
|
|
|
null);
|
|
|
closeConnectionAndChanel(channel, connection);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -166,18 +168,26 @@ public class RabbitUtils { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加队列(默认设置参数为null)
|
|
|
* 添加队列(默认设置参数为 null)
|
|
|
*/
|
|
|
public void createQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueDeclare(busQueue.getQueueName(), busQueue.getDurability(), false, busQueue.getAutoDelete(), null);
|
|
|
channel.queueDeclare(busQueue.getQueueName(),
|
|
|
busQueue.getDurability(),
|
|
|
false,
|
|
|
busQueue.getAutoDelete(),
|
|
|
null);
|
|
|
closeConnectionAndChanel(channel, connection);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清空队列 channel.queuePurge(queueName);
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* 删除队列 channel.queueDelete(queueName);
|
|
|
*/
|
|
|
public void removeQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue)
|
...
|
...
|
@@ -190,22 +200,33 @@ public class RabbitUtils { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建绑定关系
|
|
|
* 创建绑定
|
|
|
*/
|
|
|
public void createBinding(String serverIp, Integer serverPort, String virtualHostName, UserMessageBinding userMessageBinding)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueBind(userMessageBinding.getQueueName(),
|
|
|
userMessageBinding.getExchangeName(),
|
|
|
userMessageBinding.getRoutingKeyName());
|
|
|
closeConnectionAndChanel(channel, connection);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清空队列 channel.queuePurge(queueName);
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* 解除绑定 channel.queueUnbind("queueName", "exchangeName","routingKey");
|
|
|
*/
|
|
|
public void removeBinding(String serverIp, Integer serverPort, String virtualHostName,
|
|
|
UserMessageBinding userMessageBinding)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueUnbind(userMessageBinding.getQueueName(),
|
|
|
userMessageBinding.getExchangeName(),
|
|
|
userMessageBinding.getRoutingKeyName());
|
|
|
closeConnectionAndChanel(channel, connection);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前往创建交换机的路上
|
...
|
...
|
@@ -248,6 +269,28 @@ public class RabbitUtils { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前往创建绑定的路上
|
|
|
*/
|
|
|
public void toCreateBinding(UserMessageBinding userMessageBinding)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
createBinding(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), userMessageBinding);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前往解除绑定的路上
|
|
|
*/
|
|
|
public void toRemoveBinding(UserMessageBinding userMessageBinding)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
removeBinding(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), userMessageBinding);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据虚拟主机id,获取虚拟主机信息
|
|
|
*
|
|
|
* @param virtualHostId 虚拟主机id
|
...
|
...
|
|