运行具有存储在数据库中的Java类名称的Quartz作业 [英] Running a Quartz job with Java class name stored in database

查看:253
本文介绍了运行具有存储在数据库中的Java类名称的Quartz作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Quartz有两个工作,这将运行良好,但我发现我必须使用像下面的代码:

I have a two jobs in Quartz which will run perfetly well but I find I have to use code like:

jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, PollJob.class);
ct = new CronTrigger(sj.getJobTrigger(), scheduler.DEFAULT_GROUP, "0 20 * * * ?");
        scheduler.scheduleJob(jd, ct);

我必须硬编码PollJob.class才能运行作业,sj是从数据库读取的对象PollJob的详细信息。但我想从数据库中设置PollJob.class。我试过通过以下方式转换到类:

I have to hardcode PollJob.class to run the job and sj is an object read from the database containing PollJob's details. But I would like to set PollJob.class from the database as well. I've tried casting to a class by:

Class cls = Class.forName(sj.getJobJavaClassFile());
jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, cls));

直接使用类引用:

    jd = new JobDetail(sj.getJobName(), scheduler.DEFAULT_GROUP, Class.forName sj.getJobJavaClassFile()));

但是作业根本不执行。没有生成的异常,我可以看到,没有堆栈跟踪?

But the job simply doesn't execute. There are no exceptions generated that I can see and no stack trace?

我在Windows 7上运行JVM。

I'm running a JVM on Windows 7.

任何想法?

Morgan先生。

推荐答案

我猜你已经存储在数据库中的类作为一个字符串,你试图把它变成一个类?我有同样的问题。

I'm guessing that you have stored the class in your database as a String, and you are trying to turn that into a class? I had the same problem.

我需要创建的作业的细节在BatchJobDto对象中提供。它包含类似作业的名称,作为String的类的名称等等。特别是batchJobDto.getClassName()返回一个String,它是我的作业类(com.foo.bar.MyJob或其他)的完全限定名。我创建了触发器,从String中获取类并创建作业。从String获取类的顺序是:

The details of the job I need to create are provided in a BatchJobDto object. It contains things like the name of the job, the name of the class as a String and so on. In particular batchJobDto.getClassName() return a String which is the fully-qualified name of my job class ("com.foo.bar.MyJob" or whatever). I create the trigger, get the class from the String and create the job. The sequence for getting the class from the String is:

Class<? extends Job> jobClass = null;
try {
    jobClass = (Class<? extends Job>)
                  Class.forName(batchJobDto.getClassName());
} catch(ClassNotFoundException e) {
    logger.error("createJob(): ClassNotFoundException on job class {} - {}",
        batchJobDto.getClassName(), e.getMessage());
} catch(Exception e) {
    logger.error("createJob(): Exception on job class {} - {}",
        batchJobDto.getClassName(), e.getMessage());
}

记录是使用slf4j完成的,它允许您使用参数化消息{}如上)。

The logging is done with slf4j, which allows you to use a parameterised message ("{}" as above).

这里有一个未选中的转换,但是如果你的类名字符串描述一个类是一个Quartz作业它应该总是工作。

There's an unchecked cast in there, but if your classname string describes a class which is a Quartz job (implements Job) then I believe it should always work.

这篇关于运行具有存储在数据库中的Java类名称的Quartz作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