0
点赞
收藏
分享

微信扫一扫

Spring Boot定时任务(串行、异步)


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;

/**
* 供应商三年一清Controller
*
* @author XIAO
*
*/
@RestController
@EnableScheduling // 1.开启定时任务
@EnableAsync // 2.开启异步
@Api(value = "/clearAPI", tags = {"供应商三年一清"})
@RequestMapping(value = "/1.0")
public class GYSCheckAndReturnController {
private static final Logger LOG = LoggerFactory.getLogger(GYSCheckAndReturnController.class);

@Autowired
private GYSCheckAndReturnDAO gysCheckAndReturnDAO;

/**
* 系统添加清退供应商(定时任务:每天凌晨6点执行一次)
* 条件:入名录时间满三年+近三年平均分<60分的供应商
* @return
* @throws Exception
*/
//@Scheduled(cron = "*/5 * * * * ?")//每隔2秒执行一次
@Async //异步
@Scheduled(cron = "0 0 6 * * ?")//每天凌晨6点执行一次
@ApiOperation(value = "系统添加清退供应商(定时任务:每天凌晨6点执行一次)")
@RequestMapping(method = RequestMethod.POST)
public void sysAddClearRecord() {
LOG.info("系统定时任务添加清退供应商start...");
//1.从名录中获取满足清退条件(入名录时间满三年)的供应商
List<DirectoryGysRel> directoryList = gysCheckAndReturnDAO.getClearDirectory();
//2.比对清退条件
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) {//比对标准(近三年平均分小于60分的进行清退)
//3.添加满足条件的供应商进入清退表
clearGYS.setScore(avgScore);
clearGYS.setScore_detail("近三年评分的平均分小于60分");
gysCheckAndReturnDAO.sysAddClearRecord(clearGYS);
}
}
LOG.info("系统定时任务添加清退供应商end...");
}

}


举报

相关推荐

0 条评论