2727 */
2828public class JJavaExecutionControl extends DirectExecutionControl {
2929
30- // generate a semi-unique thread name prefix for each JVM run for easier detection of
31- // JJavaExecutionControl-produced threads
30+ // generate a semi-unique thread name prefix for each JVM run for easier detection of JJavaExecutionControl-produced threads
3231 private static final String THREAD_NAME_PREFIX = "jjava-exec-"
3332 + ThreadLocalRandom .current ().ints (6 , 'a' , 'z' + 1 ).collect (StringBuilder ::new , StringBuilder ::appendCodePoint , StringBuilder ::append )
3433 + "-" ;
@@ -84,21 +83,6 @@ public TimeUnit getTimeoutUnit() {
8483 return timeoutUnit ;
8584 }
8685
87- /**
88- * This method was hijacked and actually only returns a key that can be later retrieved via
89- * {@link #takeResult(String)}. This should be called for every invocation as the objects are saved and not taking
90- * them will leak the memory.
91- *
92- * @returns the key to use for {@link #takeResult(String) looking up the result}.
93- */
94- @ Override
95- protected String invoke (Method doitMethod ) throws Exception {
96- String id = UUID .randomUUID ().toString ();
97- Object value = execute (id , doitMethod );
98- results .put (id , value );
99- return id ;
100- }
101-
10286 @ Override
10387 public void stop () {
10488 executor .shutdownNow ();
@@ -121,26 +105,32 @@ public void interrupt() {
121105 running .forEach ((id , f ) -> f .cancel (true ));
122106 }
123107
108+ /**
109+ * This method was hijacked and actually only returns a key that can be later retrieved via
110+ * {@link #takeResult(String)}. This should be called for every invocation as the objects are saved and not taking
111+ * them will leak the memory.
112+ *
113+ * @returns the key to use for {@link #takeResult(String) looking up the result}.
114+ */
124115 @ Override
125- public String toString () {
126- return "JJavaExecutionControl{" +
127- "timeoutTime=" + timeoutDuration +
128- ", timeoutUnit=" + timeoutUnit +
129- '}' ;
116+ protected String invoke ( Method doitMethod ) throws Exception {
117+ String id = UUID . randomUUID (). toString ();
118+ Object value = doInvoke ( id , doitMethod );
119+ results . put ( id , value );
120+ return id ;
130121 }
131122
132- private Object execute (String id , Method doitMethod ) throws Exception {
123+ private Object doInvoke (String id , Method doitMethod ) throws Exception {
133124
134- // run on the same thread if an execution was called within another execution
135- boolean alreadyRunByKernel = Thread .currentThread ().getName ().startsWith (THREAD_NAME_PREFIX );
136- Future <Object > runningTask = alreadyRunByKernel
125+ Future <Object > task = isNestedCall ()
126+ // run on the same thread if the invocation is done within another invocation
137127 ? CompletableFuture .completedFuture (doitMethod .invoke (null ))
138128 : executor .submit (() -> doitMethod .invoke (null ));
139129
140- running .put (id , runningTask );
130+ running .put (id , task );
141131
142132 try {
143- return timeoutDuration > 0 ? runningTask .get (this . timeoutDuration , this . timeoutUnit ) : runningTask .get ();
133+ return timeoutDuration > 0 ? task .get (timeoutDuration , timeoutUnit ) : task .get ();
144134 } catch (CancellationException e ) {
145135 // If canceled this means that stop() or interrupt() was invoked.
146136 if (executor .isShutdown ()) {
@@ -166,11 +156,23 @@ private Object execute(String id, Method doitMethod) throws Exception {
166156 }
167157 } catch (TimeoutException e ) {
168158 String message = String .format ("Execution timed out after configured timeout of %d %s." ,
169- this . timeoutDuration ,
170- this . timeoutUnit .toString ().toLowerCase ());
159+ timeoutDuration ,
160+ timeoutUnit .toString ().toLowerCase ());
171161 throw new UserException (message , EXECUTION_TIMEOUT_NAME , e .getStackTrace ());
172162 } finally {
173- running .remove (id , runningTask );
163+ running .remove (id , task );
174164 }
175165 }
166+
167+ private boolean isNestedCall () {
168+ return Thread .currentThread ().getName ().startsWith (THREAD_NAME_PREFIX );
169+ }
170+
171+ @ Override
172+ public String toString () {
173+ return "JJavaExecutionControl{" +
174+ "timeoutTime=" + timeoutDuration +
175+ ", timeoutUnit=" + timeoutUnit +
176+ '}' ;
177+ }
176178}
0 commit comments