Search in sources :

Example 11 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project coprhd-controller by CoprHD.

the class IsilonApiFactory method init.

/**
 * Initialize HTTP client
 */
public void init() {
    _clientMap = new ConcurrentHashMap<String, IsilonApi>();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setMaxTotalConnections(_maxConn);
    params.setTcpNoDelay(true);
    params.setConnectionTimeout(_connTimeout);
    params.setSoTimeout(_socketConnTimeout);
    _connectionManager = new MultiThreadedHttpConnectionManager();
    _connectionManager.setParams(params);
    // close idle connections immediately
    _connectionManager.closeIdleConnections(0);
    HttpClient client = new HttpClient(_connectionManager);
    client.getParams().setConnectionManagerTimeout(connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });
    _clientHandler = new ApacheHttpClientHandler(client);
    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 443));
}
Also used : IOException(java.io.IOException) HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) ApacheHttpClientHandler(com.sun.jersey.client.apache.ApacheHttpClientHandler) Protocol(org.apache.commons.httpclient.protocol.Protocol) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 12 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project coprhd-controller by CoprHD.

the class RestClientFactory method init.

/**
 * Initialize HTTP client
 */
public void init() {
    _clientMap = new ConcurrentHashMap<String, RestClientItf>();
    _log.info("Init");
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setDefaultMaxConnectionsPerHost(_maxConnPerHost);
    params.setMaxTotalConnections(_maxConn);
    params.setTcpNoDelay(true);
    params.setConnectionTimeout(_connTimeout);
    params.setSoTimeout(_socketConnTimeout);
    _connectionManager = new MultiThreadedHttpConnectionManager();
    _connectionManager.setParams(params);
    // close idle connections immediately
    _connectionManager.closeIdleConnections(0);
    HttpClient client = new HttpClient(_connectionManager);
    client.getParams().setConnectionManagerTimeout(connManagerTimeout);
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        @Override
        public boolean retryMethod(HttpMethod httpMethod, IOException e, int i) {
            return false;
        }
    });
    if (_needCertificateManager) {
        // TMP CODE to create dummy security certificate manager
        ClientConfig clientConfig = null;
        try {
            final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

                @Override
                public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
                }

                @Override
                public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            } };
            // Install the all-trusting trust manager
            SSLContext sslContext;
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            javax.net.ssl.HostnameVerifier hostVerifier = new javax.net.ssl.HostnameVerifier() {

                @Override
                public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
                    return true;
                }
            };
            clientConfig = new com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig();
            clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(hostVerifier, sslContext));
        } catch (GeneralSecurityException ex) {
            throw new RuntimeException("Failed to obtain ApacheHTTPClient Config");
        }
        _clientHandler = new ApacheHttpClientHandler(client, clientConfig);
    } else {
        _clientHandler = new ApacheHttpClientHandler(client);
    }
    Protocol.registerProtocol("https", new Protocol("https", new NonValidatingSocketFactory(), 443));
}
Also used : HttpMethodRetryHandler(org.apache.commons.httpclient.HttpMethodRetryHandler) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) Protocol(org.apache.commons.httpclient.protocol.Protocol) HTTPSProperties(com.sun.jersey.client.urlconnection.HTTPSProperties) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) X509TrustManager(javax.net.ssl.X509TrustManager) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) ApacheHttpClientHandler(com.sun.jersey.client.apache.ApacheHttpClientHandler) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 13 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project pinot by linkedin.

the class ControllerStarter method start.

