Search in sources :

Example 6 with SentinelRpcException

use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project Sentinel by alibaba.

the class FooConsumerBootstrap method main.

public static void main(String[] args) {
    initFlowRule();
    AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext();
    consumerContext.register(ConsumerConfiguration.class);
    consumerContext.refresh();
    FooServiceConsumer service = consumerContext.getBean(FooServiceConsumer.class);
    for (int i = 0; i < 10; i++) {
        pool.submit(() -> {
            try {
                String message = service.sayHello("Eric");
                System.out.println("Success: " + message);
            } catch (SentinelRpcException ex) {
                System.out.println("Blocked");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        pool.submit(() -> System.out.println("Another: " + service.doAnother()));
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FooServiceConsumer(com.alibaba.csp.sentinel.demo.dubbo.consumer.FooServiceConsumer) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException)

Example 7 with SentinelRpcException

use of com.alibaba.csp.sentinel.slots.block.SentinelRpcException in project Sentinel by alibaba.

the class DefaultSofaRpcFallbackTest method testHandle.

@Test
public void testHandle() {
    SofaRpcFallback sofaRpcFallback = new DefaultSofaRpcFallback();
    BlockException blockException = mock(BlockException.class);
    boolean throwSentinelRpcException = false;
    boolean causeIsBlockException = false;
    try {
        sofaRpcFallback.handle(null, null, blockException);
    } catch (Exception e) {
        throwSentinelRpcException = e instanceof SentinelRpcException;
        causeIsBlockException = e.getCause() instanceof BlockException;
    }
    assertTrue(throwSentinelRpcException);
    assertTrue(causeIsBlockException);
}
Also used : BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) SentinelRpcException(com.alibaba.csp.sentinel.slots.block.SentinelRpcException) Test(org.junit.Test)

Aggregations

SentinelRpcException (com.alibaba.csp.sentinel.slots.block.SentinelRpcException)7 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 FooServiceConsumer (com.alibaba.csp.sentinel.demo.apache.dubbo.consumer.FooServiceConsumer)2 FooServiceConsumer (com.alibaba.csp.sentinel.demo.dubbo.consumer.FooServiceConsumer)2 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)1 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.Test)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 ApplicationContext (org.springframework.context.ApplicationContext)1