Search code examples
javaclassloaderquartz-scheduler

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();

Solution

  • 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.