...
|
...
|
@@ -6,6 +6,7 @@ import com.rabbitmq.client.ConnectionFactory; |
|
|
import com.sunyo.wlpt.message.bus.service.domain.*;
|
|
|
import com.sunyo.wlpt.message.bus.service.service.BusServerService;
|
|
|
import com.sunyo.wlpt.message.bus.service.service.VirtualHostService;
|
|
|
import com.sunyo.wlpt.message.bus.service.utils.EncryptionUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
...
|
...
|
@@ -87,64 +88,28 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 获取 rabbitMq 的连接,重载
|
|
|
*/
|
|
|
public Connection getConnection(String virtualHostName) throws IOException, TimeoutException
|
|
|
{
|
|
|
ConnectionFactory factory = new ConnectionFactory();
|
|
|
factory.setHost(host);
|
|
|
factory.setPort(port);
|
|
|
factory.setVirtualHost(virtualHostName);
|
|
|
factory.setUsername(username);
|
|
|
factory.setPassword(password);
|
|
|
Connection connection = factory.newConnection();
|
|
|
return connection;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取 rabbitMq 的连接,重载
|
|
|
*/
|
|
|
public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName)
|
|
|
public Connection getConnection(String serverIp, Integer serverPort, String virtualHostName, String superUsername, String superPassword)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
String base = EncryptionUtils.decryptBase64(superPassword);
|
|
|
String[] split = base.split("\\.");
|
|
|
ConnectionFactory factory = new ConnectionFactory();
|
|
|
factory.setHost(serverIp);
|
|
|
factory.setPort(serverPort);
|
|
|
factory.setVirtualHost(virtualHostName);
|
|
|
factory.setUsername(username);
|
|
|
factory.setPassword(password);
|
|
|
factory.setUsername(superUsername);
|
|
|
factory.setPassword(split[split.length - 1]);
|
|
|
Connection connection = factory.newConnection();
|
|
|
return connection;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取 rabbitMq 的连接,重载
|
|
|
*
|
|
|
* @param hostIp 服务器ip
|
|
|
* @param hostPort 服务器端口号
|
|
|
* @param vHostName 虚拟主机名称
|
|
|
* @param userName 用户名
|
|
|
* @param password 密码
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password)
|
|
|
throws Exception
|
|
|
{
|
|
|
ConnectionFactory factory = new ConnectionFactory();
|
|
|
factory.setHost(hostIp);
|
|
|
factory.setPort(hostPort);
|
|
|
factory.setVirtualHost(vHostName);
|
|
|
factory.setUsername(userName);
|
|
|
factory.setPassword(password);
|
|
|
return factory.newConnection();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加交换机
|
|
|
*/
|
|
|
public void createExchange(String serverIp, Integer serverPort, String virtualHostName, BusExchange busExchange)
|
|
|
public void createExchange(BusServer server, String virtualHostName, BusExchange busExchange)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.exchangeDeclare(busExchange.getExchangeName(),
|
|
|
busExchange.getExchangeType(),
|
...
|
...
|
@@ -158,10 +123,10 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 删除交换机 channel.exchangeDelete(exchangeName);
|
|
|
*/
|
|
|
public void removeExchange(String serverIp, Integer serverPort, String virtualHostName, BusExchange busExchange)
|
|
|
public void removeExchange(BusServer server, String virtualHostName, BusExchange busExchange)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.exchangeDelete(busExchange.getExchangeName());
|
|
|
closeConnectionAndChanel(channel, connection);
|
...
|
...
|
@@ -170,10 +135,10 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 添加队列(默认设置参数为 null)
|
|
|
*/
|
|
|
public void createQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue)
|
|
|
public void createQueue(BusServer server, String virtualHostName, BusQueue busQueue)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueDeclare(busQueue.getQueueName(),
|
|
|
busQueue.getDurability(),
|
...
|
...
|
@@ -190,10 +155,10 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 删除队列 channel.queueDelete(queueName);
|
|
|
*/
|
|
|
public void removeQueue(String serverIp, Integer serverPort, String virtualHostName, BusQueue busQueue)
|
|
|
public void removeQueue(BusServer server, String virtualHostName, BusQueue busQueue)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueDelete(busQueue.getQueueName());
|
|
|
closeConnectionAndChanel(channel, connection);
|
...
|
...
|
@@ -202,10 +167,10 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 创建绑定
|
|
|
*/
|
|
|
public void createBinding(String serverIp, Integer serverPort, String virtualHostName, UserMessageBinding userMessageBinding)
|
|
|
public void createBinding(BusServer server, String virtualHostName, UserMessageBinding userMessageBinding)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueBind(userMessageBinding.getQueueName(),
|
|
|
userMessageBinding.getExchangeName(),
|
...
|
...
|
@@ -216,11 +181,10 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 解除绑定 channel.queueUnbind("queueName", "exchangeName","routingKey");
|
|
|
*/
|
|
|
public void removeBinding(String serverIp, Integer serverPort, String virtualHostName,
|
|
|
UserMessageBinding userMessageBinding)
|
|
|
public void removeBinding(BusServer server, String virtualHostName, UserMessageBinding userMessageBinding)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
Connection connection = getConnection(serverIp, serverPort, virtualHostName);
|
|
|
Connection connection = getConnection(server.getServerIp(), server.getServerPort(), virtualHostName, server.getSuperUsername(), server.getSuperPassword());
|
|
|
Channel channel = connection.createChannel();
|
|
|
channel.queueUnbind(userMessageBinding.getQueueName(),
|
|
|
userMessageBinding.getExchangeName(),
|
...
|
...
|
@@ -231,41 +195,45 @@ public class RabbitUtils { |
|
|
/**
|
|
|
* 前往创建交换机的路上
|
|
|
*/
|
|
|
public void toCreateExchange(BusExchange busExchange) throws IOException, TimeoutException
|
|
|
public void toCreateExchange(BusExchange busExchange)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
createExchange(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), busExchange);
|
|
|
createExchange(busServer, virtualHost.getVirtualHostName(), busExchange);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前往删除交换机的路上
|
|
|
*/
|
|
|
public void toRemoveExchange(BusExchange busExchange) throws IOException, TimeoutException
|
|
|
public void toRemoveExchange(BusExchange busExchange)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(busExchange.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
removeExchange(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), busExchange);
|
|
|
removeExchange(busServer, virtualHost.getVirtualHostName(), busExchange);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前往创建队列的路上
|
|
|
*/
|
|
|
public void toCreateQueue(BusQueue BusQueue) throws IOException, TimeoutException
|
|
|
public void toCreateQueue(BusQueue BusQueue)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
createQueue(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), BusQueue);
|
|
|
createQueue(busServer, virtualHost.getVirtualHostName(), BusQueue);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前往删除队列的路上
|
|
|
*/
|
|
|
public void toRemoveQueue(BusQueue BusQueue) throws IOException, TimeoutException
|
|
|
public void toRemoveQueue(BusQueue BusQueue)
|
|
|
throws IOException, TimeoutException
|
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(BusQueue.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
removeQueue(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), BusQueue);
|
|
|
removeQueue(busServer, virtualHost.getVirtualHostName(), BusQueue);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -276,7 +244,7 @@ public class RabbitUtils { |
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
createBinding(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), userMessageBinding);
|
|
|
createBinding(busServer, virtualHost.getVirtualHostName(), userMessageBinding);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -287,7 +255,7 @@ public class RabbitUtils { |
|
|
{
|
|
|
VirtualHost virtualHost = getVirtualHost(userMessageBinding.getVirtualHostId());
|
|
|
BusServer busServer = getBusServer(virtualHost.getServerId());
|
|
|
removeBinding(busServer.getServerIp(), busServer.getServerPort(), virtualHost.getVirtualHostName(), userMessageBinding);
|
|
|
removeBinding(busServer, virtualHost.getVirtualHostName(), userMessageBinding);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
|