Search in sources :

Example 1 with FallbackHandler

use of com.netflix.ribbon.hystrix.FallbackHandler in project ribbon by Netflix.

the class RibbonTest method testFallback.

@Test
public void testFallback() throws IOException {
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:12345").withMaxAutoRetriesNextServer(1));
    final String fallback = "fallback";
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").withFallbackProvider(new FallbackHandler<ByteBuf>() {

        @Override
        public Observable<ByteBuf> getFallback(HystrixInvokableInfo<?> hystrixInfo, Map<String, Object> requestProperties) {
            try {
                return Observable.just(Unpooled.buffer().writeBytes(fallback.getBytes("UTF-8")));
            } catch (UnsupportedEncodingException e) {
                return Observable.error(e);
            }
        }
    }).build();
    RibbonRequest<ByteBuf> request = template.requestBuilder().build();
    final AtomicReference<HystrixInvokableInfo<?>> hystrixInfo = new AtomicReference<HystrixInvokableInfo<?>>();
    final AtomicBoolean failed = new AtomicBoolean(false);
    Observable<String> result = request.withMetadata().toObservable().flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<String>>() {

        @Override
        public Observable<String> call(final RibbonResponse<Observable<ByteBuf>> response) {
            hystrixInfo.set(response.getHystrixInfo());
            failed.set(response.getHystrixInfo().isFailedExecution());
            return response.content().map(new Func1<ByteBuf, String>() {

                @Override
                public String call(ByteBuf t1) {
                    return t1.toString(Charset.defaultCharset());
                }
            });
        }
    });
    String s = result.toBlocking().single();
    // this returns true only after the blocking call is done
    assertTrue(hystrixInfo.get().isResponseFromFallback());
    assertTrue(failed.get());
    assertEquals(fallback, s);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) FallbackHandler(com.netflix.ribbon.hystrix.FallbackHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Func1(rx.functions.Func1) Map(java.util.Map) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Test(org.junit.Test)

Example 2 with FallbackHandler

use of com.netflix.ribbon.hystrix.FallbackHandler 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

HystrixInvokableInfo (com.netflix.hystrix.HystrixInvokableInfo)2 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)2 FallbackHandler (com.netflix.ribbon.hystrix.FallbackHandler)2 ByteBuf (io.netty.buffer.ByteBuf)2 Map (java.util.Map)2 Observable (rx.Observable)2 Func1 (rx.functions.Func1)2 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1