定时任务-Quartz-Listener

JobListener

sc.getListenerManager().addJobListener(new MyJobListener());

public class MyJobListener extends JobListenerSupport {
    @Override
    public String getName() {
        return "myJobListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext context) {
        super.jobToBeExecuted(context);
        LocalTime now = LocalTime.now();
        System.out.println(now.toString()+"jobToBeExecuted");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("1000200");
        dataSource.setJdbcUrl("jdbc:mysql://82.157.62.33:3008/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false");

        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("insert into quartz_job_log (job_name,type,time) values (?,?,?)");
            preparedStatement.setString(1,context.getJobDetail().getKey().toString());
            preparedStatement.setInt(2,1);
            preparedStatement.setTime(3,new Time(new Date().getTime()));
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        dataSource.close();

    }

    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        super.jobWasExecuted(context, jobException);
        LocalTime now = LocalTime.now();
        System.out.println(now.toString()+"jobWasExecuted");
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("1000200");
        dataSource.setJdbcUrl("jdbc:mysql://82.157.62.33:3008/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false");

        try {
            Connection connection = dataSource.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("insert into quartz_job_log (job_name,type,time) values (?,?,?)");
            preparedStatement.setString(1,context.getJobDetail().getKey().toString());
            preparedStatement.setInt(2,2);
            preparedStatement.setTime(3,new Time(new Date().getTime()));
            preparedStatement.execute();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        dataSource.close();
    }
}

 TriggerListener

  1. 监听触发器(trigger)相关的事件
  2. 触发器触发∶ triggerFired
  3. 触发器完成∶ triggerComplete
  4. 触发器否决∶vetoJobExecution
public class MyTriggerListener extends TriggerListenerSupport {
    @Override
    public String getName() {
        return "myTriggerListener";
    }

    @Override
    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        super.triggerFired(trigger, context);
        System.out.println("开始触发");
        System.out.println(LocalDateTime.now());
    }

    @Override
    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        super.triggerComplete(trigger, context, triggerInstructionCode);
        System.out.println("触发完成");
        System.out.println(triggerInstructionCode.toString());
        System.out.println(LocalDateTime.now());
    }

    @Override
    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        System.out.println("vetoJobExecution正在执行!!");
        //如果时间是5,15,25,尾数是5的时间点,就不执行
        Calendar now = Calendar.getInstance();
        int second = now.get(Calendar.SECOND);
        if (second % 10 ==5){
            System.out.println("本次Trigger不触发!!");
            return true;
        }
        return false;
    }
}
public class TriggrtListerTest {
    public static void main(String[] args) {
        try {

            Scheduler sc = StdSchedulerFactory.getDefaultScheduler();
            JobDetail build = JobBuilder.newJob(MyjobMisfire.class).storeDurably()
                    .withIdentity("触发器", "触发器组")
                    .usingJobData("bu", "chang")
                    .withDescription("测试触发器的组")
                    .build();
            CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                    .withIdentity("触发器监听器", "触发器组")
                    .withSchedule(
                            CronScheduleBuilder.cronSchedule("0/5 * * * * ? *")
                    )
                    .startNow()
                    .build();
            sc.getListenerManager().addTriggerListener(new MyTriggerListener());
            sc.scheduleJob(build,cronTrigger);
            sc.start();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

 SchedulerListener

public class MySchedulerListener extends SchedulerListenerSupport {

    @Override
    public void schedulerStarting() {
        super.schedulerStarting();
        LocalTime now = LocalTime.now();
        System.out.println(now.toString()+",schedulerStarting!!"); 
    }

    @Override
    public void schedulerShuttingdown() {
        super.schedulerShuttingdown();
        LocalTime now = LocalTime.now();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值