首先,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.打开定时任务publiclicc 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); }); }}