xushili 11 mesiacov pred
rodič
commit
17c7d673b8

+ 26 - 9
ruoyi-admin/src/main/java/com/ruoyi/web/runner/LoadDataSourceRunner.java

@@ -3,9 +3,13 @@ package com.ruoyi.web.runner;
 import cn.hutool.core.bean.BeanUtil;
 import com.alibaba.druid.pool.DruidDataSource;
 import com.ruoyi.framework.datasource.DynamicDataSource;
+import com.ruoyi.ucp.entity.Formula;
 import com.ruoyi.ucp.entity.Method;
 import com.ruoyi.ucp.entity.UcpDataSource;
 import com.ruoyi.ucp.feign.AdapterApi;
+import com.ruoyi.ucp.glue.GlueFactory;
+import com.ruoyi.ucp.handler.IJobHandler;
+import com.ruoyi.ucp.handler.impl.MyJobHandler;
 import com.ruoyi.ucp.mapper.TurbineInfoMapper;
 import com.ruoyi.ucp.service.*;
 import org.springframework.boot.CommandLineRunner;
@@ -23,8 +27,8 @@ public class LoadDataSourceRunner implements CommandLineRunner {
     @Resource
     private IUcpDataSourceService ucpDataSourceService;
 
-    @Resource
-    private ScriptEngine engine;
+    //@Resource
+    //private ScriptEngine engine;
     @Resource
     private AdapterApi adapter;
     @Resource
@@ -37,6 +41,10 @@ public class LoadDataSourceRunner implements CommandLineRunner {
     private IMethodService methodService;
     @Resource
     private ILineInfoDayService lineInfoDayService;
+    @Resource
+    private IFormulaService formulaService;
+    @Resource
+    private MyJobHandler myJobHandler;
 
     @Override
     public void run(String... args) {
@@ -44,18 +52,27 @@ public class LoadDataSourceRunner implements CommandLineRunner {
         List<DruidDataSource> dataSources = BeanUtil.copyToList(list, DruidDataSource.class);
         dynamicDataSource.createDataSource(dataSources);
 
-        engine.put("adapter", adapter);
-        engine.put("pointService", pointService);
-        engine.put("stationInfoDayService", stationInfoDayService);
-        engine.put("stationInfoHourService", stationInfoHourService);
-        engine.put("lineInfoDayService", lineInfoDayService);
+        //engine.put("adapter", adapter);
+        //engine.put("pointService", pointService);
+        //engine.put("stationInfoDayService", stationInfoDayService);
+        //engine.put("stationInfoHourService", stationInfoHourService);
+        //engine.put("lineInfoDayService", lineInfoDayService);
 
         List<Method> methods = methodService.list();
+        List<Formula> formulas = formulaService.list();
+        String functionCode = "";
         try {
             for (Method method : methods) {
-                engine.eval(method.getMethodExpression());
+                //engine.eval(method.getMethodExpression());
+                functionCode = GlueFactory.getFunctionTemplate()
+                        .replace("//此处加公式", method.getMethodExpression()+"\n//此处加公式");
+            }
+            for (Formula formula : formulas) {
+                functionCode = functionCode.replace("//此处加公式", formula.getFormulaExpression()+"\n//此处加公式");
             }
-        } catch (ScriptException e) {
+            IJobHandler jobHandler = GlueFactory.getInstance().loadNewInstance(functionCode);
+            myJobHandler.setJobHandler(jobHandler);
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }

+ 7 - 0
universal-computing-platform/pom.xml

@@ -80,6 +80,13 @@
             <artifactId>druid-spring-boot-starter</artifactId>
         </dependency>
 
+        <!-- groovy-all -->
+        <dependency>
+            <groupId>org.apache.groovy</groupId>
+            <artifactId>groovy</artifactId>
+            <version>4.0.18</version>
+        </dependency>
+
         <!--<dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-boot-starter</artifactId>

+ 1 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/config/ScriptConfig.java

@@ -10,6 +10,7 @@ import javax.script.ScriptEngineManager;
 public class ScriptConfig {
     @Bean
     public ScriptEngine scriptEngine(){
+
         return new ScriptEngineManager().getEngineByName("nashorn");
     }
 }

+ 151 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/glue/GlueFactory.java

@@ -0,0 +1,151 @@
+package com.ruoyi.ucp.glue;
+
+import com.ruoyi.ucp.glue.impl.SpringGlueFactory;
+import com.ruoyi.ucp.handler.IJobHandler;
+import groovy.lang.GroovyClassLoader;
+
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * glue factory, product class/object by name
+ *
+ * @author xuxueli 2016-1-2 20:02:27
+ */
+public class GlueFactory {
+
+	private static GlueFactory glueFactory = new SpringGlueFactory();
+
+	public static GlueFactory getInstance(){
+		return glueFactory;
+	}
+
+	public static String getJavaGlue(){
+		String JavaGlue = "package com.ruoyi.ucp;\n" +
+				"\n" +
+				"import com.ruoyi.ucp.feign.AdapterApi;\n" +
+				"import com.ruoyi.ucp.handler.IJobHandler;\n" +
+				"import com.ruoyi.ucp.service.ILineInfoDayService;\n" +
+				"import com.ruoyi.ucp.service.IPointInfoService;\n" +
+				"import com.ruoyi.ucp.service.IStationInfoDayService;\n" +
+				"import com.ruoyi.ucp.service.IStationInfoHourService;\n" +
+				"\n" +
+				"import javax.annotation.Resource;\n" +
+				"\n" +
+				"public class JavaGlueJobHandler extends IJobHandler{\n" +
+				"\n" +
+				"    @Resource\n" +
+				"    private AdapterApi adapter;\n" +
+				"    @Resource\n" +
+				"    private IPointInfoService pointService;\n" +
+				"    @Resource\n" +
+				"    private IStationInfoHourService stationInfoHourService;\n" +
+				"    @Resource\n" +
+				"    private IStationInfoDayService stationInfoDayService;\n" +
+				"    @Resource\n" +
+				"    private ILineInfoDayService lineInfoDayService;\n" +
+				"\n" +
+				"    private IJobHandler jobHandler;\n" +
+				"\n" +
+				"    @Override\n" +
+				"    public void execute() throws Exception {\n" +
+				"        //此处运行方法\n" +
+				"    }\n" +
+				"\n" +
+				"    @Override\n" +
+				"    public IJobHandler getFunctionHandler() {\n" +
+				"        return jobHandler;\n" +
+				"    }\n" +
+				"\n" +
+				"    @Override\n" +
+				"    public void setFunctionHandler(IJobHandler jobHandler) {\n" +
+				"        this.jobHandler = jobHandler;\n" +
+				"    }\n" +
+				"}\n";
+		return JavaGlue;
+	}
+
+	public static String getFunctionTemplate(){
+		String FunctionTemplate = "package com.ruoyi.ucp;\n" +
+				"\n" +
+				"import com.ruoyi.ucp.handler.IJobHandler;\n" +
+				"\n" +
+				"public class JavaFunctionJobHandler  extends IJobHandler {\n" +
+				"    @Override\n" +
+				"    public void execute() throws Exception {\n" +
+				"\n" +
+				"    }\n" +
+				"    //此处加公式\n" +
+				"}\n";
+		return FunctionTemplate;
+	}
+
+	public static void refreshInstance(int type){
+		if (type == 0) {
+			glueFactory = new GlueFactory();
+		} else if (type == 1) {
+			glueFactory = new SpringGlueFactory();
+		}
+	}
+
+
+	/**
+	 * groovy class loader
+	 */
+	private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
+	private ConcurrentMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
+
+	/**
+	 * load new instance, prototype
+	 *
+	 * @param codeSource
+	 * @return
+	 * @throws Exception
+	 */
+	public IJobHandler loadNewInstance(String codeSource) throws Exception{
+		if (codeSource!=null && codeSource.trim().length()>0) {
+			Class<?> clazz = getCodeSourceClass(codeSource);
+			if (clazz != null) {
+				Object instance = clazz.newInstance();
+				if (instance!=null) {
+					if (instance instanceof IJobHandler) {
+						this.injectService(instance);
+						return (IJobHandler) instance;
+					} else {
+						throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
+								+ "cannot convert from instance["+ instance.getClass() +"] to IJobHandler");
+					}
+				}
+			}
+		}
+		throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
+	}
+	private Class<?> getCodeSourceClass(String codeSource){
+		try {
+			// md5
+			byte[] md5 = MessageDigest.getInstance("MD5").digest(codeSource.getBytes());
+			String md5Str = new BigInteger(1, md5).toString(16);
+
+			Class<?> clazz = CLASS_CACHE.get(md5Str);
+			if(clazz == null){
+				clazz = groovyClassLoader.parseClass(codeSource);
+				CLASS_CACHE.putIfAbsent(md5Str, clazz);
+			}
+			return clazz;
+		} catch (Exception e) {
+			return groovyClassLoader.parseClass(codeSource);
+		}
+	}
+
+	/**
+	 * inject service of bean field
+	 *
+	 * @param instance
+	 */
+	public void injectService(Object instance) {
+		// do something
+	}
+
+}

+ 53 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/glue/GlueTypeEnum.java

@@ -0,0 +1,53 @@
+package com.ruoyi.ucp.glue;
+
+/**
+ * Created by xuxueli on 17/4/26.
+ */
+public enum GlueTypeEnum {
+
+    BEAN("BEAN", false, null, null),
+    GLUE_GROOVY("GLUE(Java)", false, null, null),
+    GLUE_SHELL("GLUE(Shell)", true, "bash", ".sh"),
+    GLUE_PYTHON("GLUE(Python)", true, "python", ".py"),
+    GLUE_PHP("GLUE(PHP)", true, "php", ".php"),
+    GLUE_NODEJS("GLUE(Nodejs)", true, "node", ".js"),
+    GLUE_POWERSHELL("GLUE(PowerShell)", true, "powershell", ".ps1");
+
+    private String desc;
+    private boolean isScript;
+    private String cmd;
+    private String suffix;
+
+    private GlueTypeEnum(String desc, boolean isScript, String cmd, String suffix) {
+        this.desc = desc;
+        this.isScript = isScript;
+        this.cmd = cmd;
+        this.suffix = suffix;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public boolean isScript() {
+        return isScript;
+    }
+
+    public String getCmd() {
+        return cmd;
+    }
+
+    public String getSuffix() {
+        return suffix;
+    }
+
+    public static GlueTypeEnum match(String name){
+        for (GlueTypeEnum item: GlueTypeEnum.values()) {
+            if (item.name().equals(name)) {
+                return item;
+            }
+        }
+        return null;
+    }
+
+}

+ 90 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/glue/impl/SpringGlueFactory.java

@@ -0,0 +1,90 @@
+package com.ruoyi.ucp.glue.impl;
+
+import com.ruoyi.ucp.glue.GlueFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.core.annotation.AnnotationUtils;
+
+import javax.annotation.Resource;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+/**
+ * @author xuxueli 2018-11-01
+ */
+public class SpringGlueFactory extends GlueFactory implements ApplicationContextAware {
+    private static Logger logger = LoggerFactory.getLogger(SpringGlueFactory.class);
+
+
+    /**
+     * inject action of spring
+     * @param instance
+     */
+    @Override
+    public void injectService(Object instance){
+        if (instance==null) {
+            return;
+        }
+
+        //if (XxlJobSpringExecutor.getApplicationContext() == null) {
+        //    return;
+        //}
+
+        Field[] fields = instance.getClass().getDeclaredFields();
+        for (Field field : fields) {
+            if (Modifier.isStatic(field.getModifiers())) {
+                continue;
+            }
+
+            Object fieldBean = null;
+            // with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
+
+            if (AnnotationUtils.getAnnotation(field, Resource.class) != null) {
+                try {
+                    Resource resource = AnnotationUtils.getAnnotation(field, Resource.class);
+                    if (resource.name()!=null && resource.name().length()>0){
+                        fieldBean = SpringGlueFactory.getApplicationContext().getBean(resource.name());
+                    } else {
+                        fieldBean = SpringGlueFactory.getApplicationContext().getBean(field.getName());
+                    }
+                } catch (Exception e) {
+                }
+                if (fieldBean==null ) {
+                    fieldBean = SpringGlueFactory.getApplicationContext().getBean(field.getType());
+                }
+            } else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) {
+                Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class);
+                if (qualifier!=null && qualifier.value()!=null && qualifier.value().length()>0) {
+                    fieldBean = SpringGlueFactory.getApplicationContext().getBean(qualifier.value());
+                } else {
+                    fieldBean = SpringGlueFactory.getApplicationContext().getBean(field.getType());
+                }
+            }
+
+            if (fieldBean!=null) {
+                field.setAccessible(true);
+                try {
+                    field.set(instance, fieldBean);
+                } catch (IllegalArgumentException e) {
+                    logger.error(e.getMessage(), e);
+                } catch (IllegalAccessException e) {
+                    logger.error(e.getMessage(), e);
+                }
+            }
+        }
+    }
+    private static ApplicationContext applicationContext;
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        SpringGlueFactory.applicationContext = applicationContext;
+    }
+
+    public static ApplicationContext getApplicationContext() {
+        return applicationContext;
+    }
+}

+ 42 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/handler/IJobHandler.java

@@ -0,0 +1,42 @@
+package com.ruoyi.ucp.handler;
+
+/**
+ * job handler
+ *
+ * @author xuxueli 2015-12-19 19:06:38
+ */
+public abstract class IJobHandler {
+
+
+	/**
+	 * execute handler, invoked when executor receives a scheduling request
+	 *
+	 * @throws Exception
+	 */
+	public abstract void execute() throws Exception;
+
+
+	/*@Deprecated
+	public abstract ReturnT<String> execute(String param) throws Exception;*/
+
+	/**
+	 * init handler, invoked when JobThread init
+	 */
+	public void init() throws Exception {
+		// do something
+	}
+
+
+	/**
+	 * destroy handler, invoked when JobThread destroy
+	 */
+	public void destroy() throws Exception {
+		// do something
+	}
+
+	public abstract IJobHandler getFunctionHandler();
+
+	public abstract void setFunctionHandler(IJobHandler jobHandler);
+
+
+}

+ 48 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/handler/impl/GlueJobHandler.java

@@ -0,0 +1,48 @@
+package com.ruoyi.ucp.handler.impl;
+
+import com.ruoyi.ucp.handler.IJobHandler;
+
+/**
+ * glue job handler
+ *
+ * @author xuxueli 2016-5-19 21:05:45
+ */
+public class GlueJobHandler extends IJobHandler {
+
+	private long glueUpdatetime;
+	private IJobHandler jobHandler;
+	private IJobHandler functionHandler;
+	public GlueJobHandler(IJobHandler jobHandler, long glueUpdatetime) {
+		this.jobHandler = jobHandler;
+		this.glueUpdatetime = glueUpdatetime;
+	}
+	public long getGlueUpdatetime() {
+		return glueUpdatetime;
+	}
+
+	@Override
+	public void execute() throws Exception {
+		//XxlJobHelper.log("----------- glue.version:"+ glueUpdatetime +" -----------");
+		jobHandler.execute();
+	}
+
+	@Override
+	public void init() throws Exception {
+		this.jobHandler.init();
+	}
+
+	@Override
+	public void destroy() throws Exception {
+		this.jobHandler.destroy();
+	}
+
+	@Override
+	public IJobHandler getFunctionHandler() {
+		return null;
+	}
+
+	@Override
+	public void setFunctionHandler(IJobHandler jobHandler) {
+
+	}
+}

+ 63 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/handler/impl/MethodJobHandler.java

@@ -0,0 +1,63 @@
+package com.ruoyi.ucp.handler.impl;
+
+import com.ruoyi.ucp.handler.IJobHandler;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author xuxueli 2019-12-11 21:12:18
+ */
+public class MethodJobHandler extends IJobHandler {
+
+    private final Object target;
+    private final Method method;
+    private Method initMethod;
+    private Method destroyMethod;
+
+    public MethodJobHandler(Object target, Method method, Method initMethod, Method destroyMethod) {
+        this.target = target;
+        this.method = method;
+
+        this.initMethod = initMethod;
+        this.destroyMethod = destroyMethod;
+    }
+
+    @Override
+    public void execute() throws Exception {
+        Class<?>[] paramTypes = method.getParameterTypes();
+        if (paramTypes.length > 0) {
+            method.invoke(target, new Object[paramTypes.length]);       // method-param can not be primitive-types
+        } else {
+            method.invoke(target);
+        }
+    }
+
+    @Override
+    public void init() throws Exception {
+        if(initMethod != null) {
+            initMethod.invoke(target);
+        }
+    }
+
+    @Override
+    public void destroy() throws Exception {
+        if(destroyMethod != null) {
+            destroyMethod.invoke(target);
+        }
+    }
+
+    @Override
+    public IJobHandler getFunctionHandler() {
+        return null;
+    }
+
+    @Override
+    public void setFunctionHandler(IJobHandler jobHandler) {
+
+    }
+
+    @Override
+    public String toString() {
+        return super.toString()+"["+ target.getClass() + "#" + method.getName() +"]";
+    }
+}

+ 23 - 0
universal-computing-platform/src/main/java/com/ruoyi/ucp/handler/impl/MyJobHandler.java

@@ -0,0 +1,23 @@
+package com.ruoyi.ucp.handler.impl;
+
+import com.ruoyi.ucp.handler.IJobHandler;
+import org.springframework.stereotype.Component;
+
+/**
+ * glue job handler
+ *
+ * @author xuxueli 2016-5-19 21:05:45
+ */
+@Component
+public class MyJobHandler {
+
+	private IJobHandler jobHandler;
+
+	public IJobHandler getJobHandler() {
+		return jobHandler;
+	}
+
+	public void setJobHandler(IJobHandler jobHandler) {
+		this.jobHandler = jobHandler;
+	}
+}

+ 12 - 5
universal-computing-platform/src/main/java/com/ruoyi/ucp/util/MyTask.java

@@ -1,5 +1,9 @@
 package com.ruoyi.ucp.util;
 
+import com.ruoyi.ucp.glue.GlueFactory;
+import com.ruoyi.ucp.handler.IJobHandler;
+import com.ruoyi.ucp.handler.impl.GlueJobHandler;
+import com.ruoyi.ucp.handler.impl.MyJobHandler;
 import com.ruoyi.ucp.service.IFormulaService;
 import org.springframework.stereotype.Component;
 
@@ -14,16 +18,19 @@ import javax.script.ScriptException;
  */
 @Component("myTask")
 public class MyTask {
+    //@Resource
+    //private ScriptEngine engine;
     @Resource
-    private ScriptEngine engine;
-    @Resource
-    private IFormulaService formulaService;
+    private MyJobHandler myJobHandler;
 
     public void myParams(String params) {
         try {
-            engine.eval(formulaService.getNEMap().get(params));
+            String javaCode = GlueFactory.getJavaGlue().replace("//此处加运行方法", "jobHandler." + params + "();");
+            IJobHandler jobHandler = GlueFactory.getInstance().loadNewInstance(javaCode);
+            jobHandler.setFunctionHandler(myJobHandler.getJobHandler());
+            jobHandler.execute();
             System.out.println("执行有参方法:" + params);
-        } catch (ScriptException e) {
+        } catch (Exception e) {
             e.printStackTrace();
             System.out.println("执行有参方法:" + params + "失败!");
         }

+ 21 - 0
universal-computing-platform/src/test/java/com/ruoyi/ucp/JavaFunctionJobHandler.java

@@ -0,0 +1,21 @@
+package com.ruoyi.ucp;
+
+import com.ruoyi.ucp.handler.IJobHandler;
+
+public class JavaFunctionJobHandler  extends IJobHandler {
+    @Override
+    public void execute() throws Exception {
+
+    }
+
+    @Override
+    public IJobHandler getFunctionHandler() {
+        return null;
+    }
+
+    @Override
+    public void setFunctionHandler(IJobHandler jobHandler) {
+
+    }
+    //此处加公式
+}

+ 41 - 0
universal-computing-platform/src/test/java/com/ruoyi/ucp/JavaGlueJobHandler.java

@@ -0,0 +1,41 @@
+package com.ruoyi.ucp;
+
+import com.ruoyi.ucp.feign.AdapterApi;
+import com.ruoyi.ucp.handler.IJobHandler;
+import com.ruoyi.ucp.service.ILineInfoDayService;
+import com.ruoyi.ucp.service.IPointInfoService;
+import com.ruoyi.ucp.service.IStationInfoDayService;
+import com.ruoyi.ucp.service.IStationInfoHourService;
+
+import javax.annotation.Resource;
+
+public class JavaGlueJobHandler extends IJobHandler{
+
+    @Resource
+    private AdapterApi adapter;
+    @Resource
+    private IPointInfoService pointService;
+    @Resource
+    private IStationInfoHourService stationInfoHourService;
+    @Resource
+    private IStationInfoDayService stationInfoDayService;
+    @Resource
+    private ILineInfoDayService lineInfoDayService;
+
+    private IJobHandler jobHandler;
+
+    @Override
+    public void execute() throws Exception {
+        //此处运行方法
+    }
+
+    @Override
+    public IJobHandler getFunctionHandler() {
+        return jobHandler;
+    }
+
+    @Override
+    public void setFunctionHandler(IJobHandler jobHandler) {
+        this.jobHandler = jobHandler;
+    }
+}