大熊猫分布式组件开发系列教程(三)

今天我们来看看springboot定时任务如何做成分布式组件来供项目集成依赖使用,接下来就跟着大熊猫一起做crontask分布式组件开发。

首先我们先创建一个crontask模块

其实这个定时任务组件最主要的操作就是定时任务记录,以及定时任务日志这两张表

接着就是一些工厂类的封装,监听类的实现

编写JpJob实现Job的excute方法,以及编写执行之前的方法,执行后的方法。

package com.panda.common.crontask.web.schedule;
import com.panda.common.crontask.common.DateUtil;
import com.panda.common.crontask.service.api.QuartzConfigService;
import com.panda.common.crontask.service.api.QuartzLogService;
import com.panda.common.crontask.service.api.dto.QuartzConfigDto;
import com.panda.common.crontask.service.api.dto.QuartzLogDto;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.time.Duration;
import java.time.LocalDateTime;
public abstract class JpJob implements Job, Serializable {
    private static Logger logger = LoggerFactory.getLogger(JpJob.class);
    @Autowired
    private QuartzLogService quartzLogService;
    @Autowired
    private QuartzConfigService quartzConfigService;
    //任务开始时间
    private ThreadLocal<LocalDateTime> beforTime = new ThreadLocal<>();
    //日志Id
    private ThreadLocal<String> logId = new ThreadLocal<>();
    public abstract void runJob();
    public void beforeRun(){
        String clazz = this.getClass().getName();
        QuartzLogDto quartzLogDto = new QuartzLogDto();
        quartzLogDto.setClazz(clazz);
        QuartzConfigDto quartzConfigDto = quartzConfigService.findByClazz(clazz);
        if(quartzConfigDto != null){
            quartzLogDto.setQuartzId(quartzConfigDto.getId());
            quartzLogDto.setName(quartzConfigDto.getName());
        }
        quartzLogDto = quartzLogService.add(quartzLogDto);
        beforTime.set(LocalDateTime.now());
        logId.set(quartzLogDto.getId());
    }
    public void error(Exception e){
        QuartzLogDto quartzLogDto = quartzLogService.get(logId.get());
        Duration between = Duration.between(beforTime.get(), LocalDateTime.now());
        quartzLogDto.setSpendTime(between.toMillis());
        if(e != null && e.getMessage() != null)
            quartzLogDto.setExceptionMessage(e.getMessage().length() > 500 ? e.getMessage().substring(0, 499) : e.getMessage());
        quartzLogDto.setResult(0);
        quartzLogService.update(logId.get(), quartzLogDto);
    }
    public void afterRun(){
        QuartzLogDto quartzLogDto = quartzLogService.get(logId.get());
        Duration between = Duration.between(beforTime.get(), LocalDateTime.now());
        quartzLogDto.setSpendTime(between.toMillis());
        quartzLogDto.setResult(1);
        quartzLogService.update(logId.get(), quartzLogDto);
    }
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        String clazz = this.getClass().getName();
        logger.info("==== 定时任务 "+clazz+" ====> 开启 " + DateUtil.getStringToday());
        beforeRun();
        try {
            runJob();
        } catch (Exception e) {
            logger.info("==== 定时任务 "+clazz+" ====> 异常 "+e.getMessage());
            error(e);
        } finally {
            logger.info("==== 定时任务 "+clazz+" ====> 结束 " + DateUtil.getStringToday());
            afterRun();
        }
    }
}

编写JpJobFactory继承从而创建单例Job进行注入。

package com.panda.common.crontask.web.schedule;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component
public class JpJobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // 调用父类的方法
        Object jobInstance = super.createJobInstance(bundle);
        // 进行注入
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}

调度工厂类实现任务配置读取服务,项目启动激活所有定时任务,任务暂停,恢复,以及执行一次任务的方法。

package com.panda.common.crontask.web.schedule;
import com.panda.base.service.api.exception.ServiceException;
import com.panda.common.crontask.service.api.QuartzConfigService;
import com.panda.common.crontask.service.api.dto.QuartzConfigDto;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * 调度工厂类
 *
 */
