Java Code Examples for com.alibaba.csp.sentinel.slots.block.BlockException#printStackTrace()

The following examples show how to use com.alibaba.csp.sentinel.slots.block.BlockException#printStackTrace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AsyncEntryDemo.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void directlyAsync() {
    try {
        final AsyncEntry entry = SphU.asyncEntry("test-async-not-nested");

        this.invoke("abc", result -> {
            // If no nested entry later, we don't have to wrap in `ContextUtil.runOnContext()`.
            try {
                // Here to handle the async result (without other entry).
            } finally {
                // Exit the async entry.
                entry.exit();
            }
        });
    } catch (BlockException e) {
        // Request blocked, handle the exception.
        e.printStackTrace();
    }
}
 
Example 2
Source File: AsyncEntryDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void directlyAsync() {
    try {
        final AsyncEntry entry = SphU.asyncEntry("test-async-not-nested");

        this.invoke("abc", result -> {
            // If no nested entry later, we don't have to wrap in `ContextUtil.runOnContext()`.
            try {
                // Here to handle the async result (without other entry).
            } finally {
                // Exit the async entry.
                entry.exit();
            }
        });
    } catch (BlockException e) {
        // Request blocked, handle the exception.
        e.printStackTrace();
    }
}
 
Example 3
Source File: FlowPartialIntegrationTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFlowRule_other() {

    FlowRule flowRule = new FlowRule();
    flowRule.setResource("testOther");
    flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    flowRule.setCount(0);
    flowRule.setLimitApp("other");
    FlowRuleManager.loadRules(Arrays.asList(flowRule));

    Entry e = null;
    try {
        e = SphU.entry("testOther");
    } catch (BlockException e1) {
        e1.printStackTrace();fail("Should had failed");
    }

    if (e != null) {
        e.exit();
    } else {
        fail("Should had failed");
    }
}
 
Example 4
Source File: AsyncEntryDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void anotherAsync() {
    try {
        final AsyncEntry entry = SphU.asyncEntry("test-another-async");

        CompletableFuture.runAsync(() -> {
            ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    // Normal entry nested in asynchronous entry.
                    anotherSyncInAsync();

                    System.out.println("Async result: 666");
                } catch (InterruptedException e) {
                    // Ignore.
                } finally {
                    entry.exit();
                }
            });
        });
    } catch (BlockException ex) {
        ex.printStackTrace();
    }
}
 
Example 5
Source File: AsyncEntryIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void fetchSync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-sync");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 6
Source File: AsyncEntryDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void anotherSyncInAsync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-another-sync-in-async");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 7
Source File: AsyncEntryIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncEntryUnderSyncEntry() throws Exception {
    // Expected invocation chain:
    // EntranceNode: machine-root
    // -EntranceNode: async-context
    // --test-top
    // ---test-async
    // ----test-sync-in-async
    // ----test-another-async
    // -----test-another-in-async
    // ---test-sync
    ContextUtil.enter(contextName, origin);
    Entry entry = null;
    try {
        entry = SphU.entry("test-top");
        doAsyncThenSync();
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
        ContextUtil.exit();
    }

    // we keep the original timeout of 15 seconds although the test should
    // complete in less than 3 seconds
    await().timeout(15, TimeUnit.SECONDS)
        .until(new Callable<DefaultNode>() {
            @Override
            public DefaultNode call() throws Exception {
                return queryInvocationTree(false);
            }
        }, CoreMatchers.notNullValue());

    queryInvocationTree(true);
}
 
Example 8
Source File: AsyncEntryIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void anotherAsync() {
    try {
        final AsyncEntry entry = SphU.asyncEntry("test-another-async");

        runAsync(new Runnable() {
            @Override
            public void run() {
                ContextUtil.runOnContext(entry.getAsyncContext(), new Runnable() {
                    @Override
                    public void run() {
                        try {
                            TimeUnit.MILLISECONDS.sleep(500);
                            anotherSyncInAsync();
                            System.out.println("Async result: 666");
                        } catch (InterruptedException e) {
                            // Ignore.
                        } finally {
                            entry.exit();
                        }
                    }
                });
            }
        });
    } catch (BlockException ex) {
        ex.printStackTrace();
    }
}
 
