Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/example-app/example-app-cmd-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-app</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-app/example-app-cmd-meta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-app</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-app/example-app-query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-app</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/example-domain-leave/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-domain</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/example-domain-user/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-domain</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/example-infra-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-infra</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/example-infra-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-infra</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/example-infra-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>example-infra</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion example/example-interface/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-example</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
</parent>

<artifactId>example-interface</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion example/example-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.4.43</version>
<version>3.4.44</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</parent>

<artifactId>springboot-example</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>

<name>springboot-example</name>
<description>springboot-example project for Spring Boot</description>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-authorization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.4.43</version>
<version>3.4.44</version>
</parent>

<name>springboot-starter-data-authorization</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-fast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.4.43</version>
<version>3.4.44</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.4.43</version>
<version>3.4.44</version>
</parent>

<name>springboot-starter-flow</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public MessageResult run(FlowRecord flowRecord,
if (groovy != null) {
//执行脚本
FlowSession session = new FlowSession(flowRecord, flowWork, flowNode, createOperator, currentOperator, bindData, opinion, historyRecords);
GroovyShellContext.ShellScript script = GroovyShellContext.getInstance().parse(groovy);
return (MessageResult) script.invokeMethod("run", session);
return GroovyShellContext.getInstance().run(groovy,MessageResult.class,session);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ public class ErrTrigger {
@Getter
private final String script;

private final GroovyShellContext.ShellScript runtime;


public ErrTrigger(String script) {
if (!StringUtils.hasLength(script)) {
throw new IllegalArgumentException("script is empty");
}
this.script = script;
this.runtime = GroovyShellContext.getInstance().parse(script);
}

/**
Expand All @@ -30,7 +26,7 @@ public ErrTrigger(String script) {
* @param flowSession 流程内容
*/
public ErrorResult trigger(FlowSession flowSession) {
return (ErrorResult) runtime.invokeMethod("run", flowSession);
return GroovyShellContext.getInstance().run(script, ErrorResult.class, flowSession);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ public class TitleGenerator {
@Getter
private final String script;

private final GroovyShellContext.ShellScript runtime;

public TitleGenerator(String script) {
if (!StringUtils.hasLength(script)) {
throw new IllegalArgumentException("script is empty");
}
this.script = script;
this.runtime = GroovyShellContext.getInstance().parse(script);
}


Expand All @@ -41,7 +39,7 @@ public static TitleGenerator defaultTitleGenerator() {
* @return 标题
*/
public String generate(FlowSession flowSession) {
return (String) this.runtime.invokeMethod("run", flowSession);
return GroovyShellContext.getInstance().run(script,String.class,flowSession);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class OperatorMatcher {

private final int state;

private final GroovyShellContext.ShellScript runtime;

// 指定用户
public static final int STATE_SPECIFY = 1;
// 创建者
Expand Down Expand Up @@ -63,7 +61,6 @@ public OperatorMatcher(String script, int state) {
}
this.script = script;
this.state = state;
this.runtime = GroovyShellContext.getInstance().parse(script);
}

/**
Expand Down Expand Up @@ -114,7 +111,7 @@ public static OperatorMatcher creatorOperatorMatcher() {
* @return 是否匹配
*/
public List<Long> matcher(FlowSession flowSession) {
List<Object> values = (List<Object>) runtime.invokeMethod("run", flowSession);
List<Object> values = (List<Object>) GroovyShellContext.getInstance().run(script,List.class, flowSession);
if (values == null) {
return new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,35 @@
package com.codingapi.springboot.flow.script;

import com.codingapi.springboot.flow.utils.Sha256Utils;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import com.codingapi.springboot.framework.script.GroovyScriptRunningContext;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GroovyShellContext {

@Getter
private final static GroovyShellContext instance = new GroovyShellContext();

private final static GroovyShell groovyShell = new GroovyShell();

private final Map<String, ShellScript> cache = new HashMap<>();

private final static ExecutorService threadPool = Executors.newFixedThreadPool(10);

// 缓存最大值
private final static int MAX_CACHE_SIZE = 10000;

@Getter
private final static GroovyShellContext instance = new GroovyShellContext();

private GroovyShellContext() {
}

public ShellScript parse(String script) {
String hash = Sha256Utils.generateSHA256(script);
if (cache.containsKey(hash)) {
return cache.get(hash);
} else {
if (cache.size() > MAX_CACHE_SIZE) {
cache.clear();
}
ShellScript shellScript = new ShellScript(script);
threadPool.submit(shellScript);
cache.put(hash, shellScript);
return shellScript;
private GroovyShellContext() {
if(GroovyScriptRunningContext.getInstance().getMaxCacheSize()!=MAX_CACHE_SIZE) {
GroovyScriptRunningContext.getInstance().setMaxCacheSize(MAX_CACHE_SIZE);
}
}


public int size() {
return cache.size();
public <T> T run(String script, Class<T> returnType, Object... args) {
return GroovyScriptRunningContext.getInstance().run("run", script, returnType, args);
}

public static class ShellScript implements Runnable {

@Getter
private final String script;

private Script runtime;

public ShellScript(String script) {
this.script = script;
}

public Object invokeMethod(String run, Object params) {
synchronized (ShellScript.this) {
if (runtime == null) {
try {
ShellScript.this.wait(1000);
} catch (InterruptedException ignored) {
}
if (runtime == null) {
runtime = groovyShell.parse(script);
}
}
return runtime.invokeMethod(run, params);
}
}

@Override
public void run() {
runtime = groovyShell.parse(script);
synchronized (ShellScript.this) {
this.notifyAll();
}
}
public int size(){
return GroovyScriptRunningContext.getInstance().cacheSize();
}

public void clear(){
GroovyScriptRunningContext.getInstance().clearCache();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ public class OutTrigger {
@Getter
private final String script;

private final GroovyShellContext.ShellScript runtime;

public OutTrigger(String script) {
if (!StringUtils.hasLength(script)) {
throw new IllegalArgumentException("script is empty");
}
this.script = script;
this.runtime = GroovyShellContext.getInstance().parse(script);
}

/**
Expand All @@ -38,7 +36,7 @@ public static OutTrigger defaultOutTrigger() {
* @return true 进入下一节点,false 则返回上一节点
*/
public boolean trigger(FlowSession flowSession) {
return (Boolean) runtime.invokeMethod("run", flowSession);
return GroovyShellContext.getInstance().run(script, Boolean.class, flowSession);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ class GroovyShellContextTest {
void getInstance() {
long t1 = System.currentTimeMillis();
int count = 12000;
GroovyShellContext.ShellScript[] scripts = new GroovyShellContext.ShellScript[count];
for (int i = 0; i < count; i++) {
scripts[i] = GroovyShellContext.getInstance().parse("def run(content){ return '" + i + "';}");
String result = GroovyShellContext.getInstance().run("def run(content){ return '" + i + "';}",String.class,i);
assertEquals(result, String.valueOf(i));
}

long t2 = System.currentTimeMillis();
System.out.println("t2 time :" + (t2 - t1));
System.out.println("time :" + (t2 - t1));
System.out.println("size:" + GroovyShellContext.getInstance().size());

for (int i = 0; i < count; i++) {
assertEquals(scripts[i].invokeMethod("run", i), String.valueOf(i));
}

long t3 = System.currentTimeMillis();
System.out.println("t3 time :" + (t3 - t2));

}
}
Loading