在 Quartz 作业中动态加载 Java 类文件的名称
我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题。
它不会输出任何内容,也不会引发错误。
这是因为您的作业类没有空构造函数。因此,即使代码是正确的,它也没有创建作业对象的机制。
如果您向 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.
据我了解,您的代码应该是这样的:
您能发布不起作用的代码片段吗?
As I understand it, you code should like this:
Can you please post the code snippets which didn't work?
尝试一下
,如果它不起作用请提供有关该问题的更多详细信息 - 编译错误、运行时异常或其他问题。
Try this
And if it doesn't work please give more details about the problem - compilation error, exception in runtime or some other problem.
我有这个,也许对你有用:(getClassName() 返回一个字符串)
I have this, maybe it will be usefull to you: (getClassName() returns a string)