使用存储在数据库中的 Java 类名运行 Quartz 作业

发布于 2024-09-08 04:56:05 字数 783 浏览 4 评论 0原文

我在 Quartz 中有两个作业,它们运行得很好,但我发现我必须使用如下代码:

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 。我尝试通过以下方式转换为类:

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

但该作业根本不执行。我没有看到任何异常生成,也没有堆栈跟踪?

我在 Windows 7 上运行 JVM。

有什么想法吗?

摩根先生。

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

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

And using a class reference directly as:

    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?

I'm running a JVM on Windows 7.

Any ideas?

Mr Morgan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情定在深秋 2024-09-15 04:56:10

我猜您已将该类作为字符串存储在数据库中,并且您正在尝试将其转换为类?我也有同样的问题。

我需要创建的作业的详细信息在 BatchJobDto 对象中提供。它包含诸如作业名称、字符串形式的类名称等内容。特别是,batchJobDto.getClassName() 返回一个字符串,它是我的作业类的完全限定名称(“com.foo.bar.MyJob”或其他)。我创建触发器,从字符串中获取类并创建作业。从字符串获取类的顺序是:

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 完成的,它允许您使用参数化消息(如上所述的“{}”)。

那里有一个未经检查的强制转换,但是如果你的类名字符串描述了一个属于 Quartz 作业(实现 Job)的类,那么我相信它应该始终有效。

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.

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文