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
- 监听触发器(trigger)相关的事件
- 触发器触发∶ triggerFired
- 触发器完成∶ triggerComplete
- 触发器否决∶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();
}
}