java - groovy 执行 实例
Posted 栗子~~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java - groovy 执行 实例相关的知识,希望对你有一定的参考价值。
文章目录
前言
如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
java - groovy 执行 实例
1、准备
vo:
public class IndexFlag
private String index;
private String name;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getIndex()
return index;
public void setIndex(String index)
this.index = index;
groovy 脚本 - testCompliance
package groovy
import com.yzy.vo.IndexFlag
def callCompliance(IndexFlag reqBO)
String msg = ""
String index = reqBO.getIndex();
switch (index)
case "1":
msg = "A"
break
case "2":
msg = "B"
break
case "3":
msg = "C"
break
case "4":
msg = "D"
break
case "5":
msg = "E"
break
case "6":
msg = "F"
break
default:
msg = "X"
break
reqBO.setName(msg)
return reqBO
callCompliance(reqBO)
存放位置:
日志:
/**
* @author yangzhenyu
* 日志模板
* */
public abstract class BaseLog implements BaseLogInterface
private static Logger log = LoggerFactory.getLogger(BaseLog.class);
private ReentrantLock lock = new ReentrantLock();
private String clazz;
public void setClazz(String clazz)
this.clazz = clazz;
/**
* 打印错误日志
* */
public void error(String msg)
try
lock.lock();
log.error(msg);
finally
lock.unlock();
/**
* 打印普通日志
* */
public void info(String msg,Object... var)
try
lock.lock();
log.info(msg,var);
finally
lock.unlock();
/**
* 打印普通日志
* */
public void info(String msg)
try
lock.lock();
log.info(msg);
finally
lock.unlock();
/**
* 返回时间日志
* */
public final void endLog(String msg,long startTime)
try
lock.lock();
long endTime = System.currentTimeMillis();
log.info("<======执行源:【】==> 执行结束,耗时:秒 =======>",clazz,msg,((endTime-startTime)/1000));
finally
lock.unlock();
/**
* 返回时间错误日志
* */
public final void endLogError(String msg,long startTime,Throwable e)
try
lock.lock();
long endTime = System.currentTimeMillis();
log.error(String.format("<======执行源:【%s】==> %s执行结束,耗时:%s秒 =======>",clazz,msg,((endTime-startTime)/1000)),e);
finally
lock.unlock();
/**
* 返回开始时间
* */
public final long startLog(String msgKey,String param)
try
lock.lock();
log.info("<======执行源:【】==>方法[]开始执行 =======>",clazz,msgKey);
log.info("<======参数:【】 =======>",param);
return System.currentTimeMillis();
finally
lock.unlock();
2、编写controller
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.yzy.util.BaseLog;
import com.yzy.util.GroovyUtil;
import com.yzy.util.ResponseBo;
import com.yzy.vo.IndexFlag;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import io.micrometer.core.instrument.util.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author yangzhenyu
* @version 1.0
* @description:
* @date 2022/11/15 11:30
*/
@Api(value = "groovy测试", tags = "groovy测试")
@RestController
@RequestMapping("api/test/")
public class TestController extends BaseLog
@Override
public long init(String name, String param)
this.setClazz("TestController");
return startLog(name,param);
@ApiOperation(value = "groovy测试",notes = "groovy测试")
@ResponseBody
@PostMapping("/forkJoin")
public ResponseBo forkJoin(String index)
String msgValue = "forkJoinPool";
long startTime = init(msgValue,index);
try
File file = ResourceUtils.getFile("classpath:groovy/testCompliance.groovy");
Map<String, Object> vars = Maps.newHashMap();
IndexFlag indexFlag = new IndexFlag();
indexFlag.setIndex(index);
vars.put("reqBO", indexFlag);
GroovyShell groovyShell = new GroovyShell();
Script script = groovyShell.parse(file);
Binding binding = new Binding(vars);
script.setBinding(binding);
script.run();
info("script:", JSON.toJSON(script));
endLog(msgValue,startTime);
return ResponseBo.ok(script);
catch (Exception e)
endLogError(msgValue,startTime,e);
return ResponseBo.error("保存失败" + e.getMessage());
3、执行结果
控制台打印:
以上是关于java - groovy 执行 实例的主要内容,如果未能解决你的问题,请参考以下文章
GroovyGroovy 脚本调用 ( Linux 中调用 Groovy 脚本 | Windows 中调用 Groovy 脚本 )