@Test public void testMutliScriptEngine() throws ExecutionException, InterruptedException { GrvyScriptEngineClient client = this.context.getBean(GrvyScriptEngineClient.class); List<String> scrpList = new ArrayList<>(); scrpList.add(" def list = [\"1101\",\"1411\",\"1121\",\"1131\"]\n" + " def channels = [\"NET\",\"NCUP\"]\n" + " if (list.contains(交易码) && channels.contains(渠道))\n" + " return true "); scrpList.add( " def list = [\"1101\",\"1411\",\"1121\",\"1131\"]\n" + " def channels = [\"unknow\",\"NCUP\"]\n" + " if (list.contains(交易码) && channels.contains(渠道))\n" + " return true "); scrpList.add( " def list = [\"1101\",\"1411\",\"1121\",\"1131\"]\n" + " def channels = [\"unknow\",\"xyz\"]\n" + " if (list.contains(交易码) && channels.contains(渠道))\n" + " return true "); scrpList.add( " def list = [\"1101\",\"1411\",\"1121\",\"1131\"]\n" + " def channels = [\"NET\",\"xyz\"]\n" + " if (list.contains(交易码) && channels.contains(渠道))\n" + " return true "); List<String> list = Arrays.asList("NET","unknow" ,"NCUP2","xyz3"); List<CompletableFuture> futures = new ArrayList<>(); for (int i = 0 ; i < 200 ; i++ ){ CompletableFuture future = CompletableFuture.runAsync( () -> { long start = System.currentTimeMillis(); Map<String,Object> map = Maps.newHashMap(); map.put("交易码","1101"); int idx = ThreadLocalRandom.current().nextInt(4); String param = list.get(idx); map.put("渠道",param); ScriptContext scriptContext = new SimpleScriptContext(); Bindings binding = new SimpleBindings(); binding.putAll(map); scriptContext.setBindings(binding,ScriptContext.ENGINE_SCOPE); String script = scrpList.get(idx); try { Object obj = client.eval(script,scriptContext); logger.info(" script:{} ; param:{} ; \n result: {} ; idx :{}; cost time:{}" ,script,map,obj,idx , System.currentTimeMillis() - start); } catch (ScriptException | ExecutionException e) { e.printStackTrace(); } }); futures.add(future); } CompletableFuture[] arr = futures.toArray(new CompletableFuture[0]); CompletableFuture future = CompletableFuture.allOf(arr); future.get(); logger.info("=========cpu core :{}==============",Runtime.getRuntime().availableProcessors()); }
public <T> T eval(String script, ScriptContext scripCtx) throws ExecutionException, ScriptException { GroovyCompiledScript compiledScript = grvyCompileScriptCache.get(script); Object result; synchronized (compiledScript) { result = compiledScript.eval(scripCtx); } return (T) result; }
GrvyScriptEngineClient { public <T> T eval(String script, ScriptContext scripCtx) throws ExecutionException, ScriptException { GroovyCompiledScript compiledScript = grvyCompileScriptCache.get(script); Object result; synchronized (compiledScript) { result = compiledScript.eval(scripCtx); } return (T) result; } }
GrvyScriptEngineClient { public <T> T eval(String script, ScriptContext scripCtx) throws ExecutionException, ScriptException { GroovyCompiledScript compiledScript = grvyCompileScriptCache.get(script); Object result; synchronized (compiledScript) { result = compiledScript.eval(scripCtx); } return (T) result; } GrvyScriptEngineClient();  GrvyScriptEngineClient(GroovyClassLoader classLoader); }
GrvyScriptEngineClient { public <T> T eval(String script, ScriptContext scripCtx) throws ExecutionException, ScriptException { GroovyCompiledScript compiledScript = grvyCompileScriptCache.get(script); Object result; synchronized (compiledScript) { result = compiledScript.eval(scripCtx); } return (T) result; } GrvyScriptEngineClient();  GrvyScriptEngineClient(GroovyClassLoader classLoader); T eval(String script, ScriptContext scripCtx); void flushLoadingCache(String key); void flushLoadingCache(List<String> keyList); }
GrvyScriptEngineClient { public <T> T eval(String script, ScriptContext scripCtx) throws ExecutionException, ScriptException { GroovyCompiledScript compiledScript = grvyCompileScriptCache.get(script); Object result; synchronized (compiledScript) { result = compiledScript.eval(scripCtx); } return (T) result; } GrvyScriptEngineClient();  GrvyScriptEngineClient(GroovyClassLoader classLoader); T eval(String script, ScriptContext scripCtx); void flushLoadingCache(String key); void flushLoadingCache(List<String> keyList);  }