public void start() {
    LOGGER.info("Starting Pinot controller");
    Utils.logVersions();
    component.getServers().add(Protocol.HTTP, Integer.parseInt(config.getControllerPort()));
    component.getClients().add(Protocol.FILE);
    component.getClients().add(Protocol.JAR);
    final Context applicationContext = component.getContext().createChildContext();
    LOGGER.info("Controller download url base: {}", config.generateVipUrl());
    LOGGER.info("Injecting configuration and resource manager to the API context");
    applicationContext.getAttributes().put(ControllerConf.class.toString(), config);
    applicationContext.getAttributes().put(PinotHelixResourceManager.class.toString(), helixResourceManager);
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.getParams().setConnectionTimeout(config.getServerAdminRequestTimeoutSeconds());
    applicationContext.getAttributes().put(HttpConnectionManager.class.toString(), connectionManager);
    applicationContext.getAttributes().put(Executor.class.toString(), executorService);
    controllerRestApp.setContext(applicationContext);
    component.getDefaultHost().attach(controllerRestApp);
    MetricsHelper.initializeMetrics(config.subset("pinot.controller.metrics"));
    MetricsHelper.registerMetricsRegistry(_metricsRegistry);
    final ControllerMetrics controllerMetrics = new ControllerMetrics(_metricsRegistry);
    try {
        LOGGER.info("Starting Pinot Helix resource manager and connecting to Zookeeper");
        helixResourceManager.start();
        // Helix resource manager must be started in order to create PinotLLCRealtimeSegmentManager
        PinotLLCRealtimeSegmentManager.create(helixResourceManager, config, controllerMetrics);
        ValidationMetrics validationMetrics = new ValidationMetrics(_metricsRegistry);
        validationManager = new ValidationManager(validationMetrics, helixResourceManager, config, PinotLLCRealtimeSegmentManager.getInstance());
        LOGGER.info("Starting Pinot REST API component");
        component.start();
        LOGGER.info("Starting retention manager");
        retentionManager.start();
        LOGGER.info("Starting validation manager");
        validationManager.start();
        LOGGER.info("Starting realtime segment manager");
        realtimeSegmentsManager.start(controllerMetrics);
        PinotLLCRealtimeSegmentManager.getInstance().start();
        LOGGER.info("Starting segment status manager");
        segmentStatusChecker.start(controllerMetrics);
        LOGGER.info("Pinot controller ready and listening on port {} for API requests", config.getControllerPort());
        LOGGER.info("Controller services available at http://{}:{}/", config.getControllerHost(), config.getControllerPort());
    } catch (final Exception e) {
        LOGGER.error("Caught exception while starting controller", e);
        Utils.rethrowException(e);
        throw new AssertionError("Should not reach this");
    }
    controllerMetrics.addCallbackGauge("helix.connected", new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            return helixResourceManager.getHelixZkManager().isConnected() ? 1L : 0L;
        }
    });
    controllerMetrics.addCallbackGauge("helix.leader", new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            return helixResourceManager.getHelixZkManager().isLeader() ? 1L : 0L;
        }
    });
    controllerMetrics.addCallbackGauge("dataDir.exists", new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            return new File(config.getDataDir()).exists() ? 1L : 0L;
        }
    });
    controllerMetrics.addCallbackGauge("dataDir.fileOpLatencyMs", new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            File dataDir = new File(config.getDataDir());
            if (dataDir.exists()) {
                try {
                    long startTime = System.currentTimeMillis();
                    final File testFile = new File(dataDir, config.getControllerHost());
                    FileOutputStream outputStream = new FileOutputStream(testFile, false);
                    outputStream.write(Longs.toByteArray(System.currentTimeMillis()));
                    outputStream.flush();
                    outputStream.close();
                    FileUtils.deleteQuietly(testFile);
                    long endTime = System.currentTimeMillis();
                    return endTime - startTime;
                } catch (IOException e) {
                    LOGGER.warn("Caught exception while checking the data directory operation latency", e);
                    return DATA_DIRECTORY_EXCEPTION_VALUE;
                }
            } else {
                return DATA_DIRECTORY_MISSING_VALUE;
            }
        }
    });
    ServiceStatus.setServiceStatusCallback(new ServiceStatus.ServiceStatusCallback() {

        private boolean _isStarted = false;

        @Override
        public ServiceStatus.Status getServiceStatus() {
            if (_isStarted) {
                // If we've connected to Helix at some point, the instance status depends on being connected to ZK
                if (helixResourceManager.getHelixZkManager().isConnected()) {
                    return ServiceStatus.Status.GOOD;
                } else {
                    return ServiceStatus.Status.BAD;
                }
            }
            // Return starting until zk is connected
            if (!helixResourceManager.getHelixZkManager().isConnected()) {
                return ServiceStatus.Status.STARTING;
            } else {
                _isStarted = true;
                return ServiceStatus.Status.GOOD;
            }
        }
    });
    helixResourceManager.getHelixZkManager().addPreConnectCallback(new PreConnectCallback() {

        @Override
        public void onPreConnect() {
            controllerMetrics.addMeteredGlobalValue(ControllerMeter.HELIX_ZOOKEEPER_RECONNECTS, 1L);
        }
    });
    controllerMetrics.initializeGlobalMeters();
    ControllerRestApplication.setControllerMetrics(controllerMetrics);
}
Also used : Context(org.restlet.Context) ServiceStatus(com.linkedin.pinot.common.utils.ServiceStatus) PinotHelixResourceManager(com.linkedin.pinot.controller.helix.core.PinotHelixResourceManager) IOException(java.io.IOException) ControllerMetrics(com.linkedin.pinot.common.metrics.ControllerMetrics) IOException(java.io.IOException) ValidationMetrics(com.linkedin.pinot.common.metrics.ValidationMetrics) Executor(java.util.concurrent.Executor) ServiceStatus(com.linkedin.pinot.common.utils.ServiceStatus) FileOutputStream(java.io.FileOutputStream) PreConnectCallback(org.apache.helix.PreConnectCallback) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) File(java.io.File) ValidationManager(com.linkedin.pinot.controller.validation.ValidationManager)

