Example usage for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient

List of usage examples for org.apache.http.impl.client SystemDefaultHttpClient SystemDefaultHttpClient

Introduction

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

Prototype

public SystemDefaultHttpClient() 

Source Link

Usage

From source file:org.wso2.carbon.sample.http.performance.Http.java

@Override
public void run() {
    HttpClient httpClient = new SystemDefaultHttpClient();
    try {//from  w ww  . j a  v a2  s  .  c om
        HttpPost method = new HttpPost(url);
        log.info("Sending messages..");
        long lastTime = System.currentTimeMillis();
        DecimalFormat decimalFormat = new DecimalFormat("#");
        while (count < noOfEvents) {
            count++;
            String temp = "{\"event\": " + getRandomEvent(count).toString() + "}";
            StringEntity entity = new StringEntity(temp);
            method.setEntity(entity);
            if (url.startsWith("https")) {
                processAuthentication(method, username, password);
            }
            httpClient.execute(method).getEntity().getContent().close();
            if (count % elapsedCount == 0) {
                long currentTime = System.currentTimeMillis();
                long elapsedTime = currentTime - lastTime;
                double throughputPerSecond = (((double) elapsedCount) / elapsedTime) * 1000;
                lastTime = currentTime;
                log.info("Sent " + elapsedCount + " sensor events in " + elapsedTime
                        + " milliseconds with total throughput of " + decimalFormat.format(throughputPerSecond)
                        + " events per second.");
            }
        }
    } catch (Throwable t) {
        log.error("Error when sending the messages", t);
    }
}