Skip to content

Commit

Permalink
bug--修复定时任务点击启动重复增加问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wb9191 committed Oct 9, 2020
1 parent 887be30 commit 0a7faa5
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import co.yixiang.domain.BaseDomain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.sql.Timestamp;
import lombok.EqualsAndHashCode;

/**
* @author hupeng
* @date 2020-05-13
*/

@Data
@EqualsAndHashCode(callSuper = true)
@TableName("quartz_job")
public class QuartzJob implements Serializable {
public class QuartzJob extends BaseDomain {

public static final String JOB_KEY = "JOB_KEY";

Expand Down Expand Up @@ -61,9 +59,6 @@ public class QuartzJob implements Serializable {
private String remark;


/** 创建日期 */
@TableField(fill= FieldFill.INSERT)
private Timestamp createTime;

public void copy(QuartzJob source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import co.yixiang.domain.BaseDomain;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.sql.Timestamp;

/**
* @author hupeng
* @date 2020-05-13
*/

@Data
@TableName("quartz_log")
public class QuartzLog implements Serializable {
public class QuartzLog extends BaseDomain {

/** 任务日志ID */
@TableId
Expand All @@ -35,10 +31,6 @@ public class QuartzLog implements Serializable {
private String baenName;


/** 创建时间 */
@TableField(fill= FieldFill.INSERT)
private Timestamp createTime;


/** cron表达式 */
private String cronExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ResponseEntity<Object> getJobs(QuartzJobQueryCriteria criteria, Pageable
@GetMapping(value = "/download")
@PreAuthorize("@el.check('admin','timing:list')")
public void download(HttpServletResponse response, QuartzJobQueryCriteria criteria) throws IOException {
quartzJobService.download(generator.convert(quartzJobService.queryAll(criteria), QuartzJobDto.class), response);
quartzJobService.download(generator.convert(quartzJobService.queryAll(criteria),QuartzJobDto.class), response);
}

@Log("导出日志数据")
Expand All @@ -93,7 +93,6 @@ public ResponseEntity<Object> getJobLogs(QuartzLogQueryCriteria criteria, Pageab
@PostMapping
@PreAuthorize("@el.check('admin','timing:add')")
public ResponseEntity<Object> create(@Validated @RequestBody QuartzJob resources){

if (resources.getId() != null) {
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
}
Expand All @@ -105,17 +104,14 @@ public ResponseEntity<Object> create(@Validated @RequestBody QuartzJob resources
@PutMapping
@PreAuthorize("@el.check('admin','timing:edit')")
public ResponseEntity<Object> update(@Validated @RequestBody QuartzJob resources){

quartzJobService.updateById(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Log("更改定时任务状态")
@ApiOperation("更改定时任务状态")
@PutMapping(value = "/{id}")
@PreAuthorize("@el.check('admin','timing:edit')")
public ResponseEntity<Object> updateIsPause(@PathVariable Long id){

quartzJobService.updateIsPause(quartzJobService.getOne(new LambdaQueryWrapper<QuartzJob>()
.eq(QuartzJob::getId,id)));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
Expand All @@ -126,7 +122,6 @@ public ResponseEntity<Object> updateIsPause(@PathVariable Long id){
@PutMapping(value = "/exec/{id}")
@PreAuthorize("@el.check('admin','timing:edit')")
public ResponseEntity<Object> execution(@PathVariable Long id){

quartzJobService.execution(quartzJobService.getOne(new QueryWrapper<QuartzJob>().eq("id",id)));
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Expand All @@ -136,7 +131,6 @@ public ResponseEntity<Object> execution(@PathVariable Long id){
@DeleteMapping
@PreAuthorize("@el.check('admin','timing:del')")
public ResponseEntity<Object> delete(@RequestBody Integer[] ids){

quartzJobService.removeByIds(new ArrayList<>(Arrays.asList(ids)));
return new ResponseEntity<>(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@
import java.util.Map;

/**
* @author hupeng
* @date 2020-05-13
*/
* @author hupeng
* @date 2020-05-13
*/
public interface QuartzJobService extends BaseService<QuartzJob>{

/**
* 查询数据分页
* @param criteria 条件
* @param pageable 分页参数
* @return Map<String,Object>
*/
/**
* 查询数据分页
* @param criteria 条件
* @param pageable 分页参数
* @return Map<String,Object>
*/
Map<String,Object> queryAll(QuartzJobQueryCriteria criteria, Pageable pageable);

/**
* 查询所有数据不分页
* @param criteria 条件参数
* @return List<QuartzJobDto>
*/
* 查询所有数据不分页
* @param criteria 条件参数
* @return List<QuartzJobDto>
*/
List<QuartzJob> queryAll(QuartzJobQueryCriteria criteria);

/**
* 导出数据
* @param all 待导出的数据
* @param response /
* @throws IOException /
*/
* 导出数据
* @param all 待导出的数据
* @param response /
* @throws IOException /
*/
void download(List<QuartzJobDto> all, HttpServletResponse response) throws IOException;


Expand All @@ -64,4 +64,5 @@ public interface QuartzJobService extends BaseService<QuartzJob>{
* @return List
*/
List<QuartzJob> findByIsPauseIsFalse();
void removeByIds(List<Integer> idList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2020-05-13
*/
* @author hupeng
* @date 2020-05-13
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "quartzJob")
Expand All @@ -63,7 +63,7 @@ public Map<String, Object> queryAll(QuartzJobQueryCriteria criteria, Pageable pa

@Override
//@Cacheable
public List<QuartzJob> queryAll(QuartzJobQueryCriteria criteria){
public List<QuartzJob> queryAll(QuartzJobQueryCriteria criteria) {
return baseMapper.selectList(QueryHelpPlus.getPredicate(QuartzJob.class, criteria));
}

Expand All @@ -72,7 +72,7 @@ public List<QuartzJob> queryAll(QuartzJobQueryCriteria criteria){
public void download(List<QuartzJobDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (QuartzJobDto quartzJob : all) {
Map<String,Object> map = new LinkedHashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("Spring Bean名称", quartzJob.getBeanName());
map.put("cron 表达式", quartzJob.getCronExpression());
map.put("状态:1暂停、0启用", quartzJob.getIsPause());
Expand Down Expand Up @@ -109,7 +109,7 @@ public void download(List<QuartzJobDto> all, HttpServletResponse response) throw
*/
@Override
public void updateIsPause(QuartzJob quartzJob) {
if(quartzJob.getId().equals(1L)){
if (quartzJob.getId().equals(1L)) {
throw new BadRequestException("该任务不可操作");
}
if (quartzJob.getIsPause()) {
Expand All @@ -124,19 +124,13 @@ public void updateIsPause(QuartzJob quartzJob) {

@Override
public boolean save(QuartzJob quartzJob) {
if (quartzJob.getIsPause()) {
quartzManage.pauseJob(quartzJob);
}
quartzManage.addJob(quartzJob);
return retBool(baseMapper.insert(quartzJob));
}

@Override
public boolean updateById(QuartzJob quartzJob) {
if (quartzJob.getIsPause()) {
quartzManage.pauseJob(quartzJob);
} else {
quartzManage.resumeJob(quartzJob);
}
quartzManage.updateJobCron(quartzJob);
return retBool(baseMapper.updateById(quartzJob));
}

Expand All @@ -147,7 +141,7 @@ public boolean updateById(QuartzJob quartzJob) {
*/
@Override
public void execution(QuartzJob quartzJob) {
if(quartzJob.getId().equals(1L)){
if (quartzJob.getId().equals(1L)) {
throw new BadRequestException("该任务不可操作");
}
quartzManage.runJobNow(quartzJob);
Expand All @@ -164,4 +158,13 @@ public List<QuartzJob> findByIsPauseIsFalse() {
criteria.setIsPause(false);
return baseMapper.selectList(QueryHelpPlus.getPredicate(QuartzJob.class, criteria));
}

@Override
public void removeByIds(List<Integer> idList) {
idList.forEach(id -> {
QuartzJob quartzJob = baseMapper.selectById(id);
quartzManage.deleteJob(quartzJob);
});
baseMapper.deleteBatchIds(idList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.quartz.domain.QuartzJob;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
* @author hupeng
* @date 2020-05-13
*/
@Repository
@Mapper
public interface QuartzJobMapper extends CoreMapper<QuartzJob> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.quartz.domain.QuartzLog;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
* @author hupeng
* @date 2020-05-13
*/
@Repository
@Mapper
public interface QuartzLogMapper extends CoreMapper<QuartzLog> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@
import co.yixiang.exception.BadRequestException;
import co.yixiang.modules.quartz.domain.QuartzJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.*;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.stereotype.Component;

Expand Down

0 comments on commit 0a7faa5

Please sign in to comment.