From 775484ba611ced5a02da01239df8064035650956 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Fri, 8 Mar 2019 09:57:49 +0800 Subject: [PATCH] Add try-with-resources support for Entry class (#550) - `Entry` class now implements `AutoCloseable` so it supports try-with-resources (`close` method adapts to `exit`) Signed-off-by: Eric Zhao --- .../java/com/alibaba/csp/sentinel/Entry.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Entry.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Entry.java index 1c1f0cf1..87400229 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Entry.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/Entry.java @@ -48,7 +48,7 @@ import com.alibaba.csp.sentinel.context.Context; * @see Context * @see ContextUtil */ -public abstract class Entry { +public abstract class Entry implements AutoCloseable { private static final Object[] OBJECTS0 = new Object[0]; @@ -70,6 +70,11 @@ public abstract class Entry { return resourceWrapper; } + /** + * Complete the current resource entry and restore the entry stack in context. + * + * @throws ErrorEntryFreeException if entry in current context does not match current entry + */ public void exit() throws ErrorEntryFreeException { exit(1, OBJECTS0); } @@ -78,11 +83,21 @@ public abstract class Entry { exit(count, OBJECTS0); } + /** + * Equivalent to {@link #exit()}. Support try-with-resources since JDK 1.7. + * + * @since 1.5.0 + */ + @Override + public void close() { + exit(); + } + /** * Exit this entry. This method should invoke if and only if once at the end of the resource protection. * * @param count tokens to release. - * @param args + * @param args extra parameters * @throws ErrorEntryFreeException, if {@link Context#getCurEntry()} is not this entry. */ public abstract void exit(int count, Object... args) throws ErrorEntryFreeException; @@ -91,7 +106,7 @@ public abstract class Entry { * Exit this entry. * * @param count tokens to release. - * @param args + * @param args extra parameters * @return next available entry after exit, that is the parent entry. * @throws ErrorEntryFreeException, if {@link Context#getCurEntry()} is not this entry. */