rabbitmq

1.获取队列的详细信息

 @Override
    public List queueDatail() throws IOException {
        int num= 0;
        ArrayList rabbitMQList = new ArrayList<>();
        // 查找所有队列
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 发送HTTP GET请求来获取RabbitMQ队列信息
        HttpGet request = new HttpGet("http://"+rabbitMqHost+":15672/api/queues");
        request.addHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((rabbitMqUsername+":"+rabbitMqPassword).getBytes()));

        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        // 解析API响应并获取队列名称
        if (entity != null) {
            String jsonString = EntityUtils.toString(entity);
            JSONArray queues = new JSONArray(jsonString);

            for (int i = 0; i < queues.length(); i++) {
                RabbitMQ rabbitMQ = new RabbitMQ();
                rabbitMQ.setName(queues.getJSONObject(i).getString("name"));
                rabbitMQ.setType(queues.getJSONObject(i).getString("type"));
                rabbitMQ.setState(queues.getJSONObject(i).has("state")?queues.getJSONObject(i).getString("state"):"running");
                rabbitMQ.setReady(queues.getJSONObject(i).has("messages_ready")?String.valueOf(queues.getJSONObject(i).getInt("messages_ready")):"0");
                rabbitMQ.setUnAcked(queues.getJSONObject(i).has("messages_unacknowledged")?String.valueOf(queues.getJSONObject(i).getInt("messages_unacknowledged")):"0");
                rabbitMQ.setTotal(queues.getJSONObject(i).has("messages")?String.valueOf(queues.getJSONObject(i).getInt("messages")):"0");
                rabbitMQList.add(rabbitMQ);
            }
        }
        httpClient.close();
        return rabbitMQList;
    }

你可能感兴趣的:(工作记录,rabbitmq,ruby,分布式)