@RequestMapping("/link") @ResponseBody public String link() throws BlockException { Entry entry = SphU.entry("head1", EntryType.IN); Entry entry1 = SphU.entry("head2", EntryType.IN); Entry entry2 = SphU.entry("head3", EntryType.IN); Entry entry3 = SphU.entry("head4", EntryType.IN); entry3.exit(); entry2.exit(); entry1.exit(); entry.exit(); return "successfully create a call link"; }
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(); } }
@Override public void run() { while (true) { Entry entry = null; try { TimeUnit.MILLISECONDS.sleep(5); entry = SphU.entry(KEY); // token acquired pass.incrementAndGet(); // sleep 600 ms, as rt TimeUnit.MILLISECONDS.sleep(600); } catch (Exception e) { block.incrementAndGet(); } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } } }
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(); } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(resourceName); // token acquired, means pass pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); } catch (InterruptedException e) { // ignore } } } }
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(); } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(KEY); // token acquired, means pass pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(KEY); // token acquired, means pass pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(2000)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(KEY); pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (true) { Entry methodA = null; try { TimeUnit.MILLISECONDS.sleep(5); methodA = SphU.entry("methodA"); activeThread.incrementAndGet(); Entry methodB = SphU.entry("methodB"); TimeUnit.MILLISECONDS.sleep(methodBRunningTime); methodB.exit(); pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (methodA != null) { methodA.exit(); activeThread.decrementAndGet(); } } } } });
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(KEY); pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(KEY); // token acquired, means pass pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(resourceName); // token acquired, means pass pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (!stop) { Entry entry = null; try { entry = SphU.entry(KEY); // token acquired, means pass pass.addAndGet(1); } catch (BlockException e1) { block.incrementAndGet(); } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } Random random2 = new Random(); try { TimeUnit.MILLISECONDS.sleep(random2.nextInt(2000)); } catch (InterruptedException e) { // ignore } } } }
@Override public void run() { while (true) { Entry entry = null; try { entry = SphU.entry("methodA", EntryType.IN); pass.incrementAndGet(); try { TimeUnit.MILLISECONDS.sleep(20); } catch (InterruptedException e) { // ignore } } catch (BlockException e1) { block.incrementAndGet(); try { TimeUnit.MILLISECONDS.sleep(20); } catch (InterruptedException e) { // ignore } } catch (Exception e2) { // biz exception } finally { total.incrementAndGet(); if (entry != null) { entry.exit(); } } } }
private void doSomethingWithEntry() { Entry e0 = null; try { e0 = SphU.entry("benchmark"); doSomething(); } catch (BlockException e) { } finally { if (e0 != null) { e0.exit(); } } }
private void fetchSync() { Entry entry = null; try { entry = SphU.entry("test-sync"); } catch (BlockException ex) { ex.printStackTrace(); } finally { if (entry != null) { entry.exit(); } } }
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(); } } }
private static void testFor(/*@NonNull*/ String resource, /*@NonNull*/ String origin) { ContextUtil.enter(resource, origin); Entry entry = null; try { entry = SphU.entry(resource); System.out.println(String.format("Passed for resource %s, origin is %s", resource, origin)); } catch (BlockException ex) { System.err.println(String.format("Blocked for resource %s, origin is %s", resource, origin)); } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } }
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(); } } }