Java Code Examples for com.alibaba.csp.sentinel.SphU#asyncEntry()
The following examples show how to use
com.alibaba.csp.sentinel.SphU#asyncEntry() .
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-Dashboard-Nacos with Apache License 2.0 | 6 votes |
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 2
Source File: AsyncEntryDemo.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
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: SentinelReactorSubscriber.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
private void entryWhenSubscribed() { ContextConfig sentinelContextConfig = entryConfig.getContextConfig(); if (sentinelContextConfig != null) { // If current we're already in a context, the context config won't work. ContextUtil.enter(sentinelContextConfig.getContextName(), sentinelContextConfig.getOrigin()); } try { AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getEntryType(), entryConfig.getAcquireCount(), entryConfig.getArgs()); this.currentEntry = entry; actual.onSubscribe(this); } catch (BlockException ex) { // Mark as completed (exited) explicitly. entryExited.set(true); // Signal cancel and propagate the {@code BlockException}. cancel(); actual.onSubscribe(this); actual.onError(ex); } finally { if (sentinelContextConfig != null) { ContextUtil.exit(); } } }
Example 4
Source File: AsyncEntryDemo.java From Sentinel with Apache License 2.0 | 6 votes |
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: AsyncEntryDemo.java From Sentinel with Apache License 2.0 | 6 votes |
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 6
Source File: SentinelReactorSubscriber.java From Sentinel with Apache License 2.0 | 6 votes |
private void entryWhenSubscribed() { ContextConfig sentinelContextConfig = entryConfig.getContextConfig(); if (sentinelContextConfig != null) { // If current we're already in a context, the context config won't work. ContextUtil.enter(sentinelContextConfig.getContextName(), sentinelContextConfig.getOrigin()); } try { AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getResourceType(), entryConfig.getEntryType(), entryConfig.getAcquireCount(), entryConfig.getArgs()); this.currentEntry = entry; actual.onSubscribe(this); } catch (BlockException ex) { // Mark as completed (exited) explicitly. entryExited.set(true); // Signal cancel and propagate the {@code BlockException}. cancel(); actual.onSubscribe(this); actual.onError(ex); } finally { if (sentinelContextConfig != null) { ContextUtil.exit(); } } }
Example 7
Source File: AsyncEntryDemo.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
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 8
Source File: SentinelZuulPreFilter.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
private void doSentinelEntry(String resourceName, final int resType, RequestContext requestContext, Deque<AsyncEntry> asyncEntries) throws BlockException { Object[] params = paramParser.parseParameterFor(resourceName, requestContext, new Predicate<GatewayFlowRule>() { @Override public boolean test(GatewayFlowRule r) { return r.getResourceMode() == resType; } }); AsyncEntry entry = SphU.asyncEntry(resourceName, EntryType.IN, 1, params); asyncEntries.push(entry); }
Example 9
Source File: AsyncEntryDemo.java From Sentinel with Apache License 2.0 | 5 votes |
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 10
Source File: SentinelZuulPreFilter.java From Sentinel with Apache License 2.0 | 5 votes |
private void doSentinelEntry(String resourceName, final int resType, RequestContext requestContext, Deque<EntryHolder> holders) throws BlockException { Object[] params = paramParser.parseParameterFor(resourceName, requestContext, new Predicate<GatewayFlowRule>() { @Override public boolean test(GatewayFlowRule r) { return r.getResourceMode() == resType; } }); AsyncEntry entry = SphU.asyncEntry(resourceName, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, params); EntryHolder holder = new EntryHolder(entry, params); holders.push(holder); }
Example 11
Source File: SentinelZuulInboundFilter.java From Sentinel with Apache License 2.0 | 4 votes |
private void doSentinelEntry(String resourceName, final int resType, HttpRequestMessage input, Deque<EntryHolder> holders) throws BlockException { Object[] params = paramParser.parseParameterFor(resourceName, input, r -> r.getResourceMode() == resType); AsyncEntry entry = SphU.asyncEntry(resourceName, ResourceTypeConstants.COMMON_API_GATEWAY, EntryType.IN, params); holders.push(new EntryHolder(entry, params)); }