如何使用scala安排石英作业

时间:2015-09-29 22:42:24

标签: scala quartz-scheduler

我正在尝试在Scala中运行最简单的石英作业示例。

configure()在我的模块加载时执行一次。

lazy val quartz = StdSchedulerFactory.getDefaultScheduler

override def configure() = {
  val Job = new Job {
    override def execute(jobExecutionContext: JobExecutionContext) = {
      println("Event")
    }
  }

  val job = JobBuilder.newJob(Job.getClass)
    .withIdentity("Job", "Group")
    .build

  val trigger: Trigger = TriggerBuilder
    .newTrigger
    .withIdentity("Trigger", "Group")
    .withSchedule(
      CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
    .build

  quartz.start
  quartz.scheduleJob(job, trigger)
}

但是,当此代码运行时,我收到一条错误消息。

2015-09-29 15:27:05,015 [DefaultQuartzScheduler_QuartzSchedulerThread] ERROR
org.quartz.core.ErrorLogger - An error occured instantiating job to be executed. job= 'Group.Job'
org.quartz.SchedulerException: Problem instantiating class 'com.search.binder.SearchModule$$anon$1'
    at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:58) ~[quartz-2.2.1.jar:na]
    at org.quartz.simpl.PropertySettingJobFactory.newJob(PropertySettingJobFactory.java:69) ~[quartz-2.2.1.jar:na]
    at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127) ~[quartz-2.2.1.jar:na]
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375) [quartz-2.2.1.jar:na]
Caused by: java.lang.InstantiationException: com.search.binder.SearchModule$$anon$1
    at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_45]
    at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:56) ~[quartz-2.2.1.jar:na]
    ... 3 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.search.binder.SearchModule$$anon$1.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_45]
    at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_45]
    ... 4 common frames omitted

有没有人在Scala中运行石英调度程序的“尽可能简单”示例?

1 个答案:

答案 0 :(得分:4)

我认为问题在于quartz正在尝试实例化Job的新实例,但它无法找到它的构造函数,因为您通过Job.getClass传递的类是一个匿名类。尝试按如下方式定义它:

class MyJob extends Job {
  override def execute(jobExecutionContext: JobExecutionContext) = {
    println("Event")
  }
}

然后:

val job = JobBuilder.newJob(classOf[MyJob])
  .withIdentity("Job", "Group")
  .build