Spring Boot定时任务(含异步)
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjsasc.dao.GYSCheckAndReturnDAO;
import com.bjsasc.entity.ClearGysInfo;
import com.bjsasc.entity.DirectoryGysRel;
import com.bjsasc.entity.ReturnEntity;
import com.bjsasc.entity.ScoreYear;
import com.bjsasc.web.result.ResultCode;
import com.bjsasc.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@RestController
@EnableScheduling
@EnableAsync
@Api(value = "/clearAPI", tags = {"供应商三年一清"})
@RequestMapping(value = "/1.0")
public class GYSCheckAndReturnController {
private static final Logger LOG = LoggerFactory.getLogger(GYSCheckAndReturnController.class);
@Autowired
private GYSCheckAndReturnDAO gysCheckAndReturnDAO;
@Async
@Scheduled(cron = "0 0 6 * * ?")
@ApiOperation(value = "系统添加清退供应商(定时任务:每天凌晨6点执行一次)")
@RequestMapping(method = RequestMethod.POST)
public void sysAddClearRecord() {
LOG.info("系统定时任务添加清退供应商start...");
List<DirectoryGysRel> directoryList = gysCheckAndReturnDAO.getClearDirectory();
for (DirectoryGysRel dire : directoryList) {
String gysid = dire.getGysid();
List<ScoreYear> scoreYearList = gysCheckAndReturnDAO.getScoreYear(gysid);
double sum = 0;
ClearGysInfo clearGYS = new ClearGysInfo(gysid);
for (ScoreYear scoreYear : scoreYearList) {
clearGYS.setGys_dept_id(scoreYear.getGYS_DEPT_ID());
Double score = Double.valueOf(scoreYear.getSCORE());
sum += score;
}
double avgScore = sum/3.0;
if(avgScore < 60) {
clearGYS.setScore(avgScore);
clearGYS.setScore_detail("近三年评分的平均分小于60分");
gysCheckAndReturnDAO.sysAddClearRecord(clearGYS);
}
}
LOG.info("系统定时任务添加清退供应商end...");
}
}