配置类
package com.neusoft.mid.c3t.unite.manage.operate.config;
import com.neusoft.mid.c3t.unite.manage.operate.task.CyDataTask;
import com.neusoft.mid.c3t.unite.manage.operate.task.CyQueryTask;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author zc
* date 2020-08-14
* @description
*/
@Configuration
public class QuartzConfig {
private final CronConfig cron;
@Autowired
public QuartzConfig(CronConfig cron) {
this.cron = cron;
}
@Bean
public JobDetail cyDataTaskDetail() {
return JobBuilder.newJob(CyDataTask.class).withIdentity(CyDataTask.class.getName()).storeDurably().build();
}
@Bean
public org.quartz.Trigger cyDataTaskTrigger() {
return TriggerBuilder.newTrigger().forJob(cyDataTaskDetail())
.withIdentity(CyDataTask.class.getName())
.withSchedule(CronScheduleBuilder.cronSchedule(cron.getCy_upload()))
.build();
}
@Bean
public JobDetail cyQueryTaskDetail(){
return JobBuilder.newJob(CyQueryTask.class).withIdentity(CyQueryTask.class.getName()).storeDurably().build();
}
@Bean
public org.quartz.Trigger cyQueryTaskTrigger(){
return TriggerBuilder.newTrigger().forJob(cyQueryTaskDetail())
.withIdentity(CyQueryTask.class.getName())
.withSchedule(CronScheduleBuilder.cronSchedule(cron.getCy_query()))
.build();
}
}
任务类
1、继承QuartzJobBean
2、重写executeInternal
package com.neusoft.mid.c3t.unite.manage.operate.task;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Stopwatch;
import com.neusoft.mid.c3t.unite.manage.operate.beans.query.CyApplicationBean;
import com.neusoft.mid.c3t.unite.manage.operate.beans.query.CyPodBean;
import com.neusoft.mid.c3t.unite.manage.operate.config.CronConfig;
import com.neusoft.mid.c3t.unite.manage.operate.config.ProvinceConfig;
import com.neusoft.mid.c3t.unite.manage.operate.config.TokenConfig;
import com.neusoft.mid.c3t.unite.manage.operate.config.UrlConfig;
import com.neusoft.mid.c3t.unite.manage.operate.mapper.CyDataMapper;
import com.neusoft.mid.c3t.unite.manage.operate.service.CyQueryService;
import com.neusoft.mid.c3t.unite.manage.operate.untils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.StringUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author zc
* date 2021-05-10
* @description
*/
@Service
@Slf4j
@DisallowConcurrentExecution
public class CyQueryTask extends QuartzJobBean {
@Autowired
private CronConfig cronConfig;
@Autowired
private ProvinceConfig provinceConfig;
@Autowired
private CyQueryService cyQueryService;
@Autowired
private TokenConfig tokenConfig;
@Autowired
private ObjectMapper mapper;
@Autowired
private HttpUtil httpUtil;
@Autowired
private OkHttpClient okHttpClient;
@Autowired
private UrlConfig urlConfig;
@Autowired
private CyDataMapper cyDataMapper;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
if(cronConfig.getIs_query()){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String startDate = dateTimeFormatter.format(LocalDateTime.now());
Stopwatch stopwatch = Stopwatch.createStarted();
log.info(">>>>>> 定时任务查询开始, 时间:{} <<<<<<", startDate);
String token_url = urlConfig.getToken();
String username = tokenConfig.getUsername();
String password = tokenConfig.getPassword();
String authorization = tokenConfig.getAuthorization();
String token = httpUtil.token(okHttpClient, token_url, mapper, username, password, authorization);
if(StringUtils.isNotBlank(token)){
//组织架构
cyQueryService.queryCyOrganizations(token);
//租户
cyQueryService.queryCyTenants(token);
//应用系统
cyQueryService.queryCyApplications(token);
//资源池
String districtCode = provinceConfig.getProvince();
cyQueryService.queryCyPods(districtCode,token);
//区域
cyQueryService.queryCyDistricts(token);
}
String endDate = dateTimeFormatter.format(LocalDateTime.now());
log.info(">>>>>> 定时任务查询结束, 时间:{} 耗时: {}s <<<<<<", endDate , stopwatch.elapsed(TimeUnit.SECONDS));
}
//更改为从数据库中取
List<CyApplicationBean> businesses = cyDataMapper.queryCyApplication();
List<CyPodBean> pools = cyDataMapper.queryCyPod();
//上报数据处理
cyQueryService.execute(businesses,pools);
}
}