commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.timer.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.bean.BeanUtil; |
|
28 |
import cn.hutool.core.collection.CollectionUtil; |
|
29 |
import cn.hutool.core.util.ObjectUtil; |
|
30 |
import cn.hutool.cron.CronUtil; |
|
31 |
import cn.hutool.extra.spring.SpringUtil; |
|
32 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
33 |
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
34 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
35 |
import org.springframework.stereotype.Service; |
|
36 |
import org.springframework.transaction.annotation.Transactional; |
|
37 |
import vip.xiaonuo.core.exception.ServiceException; |
|
38 |
import vip.xiaonuo.core.factory.PageFactory; |
|
39 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
40 |
import vip.xiaonuo.core.timer.TimerTaskRunner; |
|
41 |
import vip.xiaonuo.sys.modular.timer.entity.SysTimers; |
|
42 |
import vip.xiaonuo.sys.modular.timer.enums.TimerJobStatusEnum; |
|
43 |
import vip.xiaonuo.sys.modular.timer.enums.exp.SysTimersExceptionEnum; |
|
44 |
import vip.xiaonuo.sys.modular.timer.mapper.SysTimersMapper; |
|
45 |
import vip.xiaonuo.sys.modular.timer.param.SysTimersParam; |
|
46 |
import vip.xiaonuo.sys.modular.timer.service.SysTimersService; |
|
47 |
import vip.xiaonuo.sys.modular.timer.service.TimerExeService; |
|
48 |
|
|
49 |
import javax.annotation.Resource; |
|
50 |
import java.util.Collection; |
|
51 |
import java.util.List; |
|
52 |
import java.util.Map; |
|
53 |
import java.util.stream.Collectors; |
|
54 |
|
|
55 |
/** |
|
56 |
* 定时任务 服务实现类 |
|
57 |
* |
|
58 |
* @author yubaoshan |
|
59 |
* @date 2020/6/30 18:26 |
|
60 |
*/ |
|
61 |
@Service |
|
62 |
public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers> implements SysTimersService { |
|
63 |
|
|
64 |
@Resource |
|
65 |
private TimerExeService timerExeService; |
|
66 |
|
|
67 |
@Override |
|
68 |
public PageResult<SysTimers> page(SysTimersParam sysTimersParam) { |
|
69 |
|
|
70 |
// 构造条件 |
|
71 |
LambdaQueryWrapper<SysTimers> queryWrapper = new LambdaQueryWrapper<>(); |
|
72 |
|
|
73 |
if (ObjectUtil.isNotNull(sysTimersParam)) { |
|
74 |
|
|
75 |
// 拼接查询条件-任务名称 |
|
76 |
if (ObjectUtil.isNotEmpty(sysTimersParam.getTimerName())) { |
|
77 |
queryWrapper.like(SysTimers::getTimerName, sysTimersParam.getTimerName()); |
|
78 |
} |
|
79 |
|
|
80 |
// 拼接查询条件-状态(字典 1运行 2停止) |
|
81 |
if (ObjectUtil.isNotEmpty(sysTimersParam.getJobStatus())) { |
|
82 |
queryWrapper.eq(SysTimers::getJobStatus, sysTimersParam.getJobStatus()); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
// 查询分页结果 |
|
87 |
return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper)); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public List<SysTimers> list(SysTimersParam sysTimersParam) { |
|
92 |
|
|
93 |
// 构造条件 |
|
94 |
LambdaQueryWrapper<SysTimers> queryWrapper = new LambdaQueryWrapper<>(); |
|
95 |
|
|
96 |
if (ObjectUtil.isNotNull(sysTimersParam)) { |
|
97 |
|
|
98 |
// 拼接查询条件-任务名称 |
|
99 |
if (ObjectUtil.isNotEmpty(sysTimersParam.getTimerName())) { |
|
100 |
queryWrapper.like(SysTimers::getTimerName, sysTimersParam.getTimerName()); |
|
101 |
} |
|
102 |
|
|
103 |
// 拼接查询条件-状态(字典 1运行 2停止) |
|
104 |
if (ObjectUtil.isNotEmpty(sysTimersParam.getJobStatus())) { |
|
105 |
queryWrapper.eq(SysTimers::getJobStatus, sysTimersParam.getJobStatus()); |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
return this.list(queryWrapper); |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public void add(SysTimersParam sysTimersParam) { |
|
114 |
|
|
115 |
// 将dto转为实体 |
|
116 |
SysTimers sysTimers = new SysTimers(); |
|
117 |
BeanUtil.copyProperties(sysTimersParam, sysTimers); |
|
118 |
|
|
119 |
// 设置为停止状态,点击启动时启动任务 |
|
120 |
sysTimers.setJobStatus(TimerJobStatusEnum.STOP.getCode()); |
|
121 |
|
|
122 |
this.save(sysTimers); |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public void delete(SysTimersParam sysTimersParam) { |
|
127 |
|
|
128 |
// 先停止id为参数id的定时器 |
|
129 |
CronUtil.remove(String.valueOf(sysTimersParam.getId())); |
|
130 |
|
|
131 |
this.removeById(sysTimersParam.getId()); |
|
132 |
} |
|
133 |
|
|
134 |
@Transactional(rollbackFor = Exception.class) |
|
135 |
@Override |
|
136 |
public void edit(SysTimersParam sysTimersParam) { |
|
137 |
|
|
138 |
// 更新库中记录 |
|
139 |
SysTimers oldTimer = this.querySysTimers(sysTimersParam); |
|
140 |
// 查看被编辑的任务的状态 |
|
141 |
Integer jobStatus = oldTimer.getJobStatus(); |
|
142 |
BeanUtil.copyProperties(sysTimersParam, oldTimer); |
|
143 |
oldTimer.setJobStatus(jobStatus); |
|
144 |
this.updateById(oldTimer); |
|
145 |
|
|
146 |
// 如果任务正在运行,则停掉这个任务,重新运行任务 |
|
147 |
if (jobStatus.equals(TimerJobStatusEnum.RUNNING.getCode())) { |
|
148 |
CronUtil.remove(String.valueOf(oldTimer.getId())); |
|
149 |
timerExeService.startTimer( |
|
150 |
String.valueOf(sysTimersParam.getId()), |
|
151 |
sysTimersParam.getCron(), |
|
152 |
sysTimersParam.getActionClass()); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
@Override |
|
157 |
public SysTimers detail(SysTimersParam sysTimersParam) { |
|
158 |
return this.querySysTimers(sysTimersParam); |
|
159 |
} |
|
160 |
|
|
161 |
@Transactional(rollbackFor = Exception.class) |
|
162 |
@Override |
|
163 |
public void start(SysTimersParam sysTimersParam) { |
|
164 |
|
|
165 |
// 更新库中的状态 |
|
166 |
LambdaUpdateWrapper<SysTimers> wrapper = new LambdaUpdateWrapper<>(); |
|
167 |
wrapper.set(SysTimers::getJobStatus, TimerJobStatusEnum.RUNNING.getCode()) |
|
168 |
.eq(SysTimers::getId, sysTimersParam.getId()); |
|
169 |
this.update(wrapper); |
|
170 |
|
|
171 |
// 添加定时任务调度 |
|
172 |
SysTimers sysTimers = this.querySysTimers(sysTimersParam); |
|
173 |
timerExeService.startTimer(String.valueOf(sysTimers.getId()), sysTimers.getCron(), sysTimers.getActionClass()); |
|
174 |
} |
|
175 |
|
|
176 |
@Transactional(rollbackFor = Exception.class) |
|
177 |
@Override |
|
178 |
public void stop(SysTimersParam sysTimersParam) { |
|
179 |
|
|
180 |
// 更新库中的状态 |
|
181 |
LambdaUpdateWrapper<SysTimers> wrapper = new LambdaUpdateWrapper<>(); |
|
182 |
wrapper.set(SysTimers::getJobStatus, TimerJobStatusEnum.STOP.getCode()) |
|
183 |
.eq(SysTimers::getId, sysTimersParam.getId()); |
|
184 |
this.update(wrapper); |
|
185 |
|
|
186 |
// 关闭定时任务调度 |
|
187 |
SysTimers sysTimers = this.querySysTimers(sysTimersParam); |
|
188 |
timerExeService.stopTimer(String.valueOf(sysTimers.getId())); |
|
189 |
} |
|
190 |
|
|
191 |
@Override |
|
192 |
public List<String> getActionClasses() { |
|
193 |
Map<String, TimerTaskRunner> timerTaskRunnerMap = SpringUtil.getBeansOfType(TimerTaskRunner.class); |
|
194 |
if (ObjectUtil.isNotEmpty(timerTaskRunnerMap)) { |
|
195 |
Collection<TimerTaskRunner> values = timerTaskRunnerMap.values(); |
|
196 |
return values.stream().map(i -> i.getClass().getName()).collect(Collectors.toList()); |
|
197 |
} else { |
|
198 |
return CollectionUtil.newArrayList(); |
|
199 |
} |
|
200 |
} |
|
201 |
|
|
202 |
/** |
|
203 |
* 获取定时任务 |
|
204 |
* |
|
205 |
* @author yubaoshan |
|
206 |
* @date 2020/6/30 18:26 |
|
207 |
*/ |
|
208 |
private SysTimers querySysTimers(SysTimersParam sysTimersParam) { |
|
209 |
SysTimers sysTimers = this.getById(sysTimersParam.getId()); |
|
210 |
if (ObjectUtil.isEmpty(sysTimers)) { |
|
211 |
throw new ServiceException(SysTimersExceptionEnum.NOT_EXISTED); |
|
212 |
} |
|
213 |
return sysTimers; |
|
214 |
} |
|
215 |
|
|
216 |
} |