ps0604
ps0604

Reputation: 1071

Quartz 2.1.5: Trying to create a job detail dynamically using JobBuilder

I've seen several posts about this, however don't work with the new JobBuilder approach in Quartz.

I'm trying to create a JobDetail dynamically, using a string that stores the class name. However I'm getting the following compiler error:

 The method newJob(Class<? extends Job>) in the type JobBuilder is not applicable 
 for the arguments (Class<capture#6-of ?>)

This is the code:

String s = "ClassName";
Class<?> jobClass = null;
try {
    jobClass = Class.forName (s);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
    throw new MsgException ( "Requested Job Class not found" );
}

JobDetail jobDetail = newJob(jobClass).
        withIdentity(jobKey).
        withDescription(description).
        storeDurably().
        usingJobData(dataMap).
        build();

Upvotes: 0

Views: 4423

Answers (2)

I think that the newJob constructor expects a compile time reference while methods such as Class.forName() are run time references. An intermediate solution is to use something like:

private void makeNewJob(Class<? extends Job> jobClass, String identity,
    String environment)
{
    JobDetail job = newJob(jobClass).withIdentity(identity, environment)
        .build();

// additional code
}

and call with:

makeNewJob(MyJob.class, "MyJob", "Production");

Upvotes: 1

Antimony
Antimony

Reputation: 39451

Did you look at the error message? newJob takes a parameter of type Class<? extends Job>, but you're passing it a parameter of type Class<?>. As a quick fix, you can try changing it to

newJob((Class<? extends Job>)jobClass)

In the long run you'll probably want to do actual checking to make sure it is a subclass of Job, since otherwise you'll get mysterious runtime errors from inside Quartz when it isn't.

Upvotes: 0

Related Questions