在 Quartz 作业中动态加载 Java 类文件的名称

发布于 2024-08-05 10:20:25 字数 484 浏览 6 评论 0原文

我有一个用 Java 编写的 Quartz 作业,如果我将 Quartz JobDetail 行设置如下,则该作业可以正常运行:

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

但我想动态加载该类,因为作业详细信息存储在数据库表中。所以我想要这样的东西:

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

其中 sj 是计划作业对象,方法 sj.getJobClassFile() 返回 sj 中定义的类的名称,而不是对类名进行硬编码。

我尝试过 Java Class.forName 方法的排列,但没有成功。

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.

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

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

发布评论

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

评论(4

非要怀念 2024-08-12 10:20:25

我遇到了同样的问题。
它不会输出任何内容,也不会引发错误。
这是因为您的作业类没有空构造函数。因此,即使代码是正确的,它也没有创建作业对象的机制。
如果您向 Job 类添加一个空构造函数,它将起作用。

I ran into the same issue.
It does not output anything, and does not throw an error either.
This is because your job class doesn't have an empty constructor. So even though the code is correct, it has no mechanism to create the job object.
If you add an empty constructor to your Job class, itwill work.

第几種人 2024-08-12 10:20:25

据我了解,您的代码应该是这样的:

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

您能发布不起作用的代码片段吗?

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?

豆芽 2024-08-12 10:20:25

尝试一下

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
}

,如果它不起作用提供有关该问题的更多详细信息 - 编译错误、运行时异常或其他问题。

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.

聚集的泪 2024-08-12 10:20:25

我有这个,也许对你有用:(getClassName() 返回一个字符串)

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

}

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

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