使用存储在数据库中的 Java 类名运行 Quartz 作业
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您已将该类作为字符串存储在数据库中,并且您正在尝试将其转换为类?我也有同样的问题。
我需要创建的作业的详细信息在 BatchJobDto 对象中提供。它包含诸如作业名称、字符串形式的类名称等内容。特别是,batchJobDto.getClassName() 返回一个字符串,它是我的作业类的完全限定名称(“com.foo.bar.MyJob”或其他)。我创建触发器,从字符串中获取类并创建作业。从字符串获取类的顺序是:
日志记录是使用 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:
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.