@Service
@Component
public class JpSchedulerFactory {
    private static Logger logger = LoggerFactory.getLogger(JpSchedulerFactory.class);
    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;
    // 任务配置读取服务
    @Autowired
    private QuartzConfigService quartzConfigService;
    public void scheduleJobs() {
        Scheduler scheduler = getScheduler();
        startJob(scheduler);
    }
    // 获取scheduler
    private Scheduler getScheduler(){
       return schedulerFactoryBean.getScheduler();
    }
    // 项目启动,开启所有激活的任务
    private void startJob(Scheduler scheduler)  {
        try {
            // 获取所有激活的任务
            List<QuartzConfigDto> jobList = quartzConfigService.findByStatus(1);
            for (QuartzConfigDto config : jobList) {
                Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(config.getClazz());
                JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(config.getId()).build();
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(config.getCron());
                CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(config.getId())
                        .withSchedule(scheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, cronTrigger);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    // 任务暂停
    public void pauseJob(String id) throws SchedulerException {
        QuartzConfigDto quartzConfigDto = quartzConfigService.get(id);
        if(quartzConfigDto == null) throw new ServiceException(502,"不存在的任务");
        JobKey jobKey = JobKey.jobKey(id);
        Scheduler scheduler = getScheduler();
        scheduler.deleteJob(jobKey);
    }
    // 任务恢复
    public void resumeJob(String id) throws SchedulerException, ClassNotFoundException {
        QuartzConfigDto quartzConfigDto = quartzConfigService.get(id);
        if(quartzConfigDto == null) throw new ServiceException(502,"不存在的任务");
        JobKey jobKey = JobKey.jobKey(id);
        Scheduler scheduler = getScheduler();
        Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(quartzConfigDto.getClazz());
        JobDetail detail = scheduler.getJobDetail(jobKey);
        if (detail == null){
            JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(id).build();
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartzConfigDto.getCron());
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(id).withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, cronTrigger);
        }else {
            scheduler.resumeJob(jobKey);
        }
    }
    // 执行一次任务
    public void runJob(String id) throws SchedulerException {
        QuartzConfigDto quartzConfigDto = quartzConfigService.get(id);
        if(quartzConfigDto == null) throw new ServiceException(502,"不存在的任务");
        JobKey jobKey = JobKey.jobKey(id);
        Scheduler scheduler = getScheduler();
        scheduler.triggerJob(jobKey);
    }
}

定时任务运行工厂类用来springboot启动监听和注入SchedulerFactoryBean

package com.panda.common.crontask.web.schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
 * 定时任务运行工厂类
 *
 */
@Configuration
public class StartSchedulerListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    public com.panda.common.crontask.web.schedule.JpSchedulerFactory jpSchedulerFactory;
    @Autowired
    private com.panda.common.crontask.web.schedule.JpJobFactory jpJobFactory;
    // springboot 启动监听
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        jpSchedulerFactory.scheduleJobs();
    }
    //注入SchedulerFactoryBean
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(jpJobFactory);
        return schedulerFactoryBean;
    }
}

下面贴出api层代码

