在 Spring Quartz 任务执行时记录调用者 Class/Job 上下文
Log Caller Class/Job Context When Spring Quartz Task Execute
我正在编写一个 java/spring 库以包含在其他使用 quartz 的项目中。
每次执行作业时,我都需要它来记录有关 task/calling class 的一些信息。
例如,如果 quartz 作业如下所示:
@Bean
public JobDetail jobADetail() {
return JobBuilder.newJob(QuartzTaskA.class)
.withIdentity("sampleJobA")
.storeDurably().build();
}
@Bean
public Trigger jobATrigger(JobDetail jobADetail) {
return TriggerBuilder.newTrigger()
.forJob(jobADetail)
.withIdentity("sampleTriggerA")
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * ? * * *"))
.build();
}
public class QuartzTaskA implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
log.info("QuartzA - the time is now {}", dateFormat.format(new Date()));
}
}
我希望它记录如下内容:
Job [QuartzTaskA] with details [sampleJobA] and trigger [sampleTriggerA] is starting
QuartzA - the time is now 12:07:39
我使用自定义 TaskExecutor 自定义 SchedulerFactoryBean,它在 task.run() 之前执行日志。
这有效,我可以打印额外的第一行,但我不知道如何让 details/context 传递到日志中。
@Configuration
public class SchedulerFactoryCustomizer implements SchedulerFactoryBeanCustomizer {
private static final Logger log = LogManager.getLogger(SchedulerFactoryCustomizer.class);
@Override
public void customize(SchedulerFactoryBean schedulerFactoryBean) {
Executor executor = SchedulerFactoryBean.getConfigTimeTaskExecutor();
schedulerFactoryBean.setTaskExecutor(new CustomExecutor(executor);
}
private static class CustomExecutor implements Executor {
final Executor executor;
private CustomExecutor(Executor executor) {
this.executor = executor;
}
@Override
public void execute(Runnable task) {
// This line here. How can I get the details/context to pass in?
//log.info("Job {} with details {} and trigger {} is starting");
task.run();
}
}
}
如何让 details/context 传递到日志中?
您可以实施 JobListener/TriggerListener
public class LoggingTriggerListener implements TriggerListener {
@Override
public String getName() {
return null;
}
@Override
public void triggerFired(final Trigger trigger, final JobExecutionContext context) {
}
@Override
public boolean vetoJobExecution(final Trigger trigger, final JobExecutionContext context) {
return false;
}
@Override
public void triggerMisfired(final Trigger trigger) {
}
@Override
public void triggerComplete(final Trigger trigger, final JobExecutionContext context, final Trigger.CompletedExecutionInstruction triggerInstructionCode) {
}
}
它还会注入 jobExecutionContext。
我正在编写一个 java/spring 库以包含在其他使用 quartz 的项目中。 每次执行作业时,我都需要它来记录有关 task/calling class 的一些信息。
例如,如果 quartz 作业如下所示:
@Bean
public JobDetail jobADetail() {
return JobBuilder.newJob(QuartzTaskA.class)
.withIdentity("sampleJobA")
.storeDurably().build();
}
@Bean
public Trigger jobATrigger(JobDetail jobADetail) {
return TriggerBuilder.newTrigger()
.forJob(jobADetail)
.withIdentity("sampleTriggerA")
.withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * ? * * *"))
.build();
}
public class QuartzTaskA implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
log.info("QuartzA - the time is now {}", dateFormat.format(new Date()));
}
}
我希望它记录如下内容:
Job [QuartzTaskA] with details [sampleJobA] and trigger [sampleTriggerA] is starting
QuartzA - the time is now 12:07:39
我使用自定义 TaskExecutor 自定义 SchedulerFactoryBean,它在 task.run() 之前执行日志。 这有效,我可以打印额外的第一行,但我不知道如何让 details/context 传递到日志中。
@Configuration
public class SchedulerFactoryCustomizer implements SchedulerFactoryBeanCustomizer {
private static final Logger log = LogManager.getLogger(SchedulerFactoryCustomizer.class);
@Override
public void customize(SchedulerFactoryBean schedulerFactoryBean) {
Executor executor = SchedulerFactoryBean.getConfigTimeTaskExecutor();
schedulerFactoryBean.setTaskExecutor(new CustomExecutor(executor);
}
private static class CustomExecutor implements Executor {
final Executor executor;
private CustomExecutor(Executor executor) {
this.executor = executor;
}
@Override
public void execute(Runnable task) {
// This line here. How can I get the details/context to pass in?
//log.info("Job {} with details {} and trigger {} is starting");
task.run();
}
}
}
如何让 details/context 传递到日志中?
您可以实施 JobListener/TriggerListener
public class LoggingTriggerListener implements TriggerListener {
@Override
public String getName() {
return null;
}
@Override
public void triggerFired(final Trigger trigger, final JobExecutionContext context) {
}
@Override
public boolean vetoJobExecution(final Trigger trigger, final JobExecutionContext context) {
return false;
}
@Override
public void triggerMisfired(final Trigger trigger) {
}
@Override
public void triggerComplete(final Trigger trigger, final JobExecutionContext context, final Trigger.CompletedExecutionInstruction triggerInstructionCode) {
}
}
它还会注入 jobExecutionContext。