Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
platform
/
platform-process-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 294ce56e
authored
Sep 12, 2023
by
tiger
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Service
1 parent
64e447eb
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
149 deletions
platform-process/src/main/java/cn/com/glsx/Application.java
platform-process/src/main/java/cn/com/glsx/process/job/DynamicTaskAdd.java
platform-process/src/main/java/cn/com/glsx/process/job/DynamicTaskService.java
platform-process/src/main/java/cn/com/glsx/process/modules/controller/TestJobController.java
platform-process/src/main/java/cn/com/glsx/Application.java
View file @
294ce56
package
cn
.
com
.
glsx
;
import
cn.com.glsx.process.job.DynamicTaskAdd
;
import
com.glsx.plat.context.EnableRestAdmin
;
import
static
java
.
time
.
ZoneId
.
of
;
import
static
java
.
util
.
TimeZone
.
getTimeZone
;
import
java.util.TimeZone
;
import
javax.annotation.PostConstruct
;
import
org.apache.dubbo.config.spring.context.annotation.EnableDubbo
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.CommandLineRunner
;
...
...
@@ -9,14 +14,10 @@ import org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PostConstruct
;
import
java.util.TimeZone
;
import
com.glsx.plat.context.EnableRestAdmin
;
import
static
java
.
time
.
ZoneId
.
of
;
import
static
java
.
util
.
TimeZone
.
getTimeZone
;
import
cn.com.glsx.process.job.DynamicTaskService
;
/**
* @author payu
...
...
@@ -29,7 +30,7 @@ import static java.util.TimeZone.getTimeZone;
public
class
Application
implements
CommandLineRunner
{
@Autowired
private
DynamicTaskAdd
dynamicTaskAdd
;
private
DynamicTaskService
dynamicTaskAdd
;
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
...
...
platform-process/src/main/java/cn/com/glsx/process/job/DynamicTaskAdd.java
deleted
100644 → 0
View file @
64e447e
package
cn
.
com
.
glsx
.
process
.
job
;
import
cn.com.glsx.process.modules.entity.JobManager
;
import
cn.com.glsx.process.modules.mapper.JobManagerMapper
;
import
com.dangdang.ddframe.job.api.simple.SimpleJob
;
import
com.dangdang.ddframe.job.config.JobCoreConfiguration
;
import
com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration
;
import
com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener
;
import
com.dangdang.ddframe.job.lite.config.LiteJobConfiguration
;
import
com.dangdang.ddframe.job.lite.internal.schedule.JobRegistry
;
import
com.dangdang.ddframe.job.lite.internal.schedule.JobScheduleController
;
import
com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler
;
import
com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.Date
;
import
java.util.List
;
@Component
@Slf4j
public
class
DynamicTaskAdd
{
@Autowired
private
ZookeeperRegistryCenter
zookeeperRegistryCenter
;
@Resource
private
ElasticJobListener
elasticJobListener
;
@Autowired
private
JobManagerMapper
jobManagerMapper
;
private
static
LiteJobConfiguration
.
Builder
simpleJobConfigBuilder
(
String
jobName
,
Class
<?
extends
SimpleJob
>
jobClass
,
int
shardTotalCount
,
String
cron
,
String
id
)
{
return
LiteJobConfiguration
.
newBuilder
(
new
SimpleJobConfiguration
(
JobCoreConfiguration
.
newBuilder
(
jobName
,
cron
,
shardTotalCount
).
jobParameter
(
id
).
build
(),
jobClass
.
getCanonicalName
()
));
}
/***
* 动态创建定时任务
* @param jobName:定时任务名称
* @param cron:表达式
* @param shardingTotalCount:分片数量
* @param instance:定时任务实例
* @param parameters:参数
*/
public
void
create
(
String
jobName
,
String
cron
,
int
shardingTotalCount
,
SimpleJob
instance
,
String
parameters
)
{
LiteJobConfiguration
jobConfig
=
simpleJobConfigBuilder
(
jobName
,
instance
.
getClass
(),
shardingTotalCount
,
cron
,
parameters
)
.
overwrite
(
true
).
build
();
new
SpringJobScheduler
(
instance
,
zookeeperRegistryCenter
,
jobConfig
,
elasticJobListener
).
init
();
JobManager
jobManager
=
jobManagerMapper
.
findByName
(
jobName
);
if
(
jobManager
==
null
)
{
jobManager
=
new
JobManager
();
jobManager
.
setCron
(
cron
);
jobManager
.
setJobName
(
jobName
);
jobManager
.
setCreateTime
(
new
Date
());
jobManager
.
setParameters
(
parameters
);
jobManager
.
setClassName
(
instance
.
getClass
().
getName
());
jobManager
.
setDelFlag
(
0
);
jobManager
.
setShareTotalCount
(
shardingTotalCount
);
jobManagerMapper
.
insert
(
jobManager
);
}
}
/**
* 更新定时任务
*
* @param jobName
* @param cron
*/
public
void
updateJob
(
String
jobName
,
String
cron
)
{
log
.
info
(
"更新定时任务:jobName = {}, cron = {}"
,
jobName
,
cron
);
JobScheduleController
jobScheduleController
=
JobRegistry
.
getInstance
().
getJobScheduleController
(
jobName
);
if
(
jobScheduleController
==
null
)
{
log
.
info
(
"未找到该job或者job已经下线{}"
,
jobName
);
}
else
{
jobScheduleController
.
rescheduleJob
(
cron
);
JobManager
jobManager
=
jobManagerMapper
.
findByName
(
jobName
);
if
(
jobManager
!=
null
)
{
jobManager
.
setCron
(
cron
);
jobManager
.
setUpdateTime
(
new
Date
());
jobManagerMapper
.
updateByPrimaryKeySelective
(
jobManager
);
}
}
}
/**
* 删除定时任务
*
* @param jobName
*/
public
void
removeJob
(
String
jobName
)
{
log
.
info
(
"删除定时任务:jobName = {}"
,
jobName
);
JobScheduleController
jobScheduleController
=
JobRegistry
.
getInstance
().
getJobScheduleController
(
jobName
);
if
(
jobScheduleController
==
null
)
{
log
.
info
(
"未找到该job或者job已经下线{}"
,
jobName
);
}
else
{
jobScheduleController
.
shutdown
();
JobManager
jobManager
=
jobManagerMapper
.
findByName
(
jobName
);
if
(
jobManager
!=
null
)
{
jobManager
.
setDelFlag
(-
1
);
jobManager
.
setUpdateTime
(
new
Date
());
jobManagerMapper
.
updateByPrimaryKeySelective
(
jobManager
);
}
}
}
/**
* 扫描搜索下线的服务
*/
public
void
scanAddJob
()
{
List
<
JobManager
>
jobManagers
=
jobManagerMapper
.
selectAll
();
if
(
CollectionUtils
.
isNotEmpty
(
jobManagers
))
{
for
(
JobManager
jobManager
:
jobManagers
)
{
String
className
=
jobManager
.
getClassName
();
try
{
Class
<?>
aClass
=
Class
.
forName
(
className
);
create
(
jobManager
.
getJobName
(),
jobManager
.
getCron
(),
jobManager
.
getShareTotalCount
(),
(
SimpleJob
)
aClass
.
newInstance
(),
jobManager
.
getParameters
());
}
catch
(
ClassNotFoundException
|
InstantiationException
|
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
}
platform-process/src/main/java/cn/com/glsx/process/job/DynamicTaskService.java
View file @
294ce56
package
cn
.
com
.
glsx
.
process
.
job
;
import
java.util.List
;
import
javax.annotation.Resource
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
...
...
@@ -15,6 +18,8 @@ import com.dangdang.ddframe.job.lite.internal.schedule.JobScheduleController;
import
com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler
;
import
com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter
;
import
cn.com.glsx.process.modules.entity.JobManager
;
import
cn.com.glsx.process.modules.mapper.JobManagerMapper
;
import
lombok.extern.slf4j.Slf4j
;
@Slf4j
...
...
@@ -27,6 +32,9 @@ public class DynamicTaskService {
@Resource
private
ElasticJobListener
elasticJobListener
;
@Autowired
private
JobManagerMapper
jobManagerMapper
;
private
static
LiteJobConfiguration
.
Builder
simpleJobConfigBuilder
(
String
jobName
,
Class
<?
extends
SimpleJob
>
jobClass
,
int
shardTotalCount
,
String
cron
,
String
description
,
String
param
)
{
return
LiteJobConfiguration
.
newBuilder
(
new
SimpleJobConfiguration
(
...
...
@@ -98,6 +106,7 @@ public class DynamicTaskService {
public
boolean
isJobStoped
(
String
jobName
)
{
JobScheduleController
schedule
=
JobRegistry
.
getInstance
().
getJobScheduleController
(
jobName
);
if
(
schedule
==
null
)
{
log
.
info
(
"【动态任务】{},状态:任务不存在!"
,
jobName
);
return
true
;
...
...
@@ -106,4 +115,23 @@ public class DynamicTaskService {
return
schedule
.
isPaused
();
}
}
/**
* 扫描搜索下线的服务
*/
public
void
scanAddJob
()
{
List
<
JobManager
>
jobManagers
=
jobManagerMapper
.
selectAll
();
if
(
CollectionUtils
.
isNotEmpty
(
jobManagers
))
{
for
(
JobManager
jobManager
:
jobManagers
)
{
String
className
=
jobManager
.
getClassName
();
try
{
Class
<?>
aClass
=
Class
.
forName
(
className
);
create
(
jobManager
.
getJobName
(),
jobManager
.
getCron
(),
jobManager
.
getShareTotalCount
(),
(
SimpleJob
)
aClass
.
newInstance
(),
""
,
jobManager
.
getParameters
());
}
catch
(
ClassNotFoundException
|
InstantiationException
|
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
}
}
platform-process/src/main/java/cn/com/glsx/process/modules/controller/TestJobController.java
View file @
294ce56
package
cn
.
com
.
glsx
.
process
.
modules
.
controller
;
import
cn.com.glsx.process.job.DynamicTaskAdd
;
import
cn.com.glsx.process.job.instance.DynamicJob
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.glsx.plat.core.annotation.ResponseResultBody
;
import
cn.com.glsx.process.job.DynamicTaskService
;
import
cn.com.glsx.process.job.instance.DynamicJob
;
import
io.swagger.v3.oas.annotations.Operation
;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
@Slf4j
@Tag
(
name
=
"动态任务(Demo)"
)
...
...
@@ -17,13 +23,13 @@ import org.springframework.web.bind.annotation.*;
public
class
TestJobController
{
@Autowired
private
DynamicTaskAdd
dynamicTaskAdd
;
private
DynamicTaskService
dynamicTaskAdd
;
@GetMapping
(
"/jobtest"
)
@Operation
(
summary
=
"动态任务添加"
,
description
=
"动态任务添加"
)
public
void
addtest
(
@RequestParam
(
"cron"
)
String
cron
,
@RequestParam
(
"jobName"
)
String
jobName
)
{
// cron表达式
dynamicTaskAdd
.
create
(
jobName
,
cron
,
1
,
new
DynamicJob
()
,
"test"
);
dynamicTaskAdd
.
create
(
jobName
,
cron
,
1
,
new
DynamicJob
(),
"test"
,
"test"
);
}
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment