use of org.asynchttpclient.Request in project tutorials by eugenp.
the class AsyncHttpClientTestCase method givenHttpClient_executeAsyncGetRequestWithAsyncHandler.
@Test
public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() {
// execute an unbound GET request
Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build();
HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler<Integer>() {
int responseStatusCode = -1;
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) {
responseStatusCode = responseStatus.getStatusCode();
return State.CONTINUE;
}
@Override
public State onHeadersReceived(HttpHeaders headers) {
return State.CONTINUE;
}
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) {
return State.CONTINUE;
}
@Override
public void onThrowable(Throwable t) {
}
@Override
public Integer onCompleted() {
assertEquals(200, responseStatusCode);
return responseStatusCode;
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of org.asynchttpclient.Request in project incubator-pulsar by apache.
the class BrokerServiceLookupTest method getHttpClient.
private AsyncHttpClient getHttpClient(String version) {
DefaultAsyncHttpClientConfig.Builder confBuilder = new DefaultAsyncHttpClientConfig.Builder();
confBuilder.setFollowRedirect(true);
confBuilder.setUserAgent(version);
confBuilder.setKeepAliveStrategy(new DefaultKeepAliveStrategy() {
@Override
public boolean keepAlive(Request ahcRequest, HttpRequest request, HttpResponse response) {
// Close connection upon a server error or per HTTP spec
return (response.status().code() / 100 != 5) && super.keepAlive(ahcRequest, request, response);
}
});
AsyncHttpClientConfig config = confBuilder.build();
return new DefaultAsyncHttpClient(config);
}
use of org.asynchttpclient.Request in project druid by druid-io.
the class HttpPostEmitterStressTest method testLargeAndSmallEventsQueueLimit.
@Test
public void testLargeAndSmallEventsQueueLimit() throws InterruptedException, IOException {
HttpEmitterConfig config = new HttpEmitterConfig.Builder("http://foo.bar").setFlushMillis(100).setFlushCount(4).setBatchingStrategy(BatchingStrategy.ONLY_EVENTS).setMaxBatchSize(1024 * 1024).setBatchQueueSizeLimit(10).build();
final HttpPostEmitter emitter = new HttpPostEmitter(config, httpClient, new ObjectMapper());
emitter.start();
httpClient.setGoHandler(new GoHandler() {
@Override
protected <X extends Exception> ListenableFuture<Response> go(Request request) throws X {
return GoHandlers.immediateFuture(EmitterTest.BAD_RESPONSE);
}
});
char[] chars = new char[600000];
Arrays.fill(chars, '*');
String bigString = new String(chars);
Event smallEvent = ServiceMetricEvent.builder().setFeed("smallEvents").setDimension("test", "hi").build("metric", 10).build("qwerty", "asdfgh");
Event bigEvent = ServiceMetricEvent.builder().setFeed("bigEvents").setDimension("test", bigString).build("metric", 10).build("qwerty", "asdfgh");
final CountDownLatch threadsCompleted = new CountDownLatch(2);
new Thread() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
emitter.emit(smallEvent);
Assert.assertTrue(emitter.getTotalFailedBuffers() <= 10);
Assert.assertTrue(emitter.getBuffersToEmit() <= 12);
}
threadsCompleted.countDown();
}
}.start();
new Thread() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
emitter.emit(bigEvent);
Assert.assertTrue(emitter.getTotalFailedBuffers() <= 10);
Assert.assertTrue(emitter.getBuffersToEmit() <= 12);
}
threadsCompleted.countDown();
}
}.start();
threadsCompleted.await();
emitter.flush();
}
use of org.asynchttpclient.Request in project druid by druid-io.
the class HttpPostEmitterStressTest method eventCountBased.
@Test
public void eventCountBased() throws InterruptedException, IOException {
HttpEmitterConfig config = new HttpEmitterConfig.Builder("http://foo.bar").setFlushMillis(100).setFlushCount(4).setFlushTimeout(BaseHttpEmittingConfig.TEST_FLUSH_TIMEOUT_MILLIS).setBatchingStrategy(BatchingStrategy.ONLY_EVENTS).setMaxBatchSize(1024 * 1024).setBatchQueueSizeLimit(1000).build();
final HttpPostEmitter emitter = new HttpPostEmitter(config, httpClient, OBJECT_MAPPER);
int nThreads = Runtime.getRuntime().availableProcessors() * 2;
final List<IntList> eventsPerThread = new ArrayList<>(nThreads);
final List<List<Batch>> eventBatchesPerThread = new ArrayList<>(nThreads);
for (int i = 0; i < nThreads; i++) {
eventsPerThread.add(new IntArrayList());
eventBatchesPerThread.add(new ArrayList<Batch>());
}
for (int i = 0; i < N; i++) {
eventsPerThread.get(ThreadLocalRandom.current().nextInt(nThreads)).add(i);
}
final BitSet emittedEvents = new BitSet(N);
httpClient.setGoHandler(new GoHandler() {
@Override
protected ListenableFuture<Response> go(Request request) {
ByteBuffer batch = request.getByteBufferData().slice();
while (batch.remaining() > 0) {
emittedEvents.set(batch.getInt());
}
return GoHandlers.immediateFuture(EmitterTest.okResponse());
}
});
emitter.start();
final CountDownLatch threadsCompleted = new CountDownLatch(nThreads);
for (int i = 0; i < nThreads; i++) {
final int threadIndex = i;
new Thread() {
@Override
public void run() {
IntList events = eventsPerThread.get(threadIndex);
List<Batch> eventBatches = eventBatchesPerThread.get(threadIndex);
IntEvent event = new IntEvent();
for (int i = 0, eventsSize = events.size(); i < eventsSize; i++) {
event.index = events.getInt(i);
eventBatches.add(emitter.emitAndReturnBatch(event));
if (i % 16 == 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
threadsCompleted.countDown();
}
}.start();
}
threadsCompleted.await();
emitter.flush();
System.out.println("Allocated buffers: " + emitter.getTotalAllocatedBuffers());
for (int eventIndex = 0; eventIndex < N; eventIndex++) {
if (!emittedEvents.get(eventIndex)) {
for (int threadIndex = 0; threadIndex < eventsPerThread.size(); threadIndex++) {
IntList threadEvents = eventsPerThread.get(threadIndex);
int indexOfEvent = threadEvents.indexOf(eventIndex);
if (indexOfEvent >= 0) {
Batch batch = eventBatchesPerThread.get(threadIndex).get(indexOfEvent);
System.err.println(batch);
int bufferWatermark = batch.getSealedBufferWatermark();
ByteBuffer batchBuffer = ByteBuffer.wrap(batch.buffer);
batchBuffer.limit(bufferWatermark);
while (batchBuffer.remaining() > 0) {
System.err.println(batchBuffer.getInt());
}
break;
}
}
throw new AssertionError("event " + eventIndex);
}
}
}
use of org.asynchttpclient.Request in project druid by druid-io.
the class EmitterTest method testSanityWithGeneralizedCreation.
@Test
public void testSanityWithGeneralizedCreation() throws Exception {
final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
emitter = sizeBasedEmitterGeneralizedCreation(2);
httpClient.setGoHandler(new GoHandler() {
@Override
protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
Assert.assertEquals(TARGET_URL, request.getUrl());
Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
return GoHandlers.immediateFuture(okResponse());
}
}.times(1));
for (UnitEvent event : events) {
emitter.emit(event);
}
waitForEmission(emitter, 1);
closeNoFlush(emitter);
Assert.assertTrue(httpClient.succeeded());
}
Aggregations