新增task

This commit is contained in:
启航 2024-03-07 10:19:24 +08:00
parent 6d62c7eae1
commit b0e6cfc049
14 changed files with 401 additions and 80 deletions

View File

@ -1,4 +1,4 @@
package com.qihang.sys.api.task.service; package com.qihang.common.task;
public interface IPollableService { public interface IPollableService {
/** /**

View File

@ -1,6 +1,5 @@
package com.qihang.sys.api.task.core; package com.qihang.common.task;
import com.qihang.sys.api.task.service.IPollableService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;

View File

@ -0,0 +1,176 @@
package com.qihang.jd.domain;
import java.io.Serializable;
import java.util.Date;
/**
*
* @TableName sys_task
*/
public class SysTask implements Serializable {
/**
*
*/
private Integer id;
/**
*
*/
private String taskName;
/**
*
*/
private String cron;
/**
*
*/
private String method;
/**
*
*/
private String remark;
/**
*
*/
private Date createTime;
private static final long serialVersionUID = 1L;
/**
*
*/
public Integer getId() {
return id;
}
/**
*
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
*/
public String getTaskName() {
return taskName;
}
/**
*
*/
public void setTaskName(String taskName) {
this.taskName = taskName;
}
/**
*
*/
public String getCron() {
return cron;
}
/**
*
*/
public void setCron(String cron) {
this.cron = cron;
}
/**
*
*/
public String getMethod() {
return method;
}
/**
*
*/
public void setMethod(String method) {
this.method = method;
}
/**
*
*/
public String getRemark() {
return remark;
}
/**
*
*/
public void setRemark(String remark) {
this.remark = remark;
}
/**
*
*/
public Date getCreateTime() {
return createTime;
}
/**
*
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
SysTask other = (SysTask) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getTaskName() == null ? other.getTaskName() == null : this.getTaskName().equals(other.getTaskName()))
&& (this.getCron() == null ? other.getCron() == null : this.getCron().equals(other.getCron()))
&& (this.getMethod() == null ? other.getMethod() == null : this.getMethod().equals(other.getMethod()))
&& (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getTaskName() == null) ? 0 : getTaskName().hashCode());
result = prime * result + ((getCron() == null) ? 0 : getCron().hashCode());
result = prime * result + ((getMethod() == null) ? 0 : getMethod().hashCode());
result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", taskName=").append(taskName);
sb.append(", cron=").append(cron);
sb.append(", method=").append(method);
sb.append(", remark=").append(remark);
sb.append(", createTime=").append(createTime);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,18 @@
package com.qihang.jd.mapper;
import com.qihang.jd.domain.SysTask;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author TW
* @description 针对表sys_task的数据库操作Mapper
* @createDate 2024-03-07 10:15:38
* @Entity com.qihang.jd.domain.SysTask
*/
public interface SysTaskMapper extends BaseMapper<SysTask> {
}

View File

@ -0,0 +1,13 @@
package com.qihang.jd.service;
import com.qihang.jd.domain.SysTask;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author TW
* @description 针对表sys_task的数据库操作Service
* @createDate 2024-03-07 10:15:38
*/
public interface SysTaskService extends IService<SysTask> {
}

View File

@ -0,0 +1,22 @@
package com.qihang.jd.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qihang.jd.domain.SysTask;
import com.qihang.jd.service.SysTaskService;
import com.qihang.jd.mapper.SysTaskMapper;
import org.springframework.stereotype.Service;
/**
* @author TW
* @description 针对表sys_task的数据库操作Service实现
* @createDate 2024-03-07 10:15:38
*/
@Service
public class SysTaskServiceImpl extends ServiceImpl<SysTaskMapper, SysTask>
implements SysTaskService{
}

View File

@ -0,0 +1,44 @@
package com.qihang.jd.task;
import com.qihang.common.task.SchedulingConfiguration;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicBoolean;
@Component
public class CronTaskLoader implements ApplicationRunner {
// private static final Logger log = LoggerFactory.getLogger(CronTaskLoader.class);
private final SchedulingConfiguration schedulingConfiguration;
private final AtomicBoolean appStarted = new AtomicBoolean(false);
private final AtomicBoolean initializing = new AtomicBoolean(false);
public CronTaskLoader(SchedulingConfiguration schedulingConfiguration) {
this.schedulingConfiguration = schedulingConfiguration;
}
/**
* 定时任务配置刷新(1分钟刷新一次)
*/
@Scheduled(fixedDelay = 600000)
public void cronTaskConfigRefresh() {
if (appStarted.get() && initializing.compareAndSet(false, true)) {
// log.info("定时调度任务动态加载开始>>>>>>");
try {
schedulingConfiguration.refresh();
} finally {
initializing.set(false);
}
// log.info("定时调度任务动态加载结束<<<<<<");
}
}
@Override
public void run(ApplicationArguments args) {
if (appStarted.compareAndSet(false, true)) {
cronTaskConfigRefresh();
}
}
}

View File

@ -0,0 +1,26 @@
package com.qihang.jd.task;
import com.qihang.common.task.IPollableService;
import com.qihang.jd.domain.SysTask;
import com.qihang.jd.service.SysTaskService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@AllArgsConstructor
@Service
public class OrderTask implements IPollableService {
private final SysTaskService taskService;
@Override
public void poll() {
System.out.printf("更新JD订单%s","echo");
}
@Override
public String getCronExpression() {
SysTask task = taskService.getById(1);
// return "0/1 * * * * ?";
return task.getCron();
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qihang.jd.mapper.SysTaskMapper">
<resultMap id="BaseResultMap" type="com.qihang.jd.domain.SysTask">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="taskName" column="task_name" jdbcType="VARCHAR"/>
<result property="cron" column="cron" jdbcType="VARCHAR"/>
<result property="method" column="method" jdbcType="VARCHAR"/>
<result property="remark" column="remark" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,task_name,cron,
method,remark,create_time
</sql>
</mapper>

View File

@ -26,7 +26,9 @@ public class HomeController {
} }
@GetMapping(value = "/echo-feign") @GetMapping(value = "/echo-feign")
public String feign() { public String feign() {
return echoService.echo(); String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjdkOTBmN2EzLWUwNWQtNDkxNy04NjIzLTU1OGRhNGY3NjE3NiJ9._Oukm9b0P1WvcOywLdhs6_BOt_6mRSF41Q6f4fBm_DGUkPR86Qg1tqyRTM5ouTR2Xz46IRuRAVez8Wcl3NIlwg";
return echoService.echo(token);
} }
} }

View File

@ -2,10 +2,11 @@ package com.qihang.sys.api.feign;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
@FeignClient(name = "tao-api") @FeignClient(name = "tao-api")
public interface EchoService { public interface EchoService {
@GetMapping(value = "/test/na") @GetMapping(value = "/test/na")
String echo(); String echo(@RequestHeader(name = "Authorization",required = true) String Token);
} }

View File

@ -1,30 +0,0 @@
package com.qihang.sys.api.task;
import com.qihang.sys.api.domain.SysTask;
import com.qihang.sys.api.feign.EchoService;
import com.qihang.sys.api.service.SysTaskService;
import com.qihang.sys.api.task.service.IPollableService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderTaskJD implements IPollableService {
@Autowired
private SysTaskService taskService;
@Resource
private EchoService echoService;
@Override
public void poll() {
String echo = echoService.echo();
System.out.printf("更新京东订单%s",echo);
}
@Override
public String getCronExpression() {
SysTask task = taskService.getById(2);
// return "0/1 * * * * ?";
return task.getCron();
}
}

View File

@ -0,0 +1,31 @@
//package com.qihang.sys.api.task;
//
//import com.qihang.sys.api.domain.SysTask;
//import com.qihang.sys.api.feign.EchoService;
//import com.qihang.sys.api.service.SysTaskService;
//import com.qihang.common.task.IPollableService;
//import jakarta.annotation.Resource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//
//@Service
//public class OrderTaskTao implements IPollableService {
// @Autowired
// private SysTaskService taskService;
// @Resource
// private EchoService echoService;
// @Override
// public void poll() {
// String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjdkOTBmN2EzLWUwNWQtNDkxNy04NjIzLTU1OGRhNGY3NjE3NiJ9._Oukm9b0P1WvcOywLdhs6_BOt_6mRSF41Q6f4fBm_DGUkPR86Qg1tqyRTM5ouTR2Xz46IRuRAVez8Wcl3NIlwg";
// String echo = echoService.echo(token);
// System.out.printf("更新淘宝订单%s",echo);
// }
//
// @Override
// public String getCronExpression() {
// SysTask task = taskService.getById(1);
//// return "0/1 * * * * ?";
// return task.getCron();
// }
//}

View File

@ -1,45 +1,44 @@
package com.qihang.sys.api.task.core; //package com.qihang.sys.api.task.core;
//
import org.slf4j.Logger; //import com.qihang.common.task.SchedulingConfiguration;
import org.slf4j.LoggerFactory; //import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationArguments; //import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.ApplicationRunner; //import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Scheduled; //import org.springframework.stereotype.Component;
import org.springframework.stereotype.Component; //
//import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicBoolean; //
//@Component
@Component //public class CronTaskLoader implements ApplicationRunner {
public class CronTaskLoader implements ApplicationRunner { //// private static final Logger log = LoggerFactory.getLogger(CronTaskLoader.class);
// private static final Logger log = LoggerFactory.getLogger(CronTaskLoader.class); // private final SchedulingConfiguration schedulingConfiguration;
private final SchedulingConfiguration schedulingConfiguration; // private final AtomicBoolean appStarted = new AtomicBoolean(false);
private final AtomicBoolean appStarted = new AtomicBoolean(false); // private final AtomicBoolean initializing = new AtomicBoolean(false);
private final AtomicBoolean initializing = new AtomicBoolean(false); //
// public CronTaskLoader(SchedulingConfiguration schedulingConfiguration) {
public CronTaskLoader(SchedulingConfiguration schedulingConfiguration) { // this.schedulingConfiguration = schedulingConfiguration;
this.schedulingConfiguration = schedulingConfiguration; // }
} //
// /**
/** // * 定时任务配置刷新(1分钟刷新一次)
* 定时任务配置刷新(1分钟刷新一次) // */
*/ // @Scheduled(fixedDelay = 600000)
@Scheduled(fixedDelay = 600000) // public void cronTaskConfigRefresh() {
public void cronTaskConfigRefresh() { // if (appStarted.get() && initializing.compareAndSet(false, true)) {
if (appStarted.get() && initializing.compareAndSet(false, true)) { //// log.info("定时调度任务动态加载开始>>>>>>");
// log.info("定时调度任务动态加载开始>>>>>>"); // try {
try { // schedulingConfiguration.refresh();
schedulingConfiguration.refresh(); // } finally {
} finally { // initializing.set(false);
initializing.set(false); // }
} //// log.info("定时调度任务动态加载结束<<<<<<");
// log.info("定时调度任务动态加载结束<<<<<<"); // }
} // }
} //
// @Override
@Override // public void run(ApplicationArguments args) {
public void run(ApplicationArguments args) { // if (appStarted.compareAndSet(false, true)) {
if (appStarted.compareAndSet(false, true)) { // cronTaskConfigRefresh();
cronTaskConfigRefresh(); // }
} // }
} //}
}