Browse Source
eval is not thread safe. Added lock to avoid 'No such function invokeInternalXXX' exception (#4211)
* eval is not thread safe. Added lock to avoid 'No such function invokeInternalXXX' exception
* License fix
pull/4218/head
VoBa
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
12 additions and
4 deletions
-
application/src/main/java/org/thingsboard/server/service/script/AbstractNashornJsInvokeService.java
|
|
|
@ -40,6 +40,7 @@ import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeService { |
|
|
|
@ -56,6 +57,8 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer |
|
|
|
private final FutureCallback<UUID> evalCallback = new JsStatCallback<>(jsEvalMsgs, jsTimeoutMsgs, jsFailedMsgs); |
|
|
|
private final FutureCallback<Object> invokeCallback = new JsStatCallback<>(jsInvokeMsgs, jsTimeoutMsgs, jsFailedMsgs); |
|
|
|
|
|
|
|
private final ReentrantLock evalLock = new ReentrantLock(); |
|
|
|
|
|
|
|
@Getter |
|
|
|
private final JsExecutorService jsExecutor; |
|
|
|
|
|
|
|
@ -121,10 +124,15 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer |
|
|
|
jsPushedMsgs.incrementAndGet(); |
|
|
|
ListenableFuture<UUID> result = jsExecutor.executeAsync(() -> { |
|
|
|
try { |
|
|
|
if (useJsSandbox()) { |
|
|
|
sandbox.eval(jsScript); |
|
|
|
} else { |
|
|
|
engine.eval(jsScript); |
|
|
|
evalLock.lock(); |
|
|
|
try { |
|
|
|
if (useJsSandbox()) { |
|
|
|
sandbox.eval(jsScript); |
|
|
|
} else { |
|
|
|
engine.eval(jsScript); |
|
|
|
} |
|
|
|
} finally { |
|
|
|
evalLock.unlock(); |
|
|
|
} |
|
|
|
scriptIdToNameMap.put(scriptId, functionName); |
|
|
|
return scriptId; |
|
|
|
|