Search code examples
javarabbitmqrabbitmq-exchange

Get list of bindings for RabbitMQ


We have a RabbitMQ Broker (V3.5.7) which is routing messages to a group of servers. When a server instance comes up (perhaps after a restart), I would like get a list of the current bindings for that server, so that I can confirm the bindings. The server is running Java, so that would be the preferred API.


Solution

  • It is an http call, something like that:

     HttpURLConnection connection = null;
    
            try {
                //Create connection
                URL url = new URL("http://localhost:15672/api/exchanges/%2F/topic_test/bindings/source");
                 connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
    
                connection.setRequestProperty("Content-Language", "en-US");
                String userpass =   "guest:guest";
                String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
                connection.setRequestProperty ("Authorization", basicAuth);
    
                connection.setUseCaches(false);
                connection.setDoOutput(true);
    
    
    
                //Get Response
                InputStream is = connection.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
                String line;
                while ((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\r');
                }
                rd.close();
    
                System.out.println(response.toString());
    
            } catch (Exception e) {
                e.printStackTrace();
    
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
    
    

    maybe there are more efficient ways, but this is the idea