Quartz 作业监听JobListener

2019-04-23 00:18|来源: 网路

在本教程中,我们将展示/介绍如何创建一个JobListener,跟踪运行工作状态在作业完成等。

P.S 这个例子是Quartz 2.1.5

1. Quartz 作业

作业 - 用于打印一个简单的信息,并抛出一个JobExecutionException进行测试。

File : HelloJob.java

package com656463;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class HelloJob implements Job
{
	public void execute(JobExecutionContext context)
	throws JobExecutionException {
 
		System.out.println("Hello Quartz! - by yiibsxt");	
 
		//Throw exception for testing
		throw new JobExecutionException("Testing Exception");
	}
 
}


2. JobListener

创建一个JobListener,只是实现了JobListener接口,并覆盖所有的接口的方法。

File : HelloJobListener.java

package com656463.quartz.listener;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
 
public class HelloJobListener implements JobListener {
 
	public static final String LISTENER_NAME = "dummyJobListenerName";
 
	@Override
	public String getName() {
		return LISTENER_NAME; //must return a name
	}
 
	// Run this if job is about to be executed.
	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
 
		String jobName = context.getJobDetail().getKey().toString();
		System.out.println("jobToBeExecuted");
		System.out.println("Job : " + jobName + " is going to start...");
 
	}
 
	// No idea when will run this?
	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
		System.out.println("jobExecutionVetoed");
	}
 
	//Run this after job has been executed
	@Override
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {
		System.out.println("jobWasExecuted");
 
		String jobName = context.getJobDetail().getKey().toString();
		System.out.println("Job : " + jobName + " is finished...");
 
		if (!jobException.getMessage().equals("")) {
			System.out.println("Exception thrown by: " + jobName
				+ " Exception: " + jobException.getMessage());
		}
 
	}
 
}

注意:
不知道什么是“jobExecutionVetoed”,并会在何时触发?

3. CronTrigger

例如上面HelloJobListener连接到调度和监控作业的状态。

File : CronTriggerExample.java

package com656463.quartz;
 
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
 
import com656463.quartz.listener.HelloJobListener;
 
public class CronTriggerExample {
    public static void main( String[] args ) throws Exception
    {
 
	JobKey jobKey = new JobKey("dummyJobName", "group1");
    	JobDetail job = JobBuilder.newJob(HelloJob.class)
		.withIdentity(jobKey).build();
 
    	Trigger trigger = TriggerBuilder
		.newTrigger()
		.withIdentity("dummyTriggerName", "group1")
		.withSchedule(
			CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
		.build();
 
    	Scheduler scheduler = new StdSchedulerFactory().getScheduler();
 
    	//Listener attached to jobKey
    	scheduler.getListenerManager().addJobListener(
    		new HelloJobListener(), KeyMatcher.keyEquals(jobKey)
    	);
 
    	//Listener attached to group named "group 1" only.
    	//scheduler.getListenerManager().addJobListener(
    	//	new HelloJobListener(), GroupMatcher.jobGroupEquals("group1")
    	//);
 
    	scheduler.start();
    	scheduler.scheduleJob(job, trigger);
 
    }
}



运行CronTriggerExample.java, 这里是输出结果:

jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Quartz! - by sxt.com
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception
 
jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Quartz! - by sxt.com
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception


相关问答

更多
  • 我认为你有几个问题需要解决。 检查quartz文档并修改web.xml以使用listener或servlet方法。 你现在有两个。 您需要另一个servlet在tomcat中运行才能创建作业。 谷歌如何创建一个简单的servlet或要求更多信息。 您必须获取quartz init servlet创建的调度程序工厂 - 检查quartz init servlet的javadocs以了解如何执行此操作。 它们使servlet上下文可用于其他servlet抓取 I think you have a couple ...
  • 这应该是诀窍: 0 1 0 1/1 * ? * 说明: 第一个值(在这种情况下为0)是秒,第二个值(1)是分钟,第三个值是小时(0表示上午12点),第四个值是月份的日期(1/1表示每天),第五个值是月份(*表示所有月份),第六个值是星期几(?表示没有特定值),第七个值是年份(再次*表示每年)。 This should do the trick: 0 1 0 1/1 * ? * Explanation: The first value (0 in this case) is the seconds, th ...
  • 我弄清楚哪个是问题所在。 首先,建议:在开始使用Quartz .net进行调试之前始终配置日志。 当Job准备好执行时,将通知JobListener,然后调用JobToBeExecuted方法。 正如您在JobListener的实现中所看到的,我在JobToBeExecuted方法中抛出异常,该异常阻止了Job的执行。 我没有调查为什么JobListener中的错误应该阻止Job执行。 我想有一系列被异常打破的电话。 无论如何,这是我的问题的答案。 I figured out which was the p ...
  • 问题似乎是sqlite jdbc驱动程序不支持方法ResultSet.getBlob() 。 但是quartz使用此方法来检索分配给Job的JobDataMap 。 如果您仍然想使用带有sqlite的quartz,您可以扩展StdJDBCDelegate并检索/设置blobs,如本答案中所建议的那样 。 乍一看,您似乎只能覆盖这些方法 StdJDBCDelegate.getObjectFromBlob()和 StdJDBCDelegate.getJobDataFromBlob() 由于我不确定以后会出现更多 ...
  • 我同意NinjaNye。 您必须使用API提交作业,因为它需要在运行时绑定类的命名空间。 这个过程非常简单 : // construct job info JobDetail jobDetail = new JobDetail("myJob", null, typeof(HelloJob)); // fire every hour Trigger trigger = TriggerUtils.MakeHourlyTrigger(); // start on the next even hour trigge ...
  • 您应该使用在特定时间触发并且不重复的SimpleTrigger。 TriggerUtils有很多方便的方法来创建这些类型的东西。 You should use SimpleTrigger that fires at specific time and without repeating. TriggerUtils has many handy methods for creating these kind of things.
  • 您正在JBoss上运行Quartz,因此请考虑使用HASingleton来控制主节点 - 然后您不需要使用Quartz提供的集群,Quartz使用数据库并导致问题。 HASingleton自JBoss 4或5开始提供。如果您使用的是版本7,那么您可以查看这篇文章 ,了解有关其实现的详细信息 I think I have found the solution! I disabled job file's auto scanning by doing this: org.quartz.plugin.jobIni ...
  • 你能看看TriggerListener吗? 您应该实现TriggerListener并在“vetoJobExecution”方法中使用您的中止逻辑。 boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) 它在触发器触发时由调度程序调用,并且它的关联JobDetail即将被执行。 如果实现操作执行(通过返回true),则不会调用作业的execute方法。 Can you look ...
  • 它不仅支持这种行为,而且基本上没有其他方法。 一旦计划了作业和触发器(在任何线程中),该作业将在线程池中异步执行。 您可以像线程数一样控制该线程池。 另一个问题是并行执行相同的工作。 默认情况下,相同的作业可以在由不同线程启动的多个线程中运行,除非作业是有状态的 。 Not only it supports this behaviour but there is basically no other way. Once you schedule a job and a trigger (in any thr ...
  • 要检测何时发生任何类型的错误,您将需要实现一个侦听器,一个Job侦听器或一个Trigger侦听器,或者您可能同时需要两个: http : //quartznet.sourceforge.net/tutorial/lesson_7.html 您可以通过调用您创建的IScheduler对象上的方法来附加侦听器。 我们如何汇总多封电子邮件是使用日志系统,特别是NLog 。 我们在Mail目标周围使用BufferingWrapper ,以便在记录了一些指定数量的事件(例如,200)之后或在最后一次记录的错误(例如, ...