Quartz多线程并发定时任务

配置类

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);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值