一招教你如何减少本地调试tomcat重启次数
当我们进行本地调试的时候,代码做了少量改动,却要重启tomcat。如果项目比较小还行,如果项目比较大这个时候重启tomcat的时间就比较长。下面我说的方法将会让你减少tomcat不必要的重启次数。
这次引入的技术为Groovy。
在groovy中书写的代码无需重启tomcat,修改之后需需要重新从入口进入就行了
什么是Gooovy
Apache Groovy是一种功能强大、可选的类型和动态语言,具有静态键入和静态编译功能,适用于Java平台,旨在通过简洁、熟悉和易于学习的语法提高开发人员的工作效率。它与任何Java程序顺利集成,并立即为您的应用程序提供强大的功能,包括脚本功能、特定域语言创作、运行时和编译时元编程以及功能编程。和Java兼容性强,可以无缝衔接Java代码,可以调用Java所有的库。
多得不说,直接上代码
pom依赖
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-jsr223</artifactId>
<version>3.0.6</version>
</dependency>
Controller
@Controller
@Slf4j
public class ScriptAction {
@Autowired
private GroovyEval groovyEval;
@RequestMapping(value = "/script/test")
public Object scriptTest(
@Param(value = "path", required = true) String path,
@Json("@requestBody") @RequestBody Map<String,Object> paramMap
) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
String date;
StringBuilder stringBuilder = new StringBuilder();
while((date = bufferedReader.readLine()) != null){
stringBuilder.append(date).append("\n");
}
bufferedReader.close();
return groovyEval.evalScript(bufferedReader.toString() , "solution" , new Object[]{paramMap});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Service
import com.google.gson.Gson;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class GroovyEval implements ApplicationContextAware {
private static GroovyEval groovyEval;
private ApplicationContext applicationContext;
public static <T> T getBean(Class<T> cls){
return groovyEval.applicationContext.getBean(cls);
}
public Object evalScript(String script, String methodName, Object[] args){
Object scriptObj = this.getScript(script);
try {
Map<String, Object> resultMap = (Map<String, Object>)((GroovyObject)scriptObj).invokeMethod(methodName, args);
if (CollectionUtils.isEmpty(resultMap)){
return null;
}
return resultMap.get("data");
} catch (Throwable e) {
log.error("script eval error !" , e);
}
return null;
}
private Object getScript(String script){
Class<?> cls = new GroovyClassLoader().parseClass(script);
GroovyObject groovyObject = null;
try {
log.info("load script!");
groovyObject = (GroovyObject)cls.newInstance();
} catch (IllegalAccessException | InstantiationException e) {
log.error("load script error ! script : {}" , script , e);
}
return groovyObject;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
groovyEval = this;
}
}
Groovy脚本
TestGroovy.groovy
class TestGroovy {
def Map<String,Object> solution(Map<String,Object> paramMap){
Map<String,Object> resultMap = [:];
Object shopCodes = paramMap.get("param");
resultMap.put("data", "resultData");
return resultMap;
}
}
生产环境使用
因为groovy每执行一次脚本,都会生成一个脚本的class对象,这个class对象的名字由 “script” + System.currentTimeMillis() + Math.abs(text.hashCode())组成,因此应用到生产环境需要加入缓存。推荐使用高性能缓存:Caffeine,
官方介绍Caffeine是基于JDK8的高性能本地缓存库,提供了几乎完美的命中率。它有点类似JDK中的ConcurrentMap,实际上,Caffeine中的LocalCache接口就是实现了JDK中的ConcurrentMap接口,但两者并不完全一样。最根本的区别就是,ConcurrentMap保存所有添加的元素,除非显示删除之(比如调用remove方法)。而本地缓存一般会配置自动剔除策略,为了保护应用程序,限制内存占用情况,防止内存溢出。
有兴趣的可以自己去搜索一下,我感觉蛮好用的
@Component
public class GroovyEval implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(GroovyEval.class);
private static final Object source = new Object();
private static GroovyEval groovyEval;
private ApplicationContext applicationContext;
@Autowired
private AlarmThresholdSettingsItemService alarmThresholdSettingsItemService;
public static <T> T getBean(Class<T> cls){
return groovyEval.applicationContext.getBean(cls);
}
private static final Cache<Object, Object> caffeine = Caffeine
.newBuilder()
.maximumSize(30000)
.expireAfterAccess(72 , TimeUnit.HOURS)
.build();
public Map lookUp(){
return caffeine.asMap();
}
public Object evalScript(String script,String methodName,Object[] args) {
Object scriptObj = this.getScript(script);
if(scriptObj != null){
try{
Map<String, Object> resultMap = (Map<String, Object>) ((GroovyObject) scriptObj).invokeMethod(methodName, args);
if(CollectionUtils.isEmpty(resultMap)){
return null;
}
return resultMap.get("data");
}catch (Throwable e){
LOGGER.error("script eval error !" , e);
}
}
return null;
}
private Object getScript(String script){
String cacheKey = DigestUtils.md5Hex(script);
return caffeine.get(cacheKey, new Function<Object, Object>() {
@Override
public Object apply(Object key) {
synchronized (source){
Class<?> cls = new GroovyClassLoader().parseClass(script);
GroovyObject gObj = null;
try {
LOGGER.info("load script !");
gObj = (GroovyObject) cls.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
LOGGER.error("load script error ! script : {}" , script , e);
}
return gObj;
}
}
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
groovyEval = this;
}
}
|