com.fasterxml.clustermate.client.ahc.AHCEntryInspector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clustermate-client-ahc Show documentation
Show all versions of clustermate-client-ahc Show documentation
Almost complete ClusterMate NetworkClient implementation built on
Async HTTP Client
The newest version!
package com.fasterxml.clustermate.client.ahc;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.fasterxml.clustermate.api.ClusterMateConstants;
import com.fasterxml.clustermate.api.ContentType;
import com.fasterxml.clustermate.api.EntryKey;
import com.fasterxml.clustermate.api.msg.ItemInfo;
import com.fasterxml.clustermate.client.ClusterServerNode;
import com.fasterxml.clustermate.client.StoreClientConfig;
import com.fasterxml.clustermate.client.call.*;
import com.fasterxml.clustermate.client.util.ContentConverter;
import com.fasterxml.storemate.shared.util.IOUtil;
import com.ning.http.client.*;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
public class AHCEntryInspector
extends AHCBasedAccessor
implements EntryInspector
{
public AHCEntryInspector(StoreClientConfig storeConfig,
AsyncHttpClient hc, ClusterServerNode server)
{
super(storeConfig, hc, server);
}
/*
/**********************************************************************
/* Call implementation
/**********************************************************************
*/
@Override
public ReadCallResult tryInspect(CallConfig config,
ReadCallParameters params,
long endOfTime, K contentId, ContentConverter converter)
{
if (converter == null) {
throw new IllegalArgumentException("Missing converter");
}
// first: if we can't spend at least 10 msecs, let's give up:
final long startTime = System.currentTimeMillis();
final long timeout = Math.min(endOfTime - startTime, config.getGetCallTimeoutMsecs());
if (timeout < config.getMinimumTimeoutMsecs()) {
return failed(CallFailure.timeout(_server, startTime, startTime));
}
AHCPathBuilder path = _server.rootPath();
path = _pathFinder.appendStoreEntryInfoPath(path);
path = _keyConverter.appendToPath(path, contentId);
BoundRequestBuilder reqBuilder = path
.listRequest(_httpClient)
;
InputStream in = null;
try {
Future futurama = _httpClient.executeRequest(reqBuilder.build());
// First, see if we can get the answer without time out...
Response resp;
try {
resp = futurama.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
return failed(CallFailure.timeout(_server, startTime, System.currentTimeMillis()));
}
// and if so, is it successful?
int statusCode = resp.getStatusCode();
// one thing first: handle standard headers, if any?
handleHeaders(_server, resp, startTime);
// call ok?
if (!IOUtil.isHTTPSuccess(statusCode)) {
if (statusCode == ClusterMateConstants.HTTP_STATUS_NOT_FOUND) { // missing entry is not a fail
return AHCReadCallResult.notFound(_server);
}
// if not, why not? Any well-known problems? (besides timeout that was handled earlier)
// then the default fallback
String msg = getExcerpt(resp, config.getMaxExcerptLength());
return failed(CallFailure.general(_server, statusCode, startTime, System.currentTimeMillis(), msg));
}
ContentType contentType = findContentType(resp, ContentType.JSON);
in = resp.getResponseBodyAsStream();
T result = converter.convert(contentType, in);
return new AHCReadCallResult(_server, result);
} catch (Exception e) {
return failed(failFromException(e, startTime));
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e2) { }
}
}
}
protected ReadCallResult failed(CallFailure fail) {
return new AHCReadCallResult(fail);
}
}