Quellcode durchsuchen

优化动态脚本

zhouhao vor 7 Jahren
Ursprung
Commit
1f8bc59251
11 geänderte Dateien mit 319 neuen und 86 gelöschten Zeilen
  1. 91 83
      hsweb-system/hsweb-system-script/hsweb-system-script-entity/src/main/java/org/hswebframework/web/entity/script/ScriptEntity.java
  2. 0 1
      hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-api/src/main/java/org/hswebframework/web/service/script/ScriptExecutorService.java
  3. 6 0
      hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/pom.xml
  4. 7 2
      hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/DefaultScriptExecutorService.java
  5. 139 0
      hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/MessagerScriptExecutorService.java
  6. 25 0
      hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/ScriptExecutorMessage.java
  7. 17 0
      hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/ScriptExecutorResultMessage.java
  8. 7 0
      hsweb-system/hsweb-system-script/hsweb-system-script-starter/pom.xml
  9. 23 0
      hsweb-system/hsweb-system-script/hsweb-system-script-starter/src/main/java/org/hswebframework/web/service/dynamic/script/DynamicScriptAutoConfiguration.java
  10. 3 0
      hsweb-system/hsweb-system-script/hsweb-system-script-starter/src/main/resources/META-INF/spring.factories
  11. 1 0
      hsweb-system/hsweb-system-script/hsweb-system-script-starter/src/test/java/org/hswebframework/web/workflow/flowable/ControllerTest.java

+ 91 - 83
hsweb-system/hsweb-system-script/hsweb-system-script-entity/src/main/java/org/hswebframework/web/entity/script/ScriptEntity.java

@@ -1,105 +1,113 @@
 package org.hswebframework.web.entity.script;
