Search in sources :

Example 11 with Request

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();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpResponseStatus(org.asynchttpclient.HttpResponseStatus) Request(org.asynchttpclient.Request) HttpResponseBodyPart(org.asynchttpclient.HttpResponseBodyPart) Test(org.junit.Test)

Example 12 with Request

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);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultKeepAliveStrategy(org.asynchttpclient.channel.DefaultKeepAliveStrategy) BoundRequestBuilder(org.asynchttpclient.BoundRequestBuilder) Request(org.asynchttpclient.Request) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) HttpResponse(io.netty.handler.codec.http.HttpResponse) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) DefaultAsyncHttpClientConfig(org.asynchttpclient.DefaultAsyncHttpClientConfig) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient)

Example 13 with Request

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();
}
Also used : Request(org.asynchttpclient.Request) CountDownLatch(java.util.concurrent.CountDownLatch) ListenableFuture(org.asynchttpclient.ListenableFuture) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with Request

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) BitSet(java.util.BitSet) Request(org.asynchttpclient.Request) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) IntList(it.unimi.dsi.fastutil.ints.IntList) ListenableFuture(org.asynchttpclient.ListenableFuture) ArrayList(java.util.ArrayList) List(java.util.List) IntList(it.unimi.dsi.fastutil.ints.IntList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) Test(org.junit.Test)

Example 15 with Request

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());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Aggregations

Request (org.asynchttpclient.Request)67 Test (org.testng.annotations.Test)32 Response (org.asynchttpclient.Response)31 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)18 RequestBuilder (org.asynchttpclient.RequestBuilder)17 Test (org.junit.Test)16 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)8 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)8 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)6 ListenableFuture (org.asynchttpclient.ListenableFuture)6 Uri (org.asynchttpclient.uri.Uri)5 Realm (org.asynchttpclient.Realm)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3