package com.panda.common.crontask.web.api;
import com.panda.auth.client.Authorization;
import com.panda.base.service.api.exception.ServiceException;
import com.panda.base.web.api.jersey.IResponse;
import com.panda.base.web.api.jersey.security.ISecurityContext;
import com.panda.base.web.api.response.WebApiResponse;
import com.panda.common.crontask.common.ApplicationConstants;
import com.panda.common.crontask.service.api.QuartzConfigService;
import com.panda.common.crontask.service.api.QuartzLogService;
import com.panda.common.crontask.service.api.dto.QuartzConfigDto;
import com.panda.common.crontask.service.api.dto.QuartzLogDto;
import com.panda.common.crontask.web.schedule.JpSchedulerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;
import java.time.LocalDateTime;
@Path("quartz")
@Component
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Api(tags = "【公共组件-定时任务】") 
@RolesAllowed("*")
@Authorization(permission = "quartz:*")
public class QuartzApi implements ISecurityContext, IResponse {
    private final static Logger log = LoggerFactory.getLogger(QuartzApi.class);
    @Autowired
    QuartzConfigService quartzConfigService;
    @Autowired
    QuartzLogService quartzLogService;
    //事务模版
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    JpSchedulerFactory JpSchedulerFactory;
    @ApiOperation(value = "获取任务分页")
    @GET
    public WebApiResponse<Page<QuartzConfigDto>> getQuartzConfigPage(@Context SecurityContext context,// 如果@Authorization存在,则不可缺少次参数。
@ApiParam(value = "页码(从0开始),默认0") @QueryParam("pageIndex") Integer pageIndex,
@ApiParam(value = "页大小,默认10") @QueryParam("pageSize") Integer pageSize,
@ApiParam(value = "任务名称") @QueryParam("name") String name,
@ApiParam(value = "任务状态 1-正常,0-停止") @QueryParam("status") Integer status) {
        if (pageIndex == null) pageIndex = 0;
        if (pageSize == null) pageSize = 10;
        Pageable pageable = PageRequest.of(pageIndex, pageSize,Sort.by(Sort.Direction.DESC, "updateTime"));
        return response(quartzConfigService.findQuartzConfigPage(status,name,pageable));
    }
    @ApiOperation(value = "通过Id获取任务详情")
    @GET
    @Path("{id}")
    public WebApiResponse<QuartzConfigDto> getQuartzConfigById(@Context SecurityContext context,@ApiParam(required = true, value = "quartzConfig Id") @PathParam("id") String id) {
        return response(quartzConfigService.get(id));
    }
    @ApiOperation(value = "通过类名获取任务详情")
    @GET
    @Path("getByClazz/{clazz}")
    public WebApiResponse<QuartzConfigDto> getQuartzConfigByClazz(@Context SecurityContext context,@ApiParam(required = true, value = "clazz") @PathParam("clazz") String clazz ) {
       return response(quartzConfigService.findByClazz(clazz));
    }
    @ApiOperation(value = "添加任务")
    @POST
    @Path("add")
    public WebApiResponse<QuartzConfigDto> addQuartzConfig(QuartzConfigDto quartzConfigDto, @Context SecurityContext context) {
        return response(quartzConfigService.addOrUpdateQuartzConfig(quartzConfigDto));
    }
    @ApiOperation(value = "删除任务")
    @POST
    @Path("delete/{id}")
    public WebApiResponse deleteQuartzConfig(@Context SecurityContext context, @ApiParam(required = true, value = "quartzConfig Id") @PathParam("id") String id) {
        return transactionTemplate.execute(new TransactionCallback<WebApiResponse>() {
            @Override
            public WebApiResponse doInTransaction(TransactionStatus transactionStatus) {
                try {
                    JpSchedulerFactory.pauseJob(id);
                    quartzConfigService.delete(id);
                } catch (Exception e) {
                    log.error("删除任务任务失败!", e);
                    transactionStatus.setRollbackOnly();
                    throw new ServiceException(502,"删除任务失败");
                }
                log.info("删除任务成功!");
                return done();
            }
        });
    }
    @ApiOperation(value = "更新任务")
    @POST
    public WebApiResponse<QuartzConfigDto> updateQuartzConfig(QuartzConfigDto quartzConfigDto, @Context SecurityContext context) {
        try {
            JpSchedulerFactory.pauseJob(quartzConfigDto.getId());
            quartzConfigDto = quartzConfigService.updateQuartzConfig(quartzConfigDto);
            JpSchedulerFactory.resumeJob(quartzConfigDto.getId());
        } catch (Exception e) {
            log.error("更新任务失败!", e);
            throw new ServiceException(502,"暂停任务失败");
        }
        log.info("更新任务成功!");
        return response(quartzConfigDto);
    }
    @ApiOperation(value = "暂停任务")
    @GET
    @Path("pause/{id}")
    public WebApiResponse pauseQuartzConfig(@Context SecurityContext context, @ApiParam(required = true, value = "quartzConfig Id") @PathParam("id") String id) {
        return transactionTemplate.execute(new TransactionCallback<WebApiResponse>() {
            @Override
            public WebApiResponse doInTransaction(TransactionStatus transactionStatus) {
                try {
                    JpSchedulerFactory.pauseJob(id);
                    quartzConfigService.updateJobStatus(id, ApplicationConstants.PAUSE_CODE);
                } catch (Exception e) {
                    log.error("暂停任务失败!", e);
                    transactionStatus.setRollbackOnly();
                    throw new ServiceException(502,"暂停任务失败");
                }
                log.info("暂停任务成功!");
                return done();
            }
        });
    }
    @ApiOperation(value = "恢复任务")
    @GET
    @Path("resume/{id}")
    public WebApiResponse resumeQuartzConfig(@Context SecurityContext context, @ApiParam(required = true, value = "quartzConfig Id") @PathParam("id") String id) {
        return transactionTemplate.execute(new TransactionCallback<WebApiResponse>() {
            @Override
            public WebApiResponse doInTransaction(TransactionStatus transactionStatus) {
                try {
                    JpSchedulerFactory.resumeJob(id);
                    quartzConfigService.updateJobStatus(id,ApplicationConstants.ACTIVE_CODE);
                } catch (Exception e) {
                    log.error("恢复任务失败!", e);
                    transactionStatus.setRollbackOnly();
                    throw new ServiceException(502,"恢复任务失败");
                }
                log.info("恢复任务成功!");
                return done();
            }
        });
    }
    @ApiOperation(value = "执行一次任务")
    @GET
    @Path("run/{id}")
    public WebApiResponse runQuartzConfig(@Context SecurityContext context, @ApiParam(required = true, value = "quartzConfig Id") @PathParam("id") String id) {
        try {
            JpSchedulerFactory.runJob(id);
        } catch (Exception e) {
            log.error("执行任务失败!", e);
            throw new ServiceException(502,"执行任务失败");
        }
        log.info("执行任务成功!");
        return done();
    }
    @ApiOperation(value = "获取任务日志分页")
    @GET
    @Path("log")
    public WebApiResponse<Page<QuartzLogDto>> getQuartzLogPage(@Context SecurityContext context,@ApiParam(required = false, value = "页码(从0开始),默认0") @QueryParam("pageIndex") Integer pageIndex,@ApiParam(required = false, value = "页大小,默认10") @QueryParam("pageSize") Integer pageSize,@ApiParam(value = "任务Id") @QueryParam("quartzId") String quartzId,@ApiParam(value = "任务名称") @QueryParam("name") String name,@ApiParam(value = "执行结果 1-成功,0-失败") @QueryParam("result") Integer result,@ApiParam(value = "开始时间 yyyy-MM-dd 00:00:00") @QueryParam("startTime") LocalDateTime startTime,
@ApiParam(value = "结束时间 yyyy-MM-dd 00:00:00") @QueryParam("endTime") LocalDateTime endTime) {
        if (pageIndex == null) pageIndex = 0;
        if (pageSize == null) pageSize = 10;
        Pageable pageable = PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        return response(quartzLogService.findQuartzLogPage(quartzId,name,result,startTime,endTime,pageable));
    }
}