+
 import org.hswebframework.web.commons.entity.GenericEntity;
 
 /**
-* 动态脚本 实体
-* @author hsweb-generator-online
-*/
-public interface ScriptEntity extends GenericEntity<String>{
+ * 动态脚本 实体
+ *
+ * @author hsweb-generator-online
+ */
+public interface ScriptEntity extends GenericEntity<String> {
  /*-------------------------------------------
     |               属性名常量               |
     ===========================================*/
-     /**
+    /**
      * 脚本名称
      */
-     String name="name";
-     /**
+    String name     = "name";
+    /**
      * 类型
      */
-     String type="type";
-     /**
+    String type     = "type";
+    /**
      * 脚本内容
      */
-     String script="script";
-     /**
+    String script   = "script";
+    /**
      * 脚本语言
      */
-     String language="language";
-     /**
+    String language = "language";
+    /**
      * 备注
      */
-     String remark="remark";
-     /**
+    String remark   = "remark";
+    /**
      * 状态
      */
-     String status="status";
-     /**
+    String status   = "status";
+    /**
      * 脚本标签
      */
-     String tag="tag";
-    
-        /**
-        * @return 脚本名称
-        */
-        String getName();
-
-        /**
-        * @param  name  脚本名称
-        */
-        void setName(String name);
-        /**
-        * @return 类型
-        */
-        String getType();
-
-        /**
-        * @param  type  类型
-        */
-        void setType(String type);
-        /**
-        * @return 脚本内容
-        */
-        String getScript();
-
-        /**
-        * @param  script  脚本内容
-        */
-        void setScript(String script);
-        /**
-        * @return 脚本语言
-        */
-        String getLanguage();
-
-        /**
-        * @param  language  脚本语言
-        */
-        void setLanguage(String language);
-        /**
-        * @return 备注
-        */
-        String getRemark();
-
-        /**
-        * @param  remark  备注
-        */
-        void setRemark(String remark);
-        /**
-        * @return 状态
-        */
-        Long getStatus();
-
-        /**
-        * @param  status  状态
-        */
-        void setStatus(Long status);
-        /**
-        * @return 脚本标签
-        */
-        String getTag();
-
-        /**
-        * @param  tag  脚本标签
-        */
-        void setTag(String tag);
-      
+    String tag      = "tag";
+
+    /**
+     * @return 脚本名称
+     */
+    String getName();
+
+    /**
+     * @param name 脚本名称
+     */
+    void setName(String name);
+
+    /**
+     * @return 类型
+     */
+    String getType();
+
+    /**
+     * @param type 类型
+     */
+    void setType(String type);
+
+    /**
+     * @return 脚本内容
+     */
+    String getScript();
+
+    /**
+     * @param script 脚本内容
+     */
+    void setScript(String script);
+
+    /**
+     * @return 脚本语言
+     */
+    String getLanguage();
+
+    /**
+     * @param language 脚本语言
+     */
+    void setLanguage(String language);
+
+    /**
+     * @return 备注
+     */
+    String getRemark();
+
+    /**
+     * @param remark 备注
+     */
+    void setRemark(String remark);
+
+    /**
+     * @return 状态
+     */
+    Long getStatus();
+
+    /**
+     * @param status 状态
+     */
+    void setStatus(Long status);
+
+    /**
+     * @return 脚本标签
+     */
+    String getTag();
+
+    /**
+     * @param tag 脚本标签
+     */
+    void setTag(String tag);
+
 }

+ 0 - 1
hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-api/src/main/java/org/hswebframework/web/service/script/ScriptExecutorService.java

@@ -3,7 +3,6 @@ package org.hswebframework.web.service.script;
 import java.util.Map;
 
 /**
- * TODO 完成注释
  *
  * @author zhouhao
  */

+ 6 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/pom.xml

@@ -22,5 +22,11 @@
             <artifactId>hsweb-system-script-service-api</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-message-api</artifactId>
+            <version>${project.version}</version>
+            <optional>true</optional>
+        </dependency>
     </dependencies>
 </project>

+ 7 - 2
hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/DefaultScriptExecutorService.java

@@ -16,17 +16,22 @@ import java.util.Map;
 /**
  * @author zhouhao
  */
-@Service("scriptExecutorService")
 public class DefaultScriptExecutorService implements ScriptExecutorService {
 
     @Autowired
     private ScriptService scriptService;
 
+    public void setScriptService(ScriptService scriptService) {
+        this.scriptService = scriptService;
+    }
+
     @Override
     @Transactional(rollbackFor = Throwable.class)
     public Object execute(String id, Map<String, Object> parameters) throws Exception {
         ScriptEntity scriptEntity = scriptService.selectByPk(id);
-
+        if (scriptEntity==null){
+            return null;
+        }
         DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(scriptEntity.getLanguage());
 
         String scriptId = "dynamicScript-" + id;

+ 139 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/MessagerScriptExecutorService.java

@@ -0,0 +1,139 @@
+package org.hswebframework.web.service.script.simple;
+
+import lombok.extern.slf4j.Slf4j;
+import org.hswebframework.web.entity.script.ScriptEntity;
+import org.hswebframework.web.id.IDGenerator;
+import org.hswebframework.web.message.MessageSubscribe;
+import org.hswebframework.web.message.Messager;
+import org.hswebframework.web.message.builder.StaticMessageSubjectBuilder;
+import org.hswebframework.web.service.script.ScriptExecutorService;
+import org.hswebframework.web.service.script.ScriptService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
+
+import javax.annotation.PostConstruct;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.hswebframework.web.message.builder.StaticMessageSubjectBuilder.*;
+import static org.springframework.beans.factory.wiring.BeanWiringInfo.AUTOWIRE_BY_TYPE;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@Slf4j
+public class MessagerScriptExecutorService implements ScriptExecutorService {
+
+    @Autowired
+    private Messager messager;
+
+    private String tag;
+
+    private String all = "all";
+
+    private ScriptExecutorService defaultScriptExecutorService;
+
+    @Autowired
+    private ScriptService scriptService;
+
+    @Autowired
+    private ApplicationContext applicationContext;
+
+    @Autowired
+    public void setScriptService(ScriptService scriptService) {
+        this.scriptService = scriptService;
+    }
+
+    public void setTag(String tag) {
+        this.tag = tag;
+    }
+
+    private void initSubscribe(String topic) {
+        messager.subscribe(queue(topic))
+                .onMessage(message -> {
+                    ScriptExecutorMessage msg = ((ScriptExecutorMessage) message);
+                    Object result;
+                    try {
+                        result = execute(msg.getScriptId(), msg.getParameter());
+
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                        log.error("execute script {} error", msg, e);
+                        result = e.getMessage();
+                    }
+                    messager.publish(ScriptExecutorResultMessage.builder().result(result).build())
+                            .to(queue(topic + msg.getCallback()))
+                            .send();
+                });
+    }
+
+    @PostConstruct
+    public void subscribe() {
+        defaultScriptExecutorService = (DefaultScriptExecutorService) applicationContext.getAutowireCapableBeanFactory()
+                .autowire(DefaultScriptExecutorService.class, AUTOWIRE_BY_TYPE, false);
+
+        if (tag != null) {
+            String[] tags = tag.split(",");
+            for (String tag : tags) {
+                initSubscribe(tag);
+            }
+        } else {
+            initSubscribe(all);
+        }
+    }
+
+    public Object doExecute(String id, Map<String, Object> parameters) throws Exception {
+        return defaultScriptExecutorService.execute(id, parameters);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class)
+    public Object execute(String id, Map<String, Object> parameters) throws Exception {
+        ScriptEntity scriptEntity = scriptService.selectByPk(id);
+        if (scriptEntity == null) {
+            return null;
+        }
+        String configTag = scriptEntity.getTag();
+        String callBack = IDGenerator.MD5.generate();
+
+        String topic;
+
+        if (StringUtils.isEmpty(configTag)) {
+            topic = all;
+        } else {
+            String[] tags = configTag.split(",");
+            topic = tags[new Random().nextInt(tags.length)];
+        }
+        Object[] result = new Object[1];
+        CountDownLatch latch = new CountDownLatch(1);
+
+        MessageSubscribe<ScriptExecutorResultMessage> subscribe =
+                messager.<ScriptExecutorResultMessage>subscribe(queue(topic + callBack))
+                        .onMessage(msg -> {
+                            result[0] = msg.getResult();
+                            latch.countDown();
+                        });
+
+        messager.publish(ScriptExecutorMessage.builder()
+                .callback(callBack)
+                .parameter(parameters)
+                .scriptId(id)
+                .build())
+                .to(queue(topic)).send();
+
+        boolean success = latch.await(30, TimeUnit.SECONDS);
+        if (!success) {
+            log.error("await script execute error");
+        }
+        subscribe.cancel();
+
+        return result[1];
+    }
+
+
+}

+ 25 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/ScriptExecutorMessage.java

@@ -0,0 +1,25 @@
+package org.hswebframework.web.service.script.simple;
+
+import lombok.*;
+import org.hswebframework.web.message.Message;
+
+import java.util.Map;
+
+/**
+ *
+ * @author zhouhao
+ * @since 3.0
+ */
+@Getter
+@Setter
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+@ToString
+public class ScriptExecutorMessage implements Message{
+    private String scriptId;
+
+    private Map<String,Object> parameter;
+
+    private String callback;
+}

+ 17 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-service/hsweb-system-script-service-simple/src/main/java/org/hswebframework/web/service/script/simple/ScriptExecutorResultMessage.java

@@ -0,0 +1,17 @@
+package org.hswebframework.web.service.script.simple;
+
+import lombok.*;
+import org.hswebframework.web.message.Message;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@Getter
+@Setter
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class ScriptExecutorResultMessage implements Message{
+    private Object result;
+}

+ 7 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-starter/pom.xml

@@ -73,6 +73,13 @@
             <version>2.5</version>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-message-api</artifactId>
+            <version>${project.version}</version>
+            <optional>true</optional>
+        </dependency>
       
     </dependencies>
 </project>

+ 23 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-starter/src/main/java/org/hswebframework/web/service/dynamic/script/DynamicScriptAutoConfiguration.java

@@ -0,0 +1,23 @@
+package org.hswebframework.web.service.dynamic.script;
+
+import org.hswebframework.web.service.script.simple.DefaultScriptExecutorService;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author zhouhao
+ * @since 3.0
+ */
+@Configuration
+@ComponentScan({
+        "org.hswebframework.web.service.script",
+        "org.hswebframework.web.controller.script"
+})
+public class DynamicScriptAutoConfiguration {
+
+    @Bean
+    public DefaultScriptExecutorService defaultScriptExecutorService() {
+        return new DefaultScriptExecutorService();
+    }
+}

+ 3 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-starter/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,3 @@
+# Auto Configure
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.hswebframework.web.service.dynamic.script.DynamicScriptAutoConfiguration

+ 1 - 0
hsweb-system/hsweb-system-script/hsweb-system-script-starter/src/test/java/org/hswebframework/web/workflow/flowable/ControllerTest.java

@@ -30,4 +30,5 @@ public class ControllerTest extends SimpleWebApplicationTests {
         Assert.assertEquals(jsonObject.get("result"), "success");
     }
 
+
 }