Example 14 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project kotlin by JetBrains.

the class ResolveDescriptorsFromExternalLibraries method getFileFromUrl.

private static File getFileFromUrl(@NotNull String lib, @NotNull File file, @NotNull String uri) throws IOException {
    if (file.exists()) {
        return file;
    }
    KotlinTestUtils.mkdirs(file.getParentFile());
    File tmp = new File(file.getPath() + "~");
    GetMethod method = new GetMethod(uri);
    FileOutputStream os = null;
    System.out.println("Downloading library " + lib + " to " + file);
    MultiThreadedHttpConnectionManager connectionManager = null;
    try {
        connectionManager = new MultiThreadedHttpConnectionManager();
        HttpClient httpClient = new HttpClient(connectionManager);
        os = new FileOutputStream(tmp);
        String userAgent = ResolveDescriptorsFromExternalLibraries.class.getName() + "/commons-httpclient";
        method.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
        int code = httpClient.executeMethod(method);
        if (code != 200) {
            throw new RuntimeException("failed to execute GET " + uri + ", code is " + code);
        }
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        if (responseBodyAsStream == null) {
            throw new RuntimeException("method is executed fine, but response is null");
        }
        ByteStreams.copy(responseBodyAsStream, os);
        os.close();
        if (!tmp.renameTo(file)) {
            throw new RuntimeException("failed to rename file: " + tmp + " to " + file);
        }
        return file;
    } finally {
        try {
            method.releaseConnection();
        } catch (Throwable e) {
        }
        if (connectionManager != null) {
            try {
                connectionManager.shutdown();
            } catch (Throwable e) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (Throwable e) {
            }
        }
        tmp.delete();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Example 15 with MultiThreadedHttpConnectionManager

use of org.apache.commons.httpclient.MultiThreadedHttpConnectionManager in project sling by apache.

the class JsonPipe method configureHttpClient.

/**
     * Configure http client
     */
private void configureHttpClient() {
    HttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    manager.setParams(params);
    client = new HttpClient(manager);
    client.getParams().setAuthenticationPreemptive(false);
}
Also used : HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Aggregations

MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)34 HttpClient (org.apache.commons.httpclient.HttpClient)26 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)13 IOException (java.io.IOException)11 HttpConnectionManager (org.apache.commons.httpclient.HttpConnectionManager)7 HttpMethodRetryHandler (org.apache.commons.httpclient.HttpMethodRetryHandler)7 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)6 HttpMethod (org.apache.commons.httpclient.HttpMethod)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 Protocol (org.apache.commons.httpclient.protocol.Protocol)6 ApacheHttpClientHandler (com.sun.jersey.client.apache.ApacheHttpClientHandler)5 HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)5 Credentials (org.apache.commons.httpclient.Credentials)3 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 AdvancedGeoCoder (com.google.code.geocoder.AdvancedGeoCoder)2 SocketTimeoutException (java.net.SocketTimeoutException)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 HttpException (org.apache.commons.httpclient.HttpException)2