Example 9
Source File: AsyncEntryIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncEntryUnderSyncEntry() throws Exception {
    // Expected invocation chain:
    // EntranceNode: machine-root
    // -EntranceNode: async-context
    // --test-top
    // ---test-async
    // ----test-sync-in-async
    // ----test-another-async
    // -----test-another-in-async
    // ---test-sync
    ContextUtil.enter(contextName, origin);
    Entry entry = null;
    try {
        entry = SphU.entry("test-top");
        doAsyncThenSync();
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
        ContextUtil.exit();
    }

    // we keep the original timeout of 15 seconds although the test should
    // complete in less than 3 seconds
    await().timeout(15, TimeUnit.SECONDS)
        .until(new Callable<DefaultNode>() {
            @Override
            public DefaultNode call() throws Exception {
                return queryInvocationTree(false);
            }
        }, CoreMatchers.notNullValue());

    queryInvocationTree(true);
}
 
Example 10
Source File: AsyncEntryIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public void anotherSyncInAsync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-another-in-async");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 11
Source File: AsyncEntryIntegrationTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void fetchSyncInAsync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-sync-in-async");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 12
Source File: SlotChainBuilderSpiDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // You will see this in record.log, indicating that the custom slot chain builder is activated:
    // [SlotChainProvider] Global slot chain builder resolved: com.alibaba.csp.sentinel.demo.slot.DemoSlotChainBuilder
    Entry entry = null;
    try {
        entry = SphU.entry("abc");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 13
Source File: AsyncEntryDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    initFlowRule();

    AsyncEntryDemo service = new AsyncEntryDemo();

    // Expected invocation chain:
    //
    // EntranceNode: machine-root
    // -EntranceNode: async-context
    // --test-top
    // ---test-sync
    // ---test-async
    // ----test-another-async
    // -----test-another-sync-in-async
    // ----test-sync-in-async
    ContextUtil.enter("async-context", "originA");
    Entry entry = null;
    try {
        entry = SphU.entry("test-top");
        System.out.println("Do something...");
        service.doAsyncThenSync();
    } catch (BlockException ex) {
        // Request blocked, handle the exception.
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
        ContextUtil.exit();
    }

    TimeUnit.SECONDS.sleep(20);
}
 
Example 14
Source File: AsyncEntryIntegrationTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public void anotherSyncInAsync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-another-in-async");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 15
Source File: AsyncEntryDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void doAsyncThenSync() {
    try {
        // First we call an asynchronous resource.
        final AsyncEntry entry = SphU.asyncEntry("test-async");
        this.invoke("abc", resp -> {
            // The thread is different from original caller thread for async entry.
            // So we need to wrap in the async context so that nested invocation entry
            // can be linked to the parent asynchronous entry.
            ContextUtil.runOnContext(entry.getAsyncContext(), () -> {
                try {
                    // In the callback, we do another async invocation several times under the async context.
                    for (int i = 0; i < 7; i++) {
                        anotherAsync();
                    }

                    System.out.println(resp);

                    // Then we do a sync (normal) entry under current async context.
                    fetchSyncInAsync();
                } finally {
                    // Exit the async entry.
                    entry.exit();
                }
            });
        });
        // Then we call a sync resource.
        fetchSync();
    } catch (BlockException ex) {
        // Request blocked, handle the exception.
        ex.printStackTrace();
    }
}
 
Example 16
Source File: AsyncEntryDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void fetchSyncInAsync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-sync-in-async");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 17
Source File: AsyncEntryDemo.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void fetchSync() {
    Entry entry = null;
    try {
        entry = SphU.entry("test-sync");
    } catch (BlockException ex) {
        ex.printStackTrace();
    } finally {
        if (entry != null) {
            entry.exit();
        }
    }
}
 
Example 18
Source File: DemoService.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public String sayHelloBlockHandler(String name, BlockException ex) {
    // This is the block handler.
    ex.printStackTrace();
    return String.format("Oops, <%s> blocked by Sentinel", name);
}
 
Example 19
Source File: DemoService.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public String sayHelloBlockHandler(String name, BlockException ex) {
    // This is the block handler.
    ex.printStackTrace();
    return String.format("Oops, <%s> blocked by Sentinel", name);
}
 
Example 20
Source File: UserController.java    From j360-boot-app-all with Apache License 2.0 4 votes vote down vote up
public String exceptionHandler(long s, BlockException ex) {
    // Do some log here.
    ex.printStackTrace();
    return "Oops, error occurred at " + s;
}