执行实例

package com.panda.common.crontask.web.task;
import com.panda.common.crontask.web.schedule.JpJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
 * 定时任务实现类
 */
@Component
@EnableScheduling
public class ScheduleTaskTestJob extends JpJob implements Serializable {
    private static Logger logger = LoggerFactory.getLogger(ScheduleTaskTestJob.class);
    /**
     * 执行任务
     *
     * @throws RuntimeException
     */
    @Override
    public void runJob() throws RuntimeException {
        //在这里写定时任务执行的内容
        logger.info("==== 定时任务 ScheduleTaskTestJob ====> 执行中...... ");
    }
}

以上就是实现定时任务分布式组件的主要代码,写一个run模块集成一下数据库,做一个启动类就可以运行了,然后上传到neuxs做成依赖就可以供其他项目使用了,想看源码可以关注“蛋皮皮”公众号找大熊猫给你源码。

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以成标准的Java组件或 EJBs。 Quartz的优势: 1、Quartz是一个任务调度框架(库),它几乎可以集成到任何应用系统中。 2、Quartz是非常灵活的,它让您能够以最“自然”的方式来编写您的项目的代码,实现您所期望的行为 3、Quartz是非常轻量级的,只需要非常少的配置 —— 它实际上可以被跳出框架来使用,如果你的需求是一些相对基本的简单的需求的话。 4、Quartz具有容错机制,并且可以在重启服务的时候持久(”记忆”)你的定时任务,你的任务也不会丢失。 5、可以通过Quartz,封装成自己的分布式任务调度,实现强大的功能,成为自己的产品。6、有很多的互联网公司也都在使用Quartz。比如美团 Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度。   课程说明:在我们的日常开发中,各种大型系统的开发少不了任务调度,简单的单机任务调度已经满足不了我们的系统需求,复杂的任务会让程序猿头疼, 所以急需一套专门的框架帮助我们去管理定时任务,并且可以在多台机器去执行我们的任务,还要可以管理我们的分布式定时任务。本课程从Quartz框架讲起,由浅到深,从使用到结构分析,再到源码分析,深入解析Quartz、Spring+Quartz,并且会讲解相关原理, 让大家充分的理解这个框架和框架的设计思想。由于互联网的复杂性,为了满足我们特定的需求,需要对Spring+Quartz进行二次开发,整个二次开发过程都会进行讲解。Spring被用在了越来越多的项目中, Quartz也被公认为是比较好用的定时器设置工具,学完这个课程后,不仅仅可以熟练掌握分布式定时任务,还可以深入理解大型框架的设计思想。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值