|
|
package com.sunyo.wlpt.message.bus.service.service.view;
|
|
|
|
|
|
import com.rabbitmq.http.client.domain.QueueInfo;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.BusServer;
|
|
|
import com.sunyo.wlpt.message.bus.service.domain.VirtualHost;
|
|
|
import com.sunyo.wlpt.message.bus.service.mapper.BusServerMapper;
|
|
|
import com.sunyo.wlpt.message.bus.service.mapper.VirtualHostMapper;
|
|
|
import com.sunyo.wlpt.message.bus.service.rabbit.utils.ClientUtils;
|
|
|
import com.sunyo.wlpt.message.bus.service.response.ResultJson;
|
|
|
import io.netty.util.internal.StringUtil;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author 子诚
|
|
|
* Description:
|
|
|
* 时间:2020/8/26 15:09
|
|
|
*/
|
|
|
@Service
|
|
|
public class ViewQueueFactory {
|
|
|
|
|
|
@Resource
|
|
|
private BusServerMapper busServerMapper;
|
|
|
|
|
|
@Resource
|
|
|
private VirtualHostMapper virtualHostMapper;
|
|
|
|
|
|
public ResultJson getViewQueueList(String serverName, String virtualHostName, Integer pageNum, Integer pageSize) throws IOException,
|
|
|
URISyntaxException
|
|
|
{
|
|
|
List<QueueInfo> list = new ArrayList<>();
|
|
|
|
|
|
// 服务器名称、虚拟主机名称,均为空
|
|
|
if (StringUtil.isNullOrEmpty(serverName) && StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
List<BusServer> serverList = busServerMapper.getServerList();
|
|
|
for (BusServer busServer : serverList) {
|
|
|
List<QueueInfo> queueInfoList = ClientUtils.getViewQueues(busServer);
|
|
|
list.addAll(queueInfoList);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 仅,服务器名称不为空
|
|
|
if (!StringUtil.isNullOrEmpty(serverName) && StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
BusServer busServer = busServerMapper.selectByServerName(serverName);
|
|
|
if (busServer == null) {
|
|
|
return new ResultJson("400", "该服务器名称不存在,请仔细检查");
|
|
|
}
|
|
|
List<QueueInfo> queueInfoList = ClientUtils.getViewQueues(busServer);
|
|
|
list.addAll(queueInfoList);
|
|
|
}
|
|
|
|
|
|
// 仅,虚拟主机名称不为空
|
|
|
if (StringUtil.isNullOrEmpty(serverName) && !StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
VirtualHost virtualHost = virtualHostMapper.selectByVirtualHostName(virtualHostName);
|
|
|
if (virtualHost == null) {
|
|
|
return new ResultJson("400", "该虚拟主机名称不存在,请仔细检查");
|
|
|
}
|
|
|
BusServer busServer = busServerMapper.selectByPrimaryKey(virtualHost.getServerId());
|
|
|
List<QueueInfo> queueInfoList = ClientUtils.getViewQueues(busServer, virtualHostName);
|
|
|
list.addAll(queueInfoList);
|
|
|
}
|
|
|
|
|
|
// 服务器名称、虚拟主机名称,均不为空
|
|
|
if (!StringUtil.isNullOrEmpty(serverName) && !StringUtil.isNullOrEmpty(virtualHostName)) {
|
|
|
BusServer busServer = busServerMapper.selectByServerName(serverName);
|
|
|
if (busServer == null) {
|
|
|
return new ResultJson("400", "该服务器名称不存在,请仔细检查");
|
|
|
}
|
|
|
VirtualHost virtualHost = virtualHostMapper.selectByVirtualHostName(virtualHostName);
|
|
|
if (virtualHost == null) {
|
|
|
return new ResultJson("400", "该虚拟主机名称不存在,请仔细检查");
|
|
|
}
|
|
|
if (!virtualHost.getServerId().equals(busServer.getId())) {
|
|
|
return new ResultJson("400", "该虚拟主机不属于该服务器,请仔细检查");
|
|
|
}
|
|
|
List<QueueInfo> queueInfoList = ClientUtils.getViewQueues(busServer, virtualHostName);
|
|
|
list.addAll(queueInfoList);
|
|
|
}
|
|
|
Integer total = list.size();
|
|
|
|
|
|
// 达到分页与排序效果
|
|
|
List<QueueInfo> resultList = subList(pageNum, pageSize, list);
|
|
|
return resultList.size() > 0
|
|
|
? new ResultJson<>("200", "查询队列监控,成功!", resultList, total)
|
|
|
: new ResultJson<>("500", "查询队列监控,失败!");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 实现分页与排序效果
|
|
|
*
|
|
|
* @param pageNum 开始页
|
|
|
* @param pageSize 每页大小
|
|
|
* @param list 全部的数据
|
|
|
* @return List<QueueInfo>
|
|
|
*/
|
|
|
public List<QueueInfo> subList(Integer pageNum, Integer pageSize, List<QueueInfo> list)
|
|
|
{
|
|
|
Integer total = list.size();
|
|
|
Integer start = (pageNum - 1) * pageSize;
|
|
|
Integer end = start + pageSize;
|
|
|
if (start > total) {
|
|
|
start = 0;
|
|
|
end = total;
|
|
|
}
|
|
|
if (end > total) {
|
|
|
end = total;
|
|
|
}
|
|
|
List<QueueInfo> pageList = list.subList(start, end);
|
|
|
// 对总信息数,进行降序排序
|
|
|
List<QueueInfo> resultList =
|
|
|
pageList.stream().sorted(Comparator.comparing(QueueInfo::getTotalMessages).reversed()).collect(Collectors.toList());
|
|
|
return resultList;
|
|
|
}
|
|
|
} |
...
|
...
|
|