Quartz HelloJob
I am new to Quartz and I'm running into a compiling error. I am simply trying to get the HelloJob to run based on Quartz's Lesson 1 for Hello World. I am having trouble declaring a JobDetail
with the error: The method newJob(Class<? extends Job>)
in the type JobBuilder
is not applicable for the arguments (Class)".
Originally, the code had 3 errors at newJob
, newTrigger
, and simpleSchedule
was
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
without JobBuilder.newJob(开发者_如何学编程...), TriggerBuilder.newTrigger(...), SimpleScheduleBuilder.simpleSchedule(...). Unlike the example given, I went ahead and added the imports and attached the class calls in front of newJob, newTrigger, etc. which got rid of 2/3 errors. But it seems the error persists with
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
I have also tried replacing my job declaration with
JobDetail job = new JobDetail("job1", "group1", HelloJob.class);
but that ends with Cannot instantiate the type JobDetail
and it seems like a few examples out there do this.
Will really appreciate clarification,
Thanks!
You need to have this line of code:
import static org.quartz.JobBuilder.*;
And then in should work. Hopefully.
Edit: AND MAKE SURE 'HELLOJOB' IMPLEMENTS JOB!!
There.
You need to add below four imports for helloJob examples to work for quart 2.2.x
import org.quartz.SimpleTrigger;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
Here you go:
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Simple Exapmle");
}
}
Quartz 2 APIs are big different with Quartz 1(1.5,1.6 and 1.7) Class JobDetail{ }
quartz-1.6.6: http://javasourcecode.org/html/open-source/quartz/quartz-1.6.6/org/quartz/JobDetail.html
Quartz 2:
public interface JobDetail extends Serializable, Cloneable {
}
// we have to create JobDetail in the below way.
JobDetail job = newJob(HelloJob.class)
// we have to create Trigger in the below way.
Trigger trigger = newTrigger()
Don't forget to import the below one
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
1-
Quartz provides “builder” classes that define a Domain Specific Language
You can import the missing DSL's through:
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
2- Make sure that HelloJob
class implements org.quartz.Job
not any other Job:
public class HelloJob implements org.quartz.Job{
public void execute(JobExecutionContext context) throws JobExecutionException{
System.out.println("Hello! HelloJob is executing.");
}
}
You can find a tutorial in the quartz documentation.
There is a more detailed description of this example on this website. There, you can find the libraries that are necessary to import, as well as the implementation of job in the class HelloJob.
You have to Implement HelloJob.class
with the Job
interface
using
import org.quartz.Job;
public class HelloJob implements Job {
}
精彩评论