views:

405

answers:

4

I have a Quartz job written in Java which runs fine if I have the Quartz JobDetail line set as follows:

JobDetail jd = new JobDetail("FeedMinersJob", scheduler.DEFAULT_GROUP, FeedMinersScheduler.class);

But I would like to dynamically load the class because the job details are stored in a database table. So I want something like this:

JobDetail jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, sj.getJobClassFile());

Where sj is a scheduled job object and method sj.getJobClassFile() returns the name of the class defined in sj instead of having the class name hardcoded.

I've tried permutations of the Java Class.forName method but without success.

A: 

As I understand it, you code should like this:

Class<?> jobClass = Class.forName(sj.getJobClassFile());
JobDetail jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, jobClass);

Can you please post the code snippets which didn't work?

David Rabinowitz
A: 

There are two lines of code which work as there are below:

JobDetail jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, FeedMinersScheduler.class); 
CronTrigger ct = new CronTrigger(sj.getJobTrigger(), scheduler.DEFAULT_GROUP, "0 0/" + Integer.toString(sj.getJobIntervalMinutes()) + " * * * ?");

But obviously I want the first of these to be:

Class<?> jobClass = Class.forName(sj.getJobClassFile());
JobDetail jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, jobClass);

But I find this doesn't work either. –

What does it mean "it doesn't work". Do you get a compiler error (probably because of not handling ClassNotFoundException)? Do you get some exception in runtime? Does velociraptor come from somewhere and smash your computer?
Tadeusz Kopec
When I say it doesn't work, I mean that there are now exceptions thrown and that there are no log messages recorded where there should it. Simply put, nothing happens.
Great. So there are exceptions thrown. Can you edit your question: add code that causes exception, name of the exception and stack trace?
Tadeusz Kopec
A: 

Try this

try {
    Class<?> jobClass = Class.forName(sj.getJobClassFile());
    JobDetail jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, jobClass);
} catch (ClassNotFoundException e) {
    // put here some error handling
}

And if it doesn't work please give more details about the problem - compilation error, exception in runtime or some other problem.

Tadeusz Kopec
This is almost precisely what I have except I'm testing for any exception. But there are no compiler warnings or runtime exceptions at all. Simply put, nothing happens.
A: 

I have this, maybe it will be usefull to you: (getClassName() returns a string)

Class<?> jobClass = Class.forName(t_job.getClassName());
if (Job.class.isAssignableFrom(jobClass)) {
        // create a job detail that is not volatile and is durable (is persistent and exists without trigger)
        JobDetail job = new JobDetail(t_job.getName(), t_job.getGroupName(), jobClass, false, true, true);
        job.setDescription(t_job.getDescription());

}
Hector