Commit 98389553 by zhouhaibao

[+]测试java

1 parent 42b3aeb3
package cn.com.glsx.process.modules.controller; package cn.com.glsx.process.modules.controller;
import cn.com.glsx.process.modules.service.UserDemoService;
import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.glue.GlueTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -25,6 +27,9 @@ import java.util.Date; ...@@ -25,6 +27,9 @@ import java.util.Date;
@ResponseResultBody @ResponseResultBody
public class TestXXLJobController { public class TestXXLJobController {
@Autowired
private UserDemoService userDemoService;
@GetMapping("/jobtest") @GetMapping("/jobtest")
@Operation(summary = "动态任务添加", description = "动态任务添加") @Operation(summary = "动态任务添加", description = "动态任务添加")
public void addtest(@RequestParam("cron") String cron, @RequestParam("jobName") String jobName, @RequestParam("param") String param, public void addtest(@RequestParam("cron") String cron, @RequestParam("jobName") String jobName, @RequestParam("param") String param,
...@@ -85,4 +90,10 @@ public class TestXXLJobController { ...@@ -85,4 +90,10 @@ public class TestXXLJobController {
public R findBybusKey(@RequestParam("busKey") String busKey) { public R findBybusKey(@RequestParam("busKey") String busKey) {
return R.ok().data(XxlJobUtil.findByBusKey(busKey)); return R.ok().data(XxlJobUtil.findByBusKey(busKey));
} }
@PostMapping("/testJava")
@Operation(summary = "测试java", description = "动态任务触发")
public R testJava(@RequestParam("busKey") String busKey) throws Exception {
return R.ok().data(userDemoService.testJava(busKey));
}
} }
...@@ -8,7 +8,10 @@ import cn.com.glsx.process.modules.model.UserDTO; ...@@ -8,7 +8,10 @@ import cn.com.glsx.process.modules.model.UserDTO;
import cn.com.glsx.process.modules.model.UserSearch; import cn.com.glsx.process.modules.model.UserSearch;
import cn.com.glsx.process.modules.sop.RunResult; import cn.com.glsx.process.modules.sop.RunResult;
import cn.com.glsx.process.plugin.build.SopProduct; import cn.com.glsx.process.plugin.build.SopProduct;
import cn.com.glsx.process.plugin.java.DynamicEngine;
import cn.com.glsx.process.redis.DistributedLockUtils; import cn.com.glsx.process.redis.DistributedLockUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.glsx.plat.common.utils.ObjectUtils; import com.glsx.plat.common.utils.ObjectUtils;
import com.glsx.plat.common.utils.SessionUtils; import com.glsx.plat.common.utils.SessionUtils;
...@@ -151,4 +154,12 @@ public class UserDemoService { ...@@ -151,4 +154,12 @@ public class UserDemoService {
System.out.println(ExecutorRouteStrategyEnum.FIRST.name()); System.out.println(ExecutorRouteStrategyEnum.FIRST.name());
Thread.sleep(10 * 1000); Thread.sleep(10 * 1000);
} }
public Object testJava(String user) throws Exception {
DynamicEngine de = DynamicEngine.getInstance();
String source = "user";
JSONObject jsonObject=new JSONObject();
jsonObject.put("id",22);
return de.execute("Controller", source, "execute", new Class[]{JSON.class}, new Object[]{jsonObject});
}
} }
/* /*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com) * @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package cn.com.glsx.process.plugin.java; package cn.com.glsx.process.plugin.java;
import cn.com.glsx.process.common.constant.Constants;
import javax.tools.*;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -24,29 +27,18 @@ import java.net.URLClassLoader; ...@@ -24,29 +27,18 @@ import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
import cn.com.glsx.process.common.constant.Constants;
public class DynamicEngine { public class DynamicEngine {
private static DynamicEngine ourInstance = new DynamicEngine(); private static DynamicEngine ourInstance = new DynamicEngine();
public static DynamicEngine getInstance() { public static DynamicEngine getInstance() {
return ourInstance; return ourInstance;
} }
private URLClassLoader parentClassLoader; private URLClassLoader parentClassLoader;
private String classpath; private String classpath;
private DynamicEngine() { private DynamicEngine() {
this.parentClassLoader = (URLClassLoader) this.getClass().getClassLoader(); this.parentClassLoader = (URLClassLoader) this.getClass().getClassLoader();
this.buildClassPath(); this.buildClassPath();
} }
private void buildClassPath() { private void buildClassPath() {
this.classpath = null; this.classpath = null;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -59,7 +51,6 @@ public class DynamicEngine { ...@@ -59,7 +51,6 @@ public class DynamicEngine {
/** /**
* 调用类方法 * 调用类方法
*
* @param cls 类 * @param cls 类
* @param methodName 方法名 * @param methodName 方法名
* @param paramsCls 方法参数类型 * @param paramsCls 方法参数类型
...@@ -72,8 +63,7 @@ public class DynamicEngine { ...@@ -72,8 +63,7 @@ public class DynamicEngine {
* @throws InvocationTargetException * @throws InvocationTargetException
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ */
public Object invoke(Class<?> cls, String methodName, Class<?>[] paramsCls, Object[] params) throws NoSuchMethodException, SecurityException, public Object invoke(Class<?> cls, String methodName, Class<?>[] paramsCls, Object[] params) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Object result = null; Object result = null;
Method method = cls.getDeclaredMethod(methodName, paramsCls); Method method = cls.getDeclaredMethod(methodName, paramsCls);
Object obj = cls.newInstance(); Object obj = cls.newInstance();
...@@ -81,7 +71,7 @@ public class DynamicEngine { ...@@ -81,7 +71,7 @@ public class DynamicEngine {
return result; return result;
} }
public Object execute(String fullClassName, String javaCode, String method, Class<?>[] paramsCls, Object[] params) throws Exception { public Object execute(String fullClassName, String javaCode, String method, Class<?>[] paramsCls,Object[] params) throws Exception {
Object result = null; Object result = null;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
...@@ -102,10 +92,10 @@ public class DynamicEngine { ...@@ -102,10 +92,10 @@ public class DynamicEngine {
if (success) { if (success) {
JavaClassObject jco = fileManager.getJavaClassObject(); JavaClassObject jco = fileManager.getJavaClassObject();
DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(this.parentClassLoader); DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(this.parentClassLoader);
Class<?> clazz = dynamicClassLoader.loadClass(fullClassName, jco); Class<?> clazz = dynamicClassLoader.loadClass(fullClassName,jco);
result = invoke(clazz, method, paramsCls, params); result = invoke(clazz, method, paramsCls, params);
// instance = clazz.newInstance(); // instance = clazz.newInstance();
dynamicClassLoader.close(); dynamicClassLoader.close();
} else { } else {
StringBuilder error = new StringBuilder(); StringBuilder error = new StringBuilder();
...@@ -131,4 +121,4 @@ public class DynamicEngine { ...@@ -131,4 +121,4 @@ public class DynamicEngine {
res.append("ColumnNumber:[" + diagnostic.getColumnNumber() + "]\n"); res.append("ColumnNumber:[" + diagnostic.getColumnNumber() + "]\n");
return res.toString(); return res.toString();
} }
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!