本文介绍了如何使用 Scala 安排石英作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the most simple example of a quartz job running in Scala.

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

configure() gets executed once when my module is loaded.

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)
}

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

However, I get an error message when this code runs.

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 中运行的尽可能简单"的石英调度程序示例?

Does anyone have an "as simple as possible" example of quartz scheduler running in Scala?

推荐答案

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

I think that the problem is that quartz is trying to instantiate a new instance of Job but it can't find its constructor because the class you are passing via Job.getClass is an anonymous class. Try defining it as follows:

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

然后:

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

这篇关于如何使用 Scala 安排石英作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:54