Example usage for org.apache.http.impl.client DefaultHttpClient getParams

List of usage examples for org.apache.http.impl.client DefaultHttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient getParams.

Prototype

public synchronized final HttpParams getParams() 

Source Link

Usage

From source file:voldemort.store.readonly.swapper.StoreSwapper.java

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.accepts("help", "print usage information");
    parser.accepts("cluster", "[REQUIRED] the voldemort cluster.xml file ").withRequiredArg()
            .describedAs("cluster.xml");
    parser.accepts("name", "[REQUIRED] the name of the store to swap").withRequiredArg()
            .describedAs("store-name");
    parser.accepts("servlet-path", "the path for the read-only management servlet").withRequiredArg()
            .describedAs("path");
    parser.accepts("file", "[REQUIRED] uri of a directory containing the new store files").withRequiredArg()
            .describedAs("uri");
    parser.accepts("timeout", "http timeout for the fetch in ms").withRequiredArg().describedAs("timeout ms")
            .ofType(Integer.class);
    parser.accepts("rollback", "Rollback store to older version");
    parser.accepts("admin", "Use admin services. Default = false");
    parser.accepts("push-version", "[REQUIRED] Version of push to fetch / rollback-to").withRequiredArg()
            .ofType(Long.class);

    OptionSet options = parser.parse(args);
    if (options.has("help")) {
        parser.printHelpOn(System.out);
        System.exit(0);//  w  ww.  j  av  a2  s.c  o m
    }

    Set<String> missing = CmdUtils.missing(options, "cluster", "name", "file", "push-version");
    if (missing.size() > 0) {
        if (!(missing.equals(ImmutableSet.of("file")) && (options.has("rollback")))) {
            System.err.println("Missing required arguments: " + Joiner.on(", ").join(missing));
            parser.printHelpOn(System.err);
            System.exit(1);
        }
    }

    String clusterXml = (String) options.valueOf("cluster");
    String storeName = (String) options.valueOf("name");
    String mgmtPath = CmdUtils.valueOf(options, "servlet-path", "read-only/mgmt");
    String filePath = (String) options.valueOf("file");
    int timeoutMs = CmdUtils.valueOf(options, "timeout",
            (int) (3 * Time.SECONDS_PER_HOUR * Time.MS_PER_SECOND));
    boolean useAdminServices = options.has("admin");
    boolean rollbackStore = options.has("rollback");
    Long pushVersion = (Long) options.valueOf("push-version");

    String clusterStr = FileUtils.readFileToString(new File(clusterXml));
    Cluster cluster = new ClusterMapper().readCluster(new StringReader(clusterStr));
    ExecutorService executor = Executors.newFixedThreadPool(cluster.getNumberOfNodes());
    StoreSwapper swapper = null;
    AdminClient adminClient = null;

    DefaultHttpClient httpClient = null;
    if (useAdminServices) {
        adminClient = new AdminClient(cluster, new AdminClientConfig(), new ClientConfig());
        swapper = new AdminStoreSwapper(cluster, executor, adminClient, timeoutMs);
    } else {
        int numConnections = cluster.getNumberOfNodes() + 3;
        ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();
        httpClient = new DefaultHttpClient(connectionManager);

        HttpParams clientParams = httpClient.getParams();

        connectionManager.setMaxTotal(numConnections);
        connectionManager.setDefaultMaxPerRoute(numConnections);
        HttpConnectionParams.setSoTimeout(clientParams, timeoutMs);

        swapper = new HttpStoreSwapper(cluster, executor, httpClient, mgmtPath);
    }

    try {
        long start = System.currentTimeMillis();
        if (rollbackStore) {
            swapper.invokeRollback(storeName, pushVersion.longValue());
        } else {
            swapper.swapStoreData(storeName, filePath, pushVersion.longValue());
        }
        long end = System.currentTimeMillis();
        logger.info("Succeeded on all nodes in " + ((end - start) / Time.MS_PER_SECOND) + " seconds.");
    } finally {
        if (useAdminServices && adminClient != null)
            adminClient.close();
        executor.shutdownNow();
        executor.awaitTermination(1, TimeUnit.SECONDS);
        VoldemortIOUtils.closeQuietly(httpClient);
    }
    System.exit(0);
}