public class Connection extends Object implements Closeable
Make sure to close this connection to avoid resources leak.
| Modifier and Type | Field and Description |
|---|---|
static String |
COMET_SDK_API_HEADER |
static int |
CONNECTION_SHUTDOWN_TIMEOUT_MS |
static int |
REQUEST_TIMEOUT_MS |
| Constructor and Description |
|---|
Connection(@NonNull String cometBaseUrl,
@NonNull String apiKey,
int maxAuthRetries,
@NonNull org.slf4j.Logger logger)
Creates new instance with specified parameters.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes this connection immediately by releasing underlying resources.
|
Optional<String> |
sendGet(@NonNull String endpoint,
@NonNull Map<String,String> params)
Allows sending synchronous GET request to the specified endpoint with given request parameters.
|
Optional<String> |
sendPost(@NonNull String json,
@NonNull String endpoint,
boolean throwOnFailure)
Allows sending POST to the specified endpoint with body as JSON string.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(byte[] bytes,
@NonNull String endpoint,
@NonNull Map<String,String> params)
Allows asynchronous sending of provided byte array as POST request to the specified endpoint.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(@NonNull File file,
@NonNull String endpoint,
@NonNull Map<String,String> params)
Allows asynchronous posting the content of the file as multipart form data to the specified endpoint.
|
void |
sendPostAsync(@NonNull Object payload,
@NonNull String endpoint)
Allows asynchronous sending given object as JSON encoded body of the POST request.
|
org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> |
sendPostAsync(@NonNull String json,
@NonNull String endpoint)
Allows asynchronous sending of text as JSON encoded body of the POST request.
|
void |
waitAndClose(long timeout,
TimeUnit unit)
Allows to properly close this connection after all scheduled posts request are executed or if timeout expired.
|
public static final int REQUEST_TIMEOUT_MS
public static final int CONNECTION_SHUTDOWN_TIMEOUT_MS
public static final String COMET_SDK_API_HEADER
public Connection(@NonNull
@NonNull String cometBaseUrl,
@NonNull
@NonNull String apiKey,
int maxAuthRetries,
@NonNull
@NonNull org.slf4j.Logger logger)
cometBaseUrl - the base URL of the Comet server's endpoints.apiKey - the API key to authorize Comet API accessmaxAuthRetries - the maximum number of retries per failed request.logger - the Logger to collect log records.public Optional<String> sendGet(@NonNull @NonNull String endpoint, @NonNull @NonNull Map<String,String> params)
endpoint - the request path of the endpointparams - the map with request parameters.public Optional<String> sendPost(@NonNull @NonNull String json, @NonNull @NonNull String endpoint, boolean throwOnFailure)
json - the JSON string to be posted.endpoint - the relative path to the endpointthrowOnFailure - the flag to indicate if exception should be thrown on failure of request execution.public void sendPostAsync(@NonNull
@NonNull Object payload,
@NonNull
@NonNull String endpoint)
payload - the payload object to be sent.endpoint - the relative path to the endpoint.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(@NonNull
@NonNull String json,
@NonNull
@NonNull String endpoint)
json - the JSON encoded text.endpoint - the relative path to the endpoint.ListenableFuture<Response> which can be used to monitor status of
the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(@NonNull
@NonNull File file,
@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<String,String> params)
file - the file to be included.endpoint - the relative path to the endpoint.params - the request parametersListenableFuture<Response> which can be used to monitor status of
the request execution.public org.asynchttpclient.ListenableFuture<org.asynchttpclient.Response> sendPostAsync(byte[] bytes,
@NonNull
@NonNull String endpoint,
@NonNull
@NonNull Map<String,String> params)
bytes - the data arrayendpoint - the relative path to the endpoint.params - the request parameters map.ListenableFuture<Response> which can be used to monitor status of
the request execution.public void close()
throws IOException
Please note that some asynchronous post request can still be not processed, which will result in errors. Use this method with great caution and only if you are not expecting any request still be unprocessed. For all other cases it is recommended to use waitAndClose method.
close in interface Closeableclose in interface AutoCloseableIOException - if an I/O error occurs.public void waitAndClose(long timeout,
TimeUnit unit)
throws IOException,
InterruptedException,
TimeoutException
timeout - the maximum time to wait.unit - the time unit of the timeout argument.IOException - if an I/O error occurs.InterruptedException - if current thread was interrupted during wait.TimeoutExceptionCopyright © 2021. All rights reserved.