首先需要在application 中配置注解 @EnableScheduling开启定时器
初始化定时任务周期 就是去数据库查询初始配置的定时任务,如果执行过程中有结果或者没有结果都可以对数据库表进行修改,然后下次再按修改后的时间执行任务
附上数据库表结构
package com.jiaotongbu.api.common;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiaotongbu.api.controller.BaseController;
import com.jiaotongbu.api.entity.ApiCorn;
import com.jiaotongbu.api.entity.ApiHalfYearReport;
import com.jiaotongbu.api.service.ApiCornService;
import com.jiaotongbu.api.service.ApiHalfYearReportService;
import com.jiaotongbu.api.util.RestfulUtilNew;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class HalfYearTask extends BaseController implements SchedulingConfigurer {
private static Logger logger = Logger.getLogger(HalfYearTask.class);
private static final String corn="0 0 0 1 1,7 ? ";
@Autowired
private ApiHalfYearReportService apiHalfYearReportService;
@Autowired
private ApiCornService apiCornService;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(() ->{
try {
LocalDateTime time=LocalDateTime.now();
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String strDate2 = dtf2.format(time);
logger.info("开始执行半年报..."+strDate2);
Map<String, String> map = new HashMap<>();
LocalDateTime now = LocalDateTime.now();
// 1月1号往前推6个月 然后获取那个时间的年
LocalDateTime localDateTime1 = now.minusMonths(6);
String type="00"; //00:代表上半年;01代表下半年
if (7<=localDateTime1.getMonthValue()){
type="01";
}
map.put("year",String.valueOf(localDateTime1.getYear()));
map.put("type",type);
map.put("identity",identity);
String jsonMap = JSONArray.toJSONString(map);
String resultStr = RestfulUtilNew.sendMd5JsonPost(halfYearReportUrl, jsonMap);
JSONObject jsonObject = JSONArray.parseObject(resultStr);
String code = jsonObject.getString("code");
String data = jsonObject.getString("data");
JSONArray objects = JSONArray.parseArray(data);
ApiCorn apiCorn = apiCornService.queryById(1);
// 如果data中没有值 在次日继续访问, 如果有值 则设置为 半年取一次。
if (objects.size()<=0){
LocalDateTime localDateTime = now.plusDays(1);
int dayOfMonth = localDateTime.getDayOfMonth();
int monthValue = localDateTime.getMonthValue();
String cornvalue = "0 0 0 "+dayOfMonth+" "+monthValue+" ?";
apiCorn.setCorn(cornvalue);
}else{
if(!corn.equals(apiCorn.getCorn())){
apiCorn.setCorn(corn);
}
}
apiCornService.update(apiCorn);
logger.info("半年报共返回"+objects.size()+"条数据");
if (ApiResultStatusEnum.SUCCESS.value() == (Integer.parseInt(code))){
logger.info(ApiResultStatusEnum.SUCCESS.name());
for (int i = 0; i < objects.size(); i++) {
String jsonString = objects.getJSONObject(i).toJSONString();
logger.info("第"+i+1+"条数据:"+jsonString);
ApiHalfYearReport apiHalfYearReport = JSONArray.parseObject(jsonString, ApiHalfYearReport.class);
apiHalfYearReportService.insert(apiHalfYearReport);
}
logger.info("半年报执行完毕...");
}else{
ApiResultStatusEnum error = ApiResultStatusEnum.getByValue(Integer.parseInt(code));
logger.error("半年报执行失败:"+error.getName());
}
}catch (Exception e){
e.printStackTrace();
logger.error("半年报执行失败:"+e.getMessage());
}
},triggerContext -> {
// 初始化定时任务周期
ApiCorn apiCorn = apiCornService.queryById(1);
CronTrigger trigger = new CronTrigger(apiCorn.getCorn());
return trigger.nextExecutionTime(triggerContext);
});
}
}
标签:springboot,jiaotongbu,创建,springframework,api,import,org,定时,com
From: https://blog.51cto.com/u_16163393/6500185