问题
I am trying to get the most simple example of a quartz job running in Scala.
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
Does anyone have an "as simple as possible" example of quartz scheduler running in Scala?
回答1:
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")
}
}
And then:
val job = JobBuilder.newJob(classOf[MyJob])
.withIdentity("Job", "Group")
.build
来源:https://stackoverflow.com/questions/32854936/how-to-schedule-a-quartz-job-with-scala