@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"; }
@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(); } } } }
@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); 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(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); // 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 (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(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 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 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 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(); } } }
@Override public void run() { int count = 0; while (true) { count++; Entry entry = null; try { Thread.sleep(20); entry = SphU.entry(KEY); // token acquired, means pass pass.addAndGet(1); if (count % 2 == 0) { // biz code raise an exception. throw new RuntimeException("throw runtime "); } } catch (BlockException e) { block.addAndGet(1); } catch (Throwable t) { bizException.incrementAndGet(); Tracer.trace(t); } finally { total.addAndGet(1); if (entry != null) { entry.exit(); } } } }
@Override public void run() { int count = 0; while (true) { count++; Entry entry = null; try { Thread.sleep(20); entry = SphU.entry(KEY); // token acquired, means pass pass.addAndGet(1); if (count % 2 == 0) { // biz code raise an exception. throw new RuntimeException("throw runtime "); } } catch (BlockException e) { block.addAndGet(1); } catch (Throwable t) { bizException.incrementAndGet(); Tracer.trace(t); } finally { total.addAndGet(1); if (entry != null) { entry.exit(); } } } }
private static void doSomething(MessageExt message) { pool.submit(() -> { Entry entry = null; try { ContextUtil.enter(KEY); entry = SphU.entry(KEY, EntryType.OUT); // Your business logic here. System.out.printf("[%d][%s][Success: %d] Receive New Messages: %s %n", System.currentTimeMillis(), Thread.currentThread().getName(), SUCCESS_COUNT.addAndGet(1), new String(message.getBody())); } catch (BlockException ex) { // Blocked. System.out.println("Blocked: " + FAIL_COUNT.addAndGet(1)); } finally { if (entry != null) { entry.exit(); } ContextUtil.exit(); } }); }
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(); } } } }
@Around("sentinelResourceAnnotationPointcut()") public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable { Method originMethod = resolveMethod(pjp); SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class); if (annotation == null) { // Should not go through here. throw new IllegalStateException("Wrong state for SentinelResource annotation"); } String resourceName = getResourceName(annotation.value(), originMethod); EntryType entryType = annotation.entryType(); Entry entry = null; try { entry = SphU.entry(resourceName, entryType, 1, pjp.getArgs()); Object result = pjp.proceed(); return result; } catch (BlockException ex) { return handleBlockException(pjp, annotation, ex); } catch (Throwable ex) { Tracer.trace(ex); throw ex; } finally { if (entry != null) { entry.exit(); } } } }