如何使用Java获取rabbitmq中声明的交换和队列列表?

时间:2014-05-05 23:08:45

标签: java rabbitmq

我正在寻找一种方法来列出所有声明的交换,使用Java代码在rabbitmq中排队。 我知道那个命令" rabbitmqctl list_queues"我也知道" rabbitmqadmin列表队列"

3 个答案:

答案 0 :(得分:6)

您可以使用HTTP API。

安装网络管理插件:

rabbitmq-plugins enable rabbitmq_management

然后使用API​​获取信息:

http://localhost:15672/api/exchanges
http://localhost:15672/api/queues

完整的API列表可在以下网址获得:

http://localhost:15672/api/

只需执行java http请求并获取json结果。

答案 1 :(得分:1)

我编写了一个函数来将交换或队列列表转换为JComboBox。没有使用JSON解析器,只使用了split函数...祝你好运

private void getValuesFromRabbitWebAPI(String strURL, JComboBox<String> c) throws MalformedURLException, IOException 
{

    String loginPassword = username()+ ":" + password();
    String encoded = DatatypeConverter.printBase64Binary(loginPassword.getBytes());

    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();
    conn.setRequestProperty ("Authorization", "Basic " + encoded);
    String response = "";
    try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
        String line;
        while ((line = in.readLine()) != null){
            response += line;
        }
    }
    String[] split = response.split("\"name\":\"");
    c.addItem((String)"");
    // skipping the first element "[{" or ""
    for(int i=1; i<split.length; i++)
    {
        String nameRaw = split[i];
        int index = nameRaw.indexOf("\"");
        if (index > 0 && !nameRaw.startsWith("amq."))
        {
            String name = nameRaw.substring(0, index);
            c.addItem((String) name);
        }
    }
}

答案 2 :(得分:0)

获取交换的函数的参数:

getValuesFromRabbitWebAPI("http://" + hostname + ":15672/api/exchanges", exchangeComboBox);

或获取队列:

getValuesFromRabbitWebAPI("http://" + hostname + ":15672/api/queues", queueComboBox);
相关问题