Example usage for org.apache.http.entity StringEntity StringEntity

List of usage examples for org.apache.http.entity StringEntity StringEntity

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity StringEntity.

Prototype

public StringEntity(String str, Charset charset) 

Source Link

Usage

From source file:com.jivesoftware.os.amza.service.AmzaSetStress.java

private static void feed(org.apache.http.client.HttpClient httpClient, String hostName, int port,
        String partitionName, int firstDocId, int count, int batchSize)
        throws IOException, InterruptedException {
    long start = System.currentTimeMillis();
    for (int key = firstDocId; key < count; key++) {
        StringBuilder url = new StringBuilder();
        url.append("http://");
        url.append(hostName).append(":").append(port);
        url.append("/amza/multiSet");
        url.append("/").append(partitionName);

        Map<String, String> values = new LinkedHashMap<>();
        for (int b = 0; b < batchSize; b++) {

            values.put(b + "k" + key, b + "v" + key);
        }/*from  w  w w .  ja  v a2 s  .  com*/

        String postJsonBody = MAPPER.writeValueAsString(values);
        while (true) {
            HttpPost method = new HttpPost(url.toString());
            method.setEntity(new StringEntity(postJsonBody, ContentType.APPLICATION_JSON));
            method.setHeader(CONTENT_TYPE_HEADER_NAME, APPLICATION_JSON_CONTENT_TYPE);

            StatusLine statusLine;
            try {
                try {
                    HttpResponse response = httpClient.execute(method);
                    statusLine = response.getStatusLine();
                    if (statusLine.getStatusCode() == 200) {
                        break;
                    }
                } catch (Exception x) {
                    x.printStackTrace();
                }
                Thread.sleep(1000);
            } finally {
                method.releaseConnection();
            }
        }

        if (key % 100 == 0) {
            long elapse = System.currentTimeMillis() - start;
            double millisPerAdd = (elapse / (double) key);
            System.out.println(partitionName + " millisPerAdd:" + millisPerAdd + " addsPerSec:"
                    + (1000d / millisPerAdd) + " key:" + key);
        }
    }
}