Search in sources :

Example 21 with HttpClientResponse

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

the class RibbonExamples method main.

public static void main(String[] args) {
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient");
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("GetUser").withResponseValidator(new ResponseValidator<HttpClientResponse<ByteBuf>>() {

        @Override
        public void validate(HttpClientResponse<ByteBuf> response) throws UnsuccessfulResponseException, ServerError {
            if (response.getStatus().code() >= 500) {
                throw new ServerError("Unexpected response");
            }
        }
    }).withFallbackProvider(new FallbackHandler<ByteBuf>() {

        @Override
        public Observable<ByteBuf> getFallback(HystrixInvokableInfo<?> t1, Map<String, Object> vars) {
            return Observable.empty();
        }
    }).withHystrixProperties((HystrixObservableCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("mygroup")).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(2000)))).withUriTemplate("/{id}").build();
    template.requestBuilder().withRequestProperty("id", 1).build().execute();
    // example showing the use case of getting the entity with Hystrix meta data
    template.requestBuilder().withRequestProperty("id", 3).build().withMetadata().observe().flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<String>>() {

        @Override
        public Observable<String> call(RibbonResponse<Observable<ByteBuf>> t1) {
            if (t1.getHystrixInfo().isResponseFromFallback()) {
                return Observable.empty();
            }
            return t1.content().map(new Func1<ByteBuf, String>() {

                @Override
                public String call(ByteBuf t1) {
                    return t1.toString();
                }
            });
        }
    });
}
Also used : HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) FallbackHandler(com.netflix.ribbon.hystrix.FallbackHandler) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) Func1(rx.functions.Func1) Map(java.util.Map) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo)

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