|
|
@ -33,9 +33,7 @@ public class GlueFactory {
|
|
|
|
* groovy class loader
|
|
|
|
* groovy class loader
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
|
|
|
|
private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
|
|
|
|
|
|
|
|
private ConcurrentHashMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
|
|
|
|
private static final ConcurrentHashMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
private static final ConcurrentHashMap<Long, String> JOBID_MD5KEY_CACHE = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* load new instance, prototype
|
|
|
|
* load new instance, prototype
|
|
|
@ -44,9 +42,9 @@ public class GlueFactory {
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public IJobHandler loadNewInstance(long jobId, String codeSource) throws Exception{
|
|
|
|
public IJobHandler loadNewInstance(String codeSource) throws Exception{
|
|
|
|
if (codeSource!=null && codeSource.trim().length()>0) {
|
|
|
|
if (codeSource!=null && codeSource.trim().length()>0) {
|
|
|
|
Class<?> clazz = getCodeSourceClass(jobId, codeSource);
|
|
|
|
Class<?> clazz = getCodeSourceClass(codeSource);
|
|
|
|
if (clazz != null) {
|
|
|
|
if (clazz != null) {
|
|
|
|
Object instance = clazz.newInstance();
|
|
|
|
Object instance = clazz.newInstance();
|
|
|
|
if (instance!=null) {
|
|
|
|
if (instance!=null) {
|
|
|
@ -62,38 +60,30 @@ public class GlueFactory {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
|
|
|
|
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Class<?> getCodeSourceClass(String codeSource){
|
|
|
|
/**
|
|
|
|
|
|
|
|
* inject service of bean field
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param instance
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void injectService(Object instance) {
|
|
|
|
|
|
|
|
// do something
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Class<?> getCodeSourceClass(long jobId, String codeSource){
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
|
// md5
|
|
|
|
byte[] md5 = md.digest(codeSource.getBytes());
|
|
|
|
byte[] md5 = MessageDigest.getInstance("MD5").digest(codeSource.getBytes());
|
|
|
|
BigInteger no = new BigInteger(1, md5);
|
|
|
|
String md5Str = new BigInteger(1, md5).toString(16);
|
|
|
|
String md5Str = no.toString(16);
|
|
|
|
|
|
|
|
Class<?> clazz = CLASS_CACHE.get(md5Str);
|
|
|
|
Class<?> clazz = CLASS_CACHE.get(md5Str);
|
|
|
|
if(clazz == null){
|
|
|
|
if(clazz == null){
|
|
|
|
clazz = groovyClassLoader.parseClass(codeSource);
|
|
|
|
clazz = groovyClassLoader.parseClass(codeSource);
|
|
|
|
Class<?> preClazz = CLASS_CACHE.putIfAbsent(md5Str, clazz);
|
|
|
|
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;
|
|
|
|
return clazz;
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
return groovyClassLoader.parseClass(codeSource);
|
|
|
|
return groovyClassLoader.parseClass(codeSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* inject service of bean field
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param instance
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void injectService(Object instance) {
|
|
|
|
|
|
|
|
// do something
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|