Managing RabbitMQ instances

RabbitMQ provides a number of utilities for managing RabbitMQ instances since the AMQP protocol provides limited support for that purpose (and it is not a responsibility of the protocol in general to do so). So far we have seen how we can administer RabbitMQ from the command line using the rabbitmqctl or the rabbitmqadmin utilities. However there are many scenarios where more sophisticated tools for provisioning and managing the RabbitMQ broker components are needed (for example, in the form of an alternative web interface).

In that case, the management plugin provides an interface of REST (Representational State Transfer)-based web services. In order to see all the available services in your current installation of the management plugin you can navigate from the browser to http://localhost:15672/api/—there is a short description with basic examples and a reference guide for the various services. For testing purposes, you can use any utility (such as cURL) that allows you to send HTTP requests to the manage REST API. As everything in REST is a resource that is managed with CRUD operations provided by the HTTP methods (such as GET, POST, PUT, DELETE), so are RabbitMQ resources. If you take a closer look you will notice that all of the resources are precisely the various types of RabbitMQ components (such as vhosts, users, permissions, queues, exchanges, and bindings); no rocket science here. The REST interface respects the current user permissions (configure, write, read for particular components) when checking for permissions for performing a certain action.

Let's assume that we want to implement a simple utility called ComponentFinder that allows us to list particular RabbitMQ components in a given vhost based on a regular expression. For that purpose we will create a new Maven project that uses the REST client from the Apache Jersey library provided as a Maven dependency, along with the standard JSON utility in Java:

<dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-client</artifactId><version>1.19</version></dependency>
<dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20140107</version>
</dependency>

Here is the class for the ComponentFinder utility:

import java.util.Scanner;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class ComponentFinder {

    private final static Logger LOGGER = LoggerFactory
        .getLogger(ComponentFinder.class);
private static final String API_ROOT =                         "http://localhost:15672/api";

The main() method provides the logic for the tool, reading from the standard input and processing the request based on the input parameters. A simple HTTP client is used for the purpose:

    public static void main(String[] args) {

        Scanner scanner = null;
        try {
            scanner = new Scanner(System.in);
            System.out.println("Enter component type in                     plural form (for example, queues, exchanges) ");
            String type = scanner.nextLine();
            System.out.println("Enter vhost (leave empty                     for default vhost) ");
            String vhost = scanner.nextLine();
            System.out.println("Enter name pattern (leave                     empty for match-all pattern)");
            String pattern = scanner.nextLine();

            Client client = Client.create();
            String path;
            if (vhost.trim().isEmpty()) {
                path = API_ROOT + "/" + type +                             "?columns=name";
            } else {
                path = API_ROOT + "/" + type +                             "/" + vhost + "?columns=name";
            }

            WebResource resource = client.resource(path);
            resource.header("Content-Type",                                 "application/json;charset=UTF-8");
            resource.addFilter(new HTTPBasicAuthFilter("guest", "guest".getBytes()));
            String result = resource.get(String.class);
            JSONArray jsonResult = new JSONArray(result);
            LOGGER.debug("Result: 
" + jsonResult.toString(4));
filterResult(jsonResult, pattern);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }

The filterResult() helper method is used to filter the response from the management API based on a regular expression:

private static void filterResult(JSONArray jsonResult,                 String pattern) {
        // filter the result based on the pattern
        for (int index = 0; index < jsonResult.length();                 index++) {
            JSONObject componentInfo =                                 (JSONObject) jsonResult.get(index);
            String componentName =                                 (String) componentInfo.get("name");
            if (Pattern.matches(pattern, componentName)) {
                LOGGER.info("Matched component: " +                         componentName);
                // do something else with component
            }
        }
    }}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.186.79