Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
熊鹏飞
/
xxljob220
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1a2c0c5f
authored
Dec 18, 2018
by
mingtao.sun
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
增加class的加载缓存,解决频繁加载class会使jvm的方法区空间不足导致OOM的问题
parent
bd30a59e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
3 deletions
xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java
xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java
xxl-job-core/src/main/java/com/xxl/job/core/biz/impl/ExecutorBizImpl.java
View file @
1a2c0c5f
...
...
@@ -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
.
get
JobId
(),
triggerParam
.
get
GlueSource
());
jobHandler
=
new
GlueJobHandler
(
originJobHandler
,
triggerParam
.
getGlueUpdatetime
());
}
catch
(
Exception
e
)
{
logger
.
error
(
e
.
getMessage
(),
e
);
...
...
xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java
View file @
1a2c0c5f
...
...
@@ -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
<
String
,
Class
<?>>
CLASS_CACHE
=
new
ConcurrentHashMap
<>();
private
static
final
ConcurrentHashMap
<
Long
,
String
>
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
=
g
roovyClassLoader
.
parseClass
(
codeSource
);
Class
<?>
clazz
=
g
etCodeSourceClass
(
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
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment