quartz的多任务使用

由于在做项目时使用到了定时任务中的多任务,所以来记录一下,下面各个文件的实际定义顺序并非按照序号。
1、在SpringBoot中使用时所需要添加的坐标

<!--quartz依赖-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!--添加Scheduled坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!--SPRING tx坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
        </dependency>
            

2、定义一个类(方法),便于多任务调用

@Component
@EnableScheduling
public class Quartz1 {
    @Autowired
    private PurchaseOrderService purchaseOrderService;
    public void getMoney(){

        Double a = purchaseOrderService.getMonthMoney();
        Double b = purchaseOrderService.getYearMoney();
        purchaseOrderService.updateMonthMoney(a);
        purchaseOrderService.updateYearMoney(b);
  
    }
}
@Service
@EnableScheduling
public class Quartz2  {
    @Autowired
    private PurchaseOrderService purchaseOrderService;
    public void newMonthMoney(){
        purchaseOrderService.newMonthMoney();
    }
}

@Service
@EnableScheduling
public class Quart3 {

    @Autowired
    private PurchaseOrderService purchaseOrderService;
    public void newYearMoney(){
        purchaseOrderService.newYearMoney();
       // System.out.println("任务三执行了!");
    }
}

3、在Service包下(也可以随便建个包),建立PurchaseOrderService类

public class PurchaseOrderService {
    @Autowired
    PurchaseOrderMapper purchaseOrderMapper;
    public Double getAllMoney(String start,String end){

        double money = purchaseOrderMapper.getAllMoney(start,end);
        //double money = 10.0;
        Double all_Money = new Double(money);
        System.out.println("PurchaseOrderService正常工作了!!!!!!");
        System.out.println("start:   "+start);
        System.out.println("end:   "+end);
        return all_Money;

    }

    public Double getMonthMoney(){
        double money = purchaseOrderMapper.getMonthMoney();
        Double month_Money = new Double(money);
       // System.out.println("getMonthMoney正常工作了!!!!");
        return month_Money;
    }

    public void updateMonthMoney(Double money){
        purchaseOrderMapper.updateMonthMoney(money);
        //System.out.println("purchaseorderService 中的updateMonthMoney执行了!");

    }

    public void updateYearMoney(Double money){
        purchaseOrderMapper.updateYearMoney(money);
       // System.out.println("purchaseorderService 中的updateYearMoney执行了!");

    }

    public Double getYearMoney(){
        double money = purchaseOrderMapper.getYearMoney();
        Double year_Month = new Double(money);
        //System.out.println("getYearMoney正常工作了!!!!");
        return year_Month;
    }

    public void newMonthMoney(){
        purchaseOrderMapper.newMonthMoney();
       // System.out.println("purchaseorderService 中的newMonthMoney执行了!");
    }

    public void newYearMoney(){
        purchaseOrderMapper.newYearMoney();
        //System.out.println("purchaseorderService 中的newYearMoney执行了!");
    }
 }

4、接口处声明函数(这点应该是放在前面的)

public interface PurchaseOrderMapper  extends BaseMapper<PurchaseOrder> {
 double getAllMoney(@Param("start") String start,@Param("end") String end);
    double getMonthMoney();
    double getYearMoney();
    void newMonthMoney();
    void newYearMoney();
    void updateMonthMoney(@Param("money") Double money);
    void updateYearMoney(@Param("money") Double money);  
}  

5、编写一个mapper用于将接口中定义的方法是用上sql语句并将结果传至数据库

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mapper.PurchaseOrderMapper">
 <select id="newMonthMoney" resultType="com.entity.Sum">
        insert into order_money (sum_money,money_status,money_time)values('0','0',now())
    </select>

    <select id="newYearMoney" resultType="com.entity.Sum">
        insert into order_money (sum_money,money_status,money_time)values('0','1',now())
    </select>

    <select id="getAllMoney" resultType="java.lang.Double">
        select sum(ifnull(po_Sum,0)) from purchase_order where gmt_create between #{start} and #{end}
    </select>


    <select id="getMonthMoney" resultType="java.lang.Double">
        select sum(ifnull(po_Sum,0)) from purchase_order where date_format(gmt_create,'%Y-%m')=date_format(now(),'%Y-%m');
    </select>

    <select id="updateMonthMoney" resultType="java.lang.Double">
        update  order_money set sum_money = #{money} where money_status = '1' and date_format(money_time,'%Y-%m')=date_format(now(),'%Y-%m')
    </select>

    <select id="updateYearMoney" resultType="java.lang.Double">
        update  order_money set sum_money = #{money} where money_status = '0' and date_format(money_time,'%Y')=date_format(now(),'%Y')
    </select>

    <select id="getYearMoney" resultType="java.lang.Double">
        select sum(ifnull(po_Sum,0)) from purchase_order where date_format(gmt_create,'%Y')=date_format(now(),'%Y');
    </select>
    </mapper>

6、编写一个是用quartz多任务的类

@Configuration
public class QuartzConfigration {

    @Bean(name = "firstJobDetail")
    public MethodInvokingJobDetailFactoryBean firstJobDetail(Quartz1 quartz1){
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        jobDetail.setConcurrent(true);
        jobDetail.setTargetObject(quartz1);
        jobDetail.setTargetMethod("getMoney");
        return jobDetail;
    }

    @Bean(name = "firstTrigger")
    public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail){
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(firstJobDetail);
        trigger.setStartDelay(0);
        trigger.setRepeatInterval(5000);
        return trigger;
    }

    @Bean(name = "secondJobDetail")
    public MethodInvokingJobDetailFactoryBean secondJobDetail(Quartz2 quartz2){
        MethodInvokingJobDetailFactoryBean jobDeatail = new MethodInvokingJobDetailFactoryBean();
        jobDeatail.setConcurrent(true);
        jobDeatail.setTargetObject(quartz2);
        jobDeatail.setTargetMethod("newMonthMoney");
        return jobDeatail;
    }

    @Bean(name = "secondTrigger")
    public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail){
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(secondJobDetail);
        trigger.setCronExpression("0 0 0 1 * ? *");
        //trigger.setCronExpression("0/2 * * * * ? *");
        return trigger;
    }

    @Bean(name = "thirdJobDetail")
    public MethodInvokingJobDetailFactoryBean thirdJobDetail(Quart3 quartz3){
        MethodInvokingJobDetailFactoryBean jobDeatail = new MethodInvokingJobDetailFactoryBean();
        jobDeatail.setConcurrent(true);
        jobDeatail.setTargetObject(quartz3);
        jobDeatail.setTargetMethod("newYearMoney");
        return jobDeatail;
    }

    @Bean(name = "thirdTrigger")
    public CronTriggerFactoryBean thirdTrigger(JobDetail thirdJobDetail){
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(thirdJobDetail);
        trigger.setCronExpression("0 0 0 1 1 ? *");
       // trigger.setCronExpression("0/2 * * * * ? *");
        return trigger;
    }


    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger,Trigger thirdTrigger){
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setStartupDelay(1);
        bean.setTriggers(firstTrigger,secondTrigger,thirdTrigger);
        return bean;
    }

}

其中的写法几乎固定,使用时换一换类名即可,其中包含了两种定时方法。

你可能感兴趣的:(quartz,quartz,java)