All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mydaco.client.AsyncMydacoClient Maven / Gradle / Ivy

The newest version!
package com.mydaco.client;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;

import com.google.gson.JsonObject;


public class AsyncMydacoClient extends MydacoClient {

	private static final int MAX_CONNECTIONS = 500;
	private static final int TIMEOUT = Integer.MAX_VALUE;
	
	private final CloseableHttpAsyncClient httpclient;
	
	public AsyncMydacoClient() {
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT).setConnectTimeout(TIMEOUT).build();
		httpclient = HttpAsyncClients.custom().setMaxConnTotal(MAX_CONNECTIONS).setMaxConnPerRoute(MAX_CONNECTIONS).setDefaultRequestConfig(requestConfig).build();
		httpclient.start();
	}
	
	public void call(String endpointToken, JsonObject parameters, final ClientCallback clientCallback) {
		HttpPost httpPost = createRequest(endpointToken, parameters);
		httpclient.execute(httpPost, new FutureCallback() {
			
			public void completed(final HttpResponse response) {
				JsonObject returnObject;				
				try {
					returnObject = readResponse(response);
				} catch (IOException e) {
					returnObject = null;
				}
				clientCallback.callback(returnObject);
			}

			public void failed(final Exception ex) {
				clientCallback.callback(null);
			}

			public void cancelled() {	
				clientCallback.callback(null);
			}

		});

	}

	@Override
	public void close() throws IOException {
		httpclient.close();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy