Version 2.0.3及以上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@
Data
@
Configuration
@
ConfigurationProperties
(
prefix
=
"com.antergone.ejob"
)
@
ConditionalOnProperty
(
prefix
=
"com.antergone.ejob"
,
name
=
"enabled"
,
havingValue
=
"true"
)
public
class
ElasticJobConfig
{
private
String
zkNodes
;
private
String
namespace
;
@
Bean
public
ZookeeperConfiguration
zkConfig
(
)
{
return
new
ZookeeperConfiguration
(
zkNodes
,
namespace
)
;
}
@
Bean
(
initMethod
=
"init"
)
public
ZookeeperRegistryCenter
regCenter
(
ZookeeperConfiguration
config
)
{
return
new
ZookeeperRegistryCenter
(
config
)
;
}
|
定义任务也比较简单,方法也比较多。前期我是直接把每个任务都做成一个Bean(代码段1),后来看了源码发现这个实例只需要init向zk注册一下任务信息,所以就换用了第二种方式(代码段2)。
代码段1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@
Configuration
@
ConditionalOnBean
(
JobConfig
.
class
)
@
RequiredArgsConstructor
(
onConstructor
=
@
__
(
@
Autowired
)
)
public
class
ElasticJobs
{
private
final
ZookeeperRegistryCenter
regCenter
;
private
final
SampleJob
sampleJob
;
private
final
SampleJobListener
sampleJobListener
;
@
Bean
(
initMethod
=
"init"
)
private
SpringJobScheduler
sampleJob
(
)
{
LiteJobConfiguration
jobConfig
=
LiteJobConfiguration
.
newBuilder
(
new
SimpleJobConfiguration
(
JobCoreConfiguration
.
newBuilder
(
"Sample Job"
,
"0 0 22 * * ? "
,
5
)
.
build
(
)
,
SampleJob
.
class
.
getCanonicalName
(
)
)
)
.
overwrite
(
true
)
.
build
(
)
;
return
new
SpringJobScheduler
(
sampleJob
,
regCenter
,
jobConfig
,
sampleJobListener
)
.
init
(
)
;
}
}
|
代码段2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@
Configuration
@
ConditionalOnBean
(
JobConfig
.
class
)
@
RequiredArgsConstructor
(
onConstructor
=
@
__
(
@
Autowired
)
)
public
class
ElasticJobs
{
private
final
ZookeeperRegistryCenter
regCenter
;
private
final
SampleJob
sampleJob
;
private
final
SampleJobListener
sampleJobListener
;
@
PostConstruct
public
void
init
(
)
{
_sampleJob
(
)
;
}
private
void
_sampleJob
(
)
{
LiteJobConfiguration
jobConfig
=
LiteJobConfiguration
.
newBuilder
(
new
SimpleJobConfiguration
(
JobCoreConfiguration
.
newBuilder
(
"Sample Job"
,
"0 0 22 * * ? "
,
5
)
.
build
(
)
,
SampleJob
.
class
.
getCanonicalName
(
)
)
)
.
overwrite
(
true
)
.
build
(
)
;
new
SpringJobScheduler
(
sampleJob
,
regCenter
,
jobConfig
,
sampleJobListener
)
.
init
(
)
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
public
final
class
SampleJobHandler
implements
ExecutorServiceHandler
{
public
SampleJobHandler
(
)
{
}
@
Override
public
ExecutorService
createExecutorService
(
String
jobName
)
{
return
ExecutorManager
.
SAMPLE_JOB_EXECUTOR
;
}
}
|
1
2
3
4
5
6
7
8
|
private
void
_sampleJob
(
)
{
LiteJobConfiguration
jobConfig
=
LiteJobConfiguration
.
newBuilder
(
new
SimpleJobConfiguration
(
JobCoreConfiguration
.
newBuilder
(
"Sample Job"
,
"0 0 22 * * ? "
,
5
)
.
jobProperties
(
"executor_service_handler"
,
ExecutorManager
.
SAMPLE_JOB_EXECUTOR
)
.
build
(
)
,
SampleJob
.
class
.
getCanonicalName
(
)
)
)
.
overwrite
(
true
)
.
build
(
)
;
new
SpringJobScheduler
(
sampleJob
,
regCenter
,
jobConfig
,
sampleJobListener
)
.
init
(
)
;
}
|
这样就可以控制每个任务执行节点上的线程池。
用了一段时间的ElasticJob,官方API都是使用原有的XML进行配置。之前有跟一个开发者聊过,他们甚至没有考虑过做SpringBoot的尝试。SpringBoot上使用ElasticJob还有很多坑没有踩,还需要更多更多的实践和探究。我上面的demo也只能说可以用,并没有做什么优化和创新。