Search in sources :

Example 1 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class SimpleGet method main.

@edu.umd.cs.findbugs.annotations.SuppressWarnings
public static void main(String[] args) throws Exception {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient();
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com/");
    final CountDownLatch latch = new CountDownLatch(1);
    client.submit(request).toBlocking().forEach(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> t1) {
            System.out.println("Status code: " + t1.getStatus());
            t1.getContent().subscribe(new Action1<ByteBuf>() {

                @Override
                public void call(ByteBuf content) {
                    System.out.println("Response content: " + content.toString(Charset.defaultCharset()));
                    latch.countDown();
                }
            });
        }
    });
    latch.await(2, TimeUnit.SECONDS);
}
Also used : Action1(rx.functions.Action1) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class RxMovieServerTest method testUpateRecommendations.

@Test
public void testUpateRecommendations() {
    movieServer.movies.put(ORANGE_IS_THE_NEW_BLACK.getId(), ORANGE_IS_THE_NEW_BLACK);
    HttpResponseStatus statusCode = RxNetty.createHttpPost(baseURL + "/users/" + TEST_USER_ID + "/recommendations", Observable.just(ORANGE_IS_THE_NEW_BLACK.getId()), new StringTransformer()).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<HttpResponseStatus>>() {

        @Override
        public Observable<HttpResponseStatus> call(HttpClientResponse<ByteBuf> httpClientResponse) {
            return Observable.just(httpClientResponse.getStatus());
        }
    }).toBlocking().first();
    assertEquals(HttpResponseStatus.OK, statusCode);
    assertTrue(movieServer.userRecommendations.get(TEST_USER_ID).contains(ORANGE_IS_THE_NEW_BLACK.getId()));
}
Also used : StringTransformer(io.reactivex.netty.channel.StringTransformer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) Test(org.junit.Test)

Example 3 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class RxMovieServerTest method testMovieRegistration.

@Test
public void testMovieRegistration() {
    String movieFormatted = ORANGE_IS_THE_NEW_BLACK.toString();
    HttpResponseStatus statusCode = RxNetty.createHttpPost(baseURL + "/movies", Observable.just(movieFormatted), new StringTransformer()).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<HttpResponseStatus>>() {

        @Override
        public Observable<HttpResponseStatus> call(HttpClientResponse<ByteBuf> httpClientResponse) {
            return Observable.just(httpClientResponse.getStatus());
        }
    }).toBlocking().first();
    assertEquals(HttpResponseStatus.CREATED, statusCode);
    assertEquals(ORANGE_IS_THE_NEW_BLACK, movieServer.movies.get(ORANGE_IS_THE_NEW_BLACK.getId()));
}
Also used : StringTransformer(io.reactivex.netty.channel.StringTransformer) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) Test(org.junit.Test)

Example 4 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class LoadBalancingHttpClient method submit.

/**
     * Subject an operation to run in the load balancer
     * 
     * @param request
     * @param errorHandler
     * @param requestConfig
     * @param rxClientConfig
     * @return
     */
private Observable<HttpClientResponse<O>> submit(final Server server, final HttpClientRequest<I> request, final RetryHandler errorHandler, final IClientConfig requestConfig, final ClientConfig rxClientConfig) {
    RetryHandler retryHandler = errorHandler;
    if (retryHandler == null) {
        retryHandler = getRequestRetryHandler(request, requestConfig);
    }
    final IClientConfig config = requestConfig == null ? DefaultClientConfigImpl.getEmptyConfig() : requestConfig;
    final ExecutionContext<HttpClientRequest<I>> context = new ExecutionContext<HttpClientRequest<I>>(request, config, this.getClientConfig(), retryHandler);
    Observable<HttpClientResponse<O>> result = submitToServerInURI(request, config, rxClientConfig, retryHandler, context);
    if (result == null) {
        LoadBalancerCommand<HttpClientResponse<O>> command;
        if (retryHandler != defaultRetryHandler) {
            // need to create new builder instead of the default one
            command = LoadBalancerCommand.<HttpClientResponse<O>>builder().withExecutionContext(context).withLoadBalancerContext(lbContext).withListeners(listeners).withClientConfig(this.getClientConfig()).withRetryHandler(retryHandler).withServer(server).build();
        } else {
            command = defaultCommandBuilder;
        }
        result = command.submit(requestToOperation(request, getRxClientConfig(config, rxClientConfig)));
    }
    return result;
}
Also used : HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) ExecutionContext(com.netflix.loadbalancer.reactive.ExecutionContext) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) RetryHandler(com.netflix.client.RetryHandler) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) IClientConfig(com.netflix.client.config.IClientConfig) URI(java.net.URI)

Example 5 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class ListenerTest method testAbortedExecution.

@Test
public void testAbortedExecution() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {

        @Override
        public void onExecutionStart(ExecutionContext context) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
Also used : Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) ByteBuf(io.netty.buffer.ByteBuf) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionContext(com.netflix.loadbalancer.reactive.ExecutionContext) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Aggregations

HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)21 ByteBuf (io.netty.buffer.ByteBuf)18 Test (org.junit.Test)16 Server (com.netflix.loadbalancer.Server)9 MockWebServer (com.google.mockwebserver.MockWebServer)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)5 HttpServer (com.sun.net.httpserver.HttpServer)5 Observable (rx.Observable)5 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)4 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)3 DummyPing (com.netflix.loadbalancer.DummyPing)3 ExecutionContext (com.netflix.loadbalancer.reactive.ExecutionContext)3 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)3 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)3 ClientException (com.netflix.client.ClientException)2 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)2