From 1a2c0c5f39aca4834df4310459128801ce1a2b9b Mon Sep 17 00:00:00 2001 From: "mingtao.sun" Date: Tue, 18 Dec 2018 18:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0class=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E7=BC=93=E5=AD=98=EF=BC=8C=E8=A7=A3=E5=86=B3=E9=A2=91?= =?UTF-8?q?=E7=B9=81=E5=8A=A0=E8=BD=BDclass=E4=BC=9A=E4=BD=BFjvm=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=8C=BA=E7=A9=BA=E9=97=B4=E4=B8=8D=E8=B6=B3?= =?UTF-8?q?=E5=AF=BC=E8=87=B4OOM=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/core/biz/impl/ExecutorBizImpl.java | 2 +- .../com/xxl/job/core/glue/GlueFactory.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java b/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java index e5b90b7a..36b12f2f 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java @@ -113,7 +113,7 @@ public class ExecutorBizImpl implements ExecutorBiz { // valid handler if (jobHandler == null) { try { - IJobHandler originJobHandler = GlueFactory.getInstance().loadNewInstance(triggerParam.getGlueSource()); + IJobHandler originJobHandler = GlueFactory.getInstance().loadNewInstance(triggerParam.getJobId(), triggerParam.getGlueSource()); jobHandler = new GlueJobHandler(originJobHandler, triggerParam.getGlueUpdatetime()); } catch (Exception e) { logger.error(e.getMessage(), e); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java index 8b9e8cd2..d6104026 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java @@ -4,6 +4,10 @@ import com.xxl.job.core.glue.impl.SpringGlueFactory; import com.xxl.job.core.handler.IJobHandler; import groovy.lang.GroovyClassLoader; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.util.concurrent.ConcurrentHashMap; + /** * glue factory, product class/object by name * @@ -30,6 +34,8 @@ public class GlueFactory { */ private GroovyClassLoader groovyClassLoader = new GroovyClassLoader(); + private static final ConcurrentHashMap> CLASS_CACHE = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap JOBID_MD5KEY_CACHE = new ConcurrentHashMap<>(); /** * load new instance, prototype @@ -38,9 +44,9 @@ public class GlueFactory { * @return * @throws Exception */ - public IJobHandler loadNewInstance(String codeSource) throws Exception{ + public IJobHandler loadNewInstance(long jobId, String codeSource) throws Exception{ if (codeSource!=null && codeSource.trim().length()>0) { - Class clazz = groovyClassLoader.parseClass(codeSource); + Class clazz = getCodeSourceClass(jobId, codeSource); if (clazz != null) { Object instance = clazz.newInstance(); if (instance!=null) { @@ -66,4 +72,28 @@ public class GlueFactory { // do something } + private Class getCodeSourceClass(long jobId, String codeSource){ + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] md5 = md.digest(codeSource.getBytes()); + BigInteger no = new BigInteger(1, md5); + String md5Str = no.toString(16); + Class clazz = CLASS_CACHE.get(md5Str); + if(clazz == null){ + clazz = groovyClassLoader.parseClass(codeSource); + Class preClazz = CLASS_CACHE.putIfAbsent(md5Str, clazz); + + // 如果代碼有變化則刪除之前class緩存 + if(preClazz == null){ + String preMd5 = JOBID_MD5KEY_CACHE.put(jobId, md5Str); + if(preMd5 != null){ + CLASS_CACHE.remove(preMd5); + } + } + } + return clazz; + } catch (Exception e) { + return groovyClassLoader.parseClass(codeSource); + } + } }