quartz - java.lang.ClassCastException org.quartz.JobDetail无法使用JobStoreTX强制转换为ScheduledJobDetail

时间:2010-07-27 06:10:40

标签: java quartz-scheduler classcastexception

我正在使用石英1.6.5。我最近从Quartj Job Store从RamJobStore搬到了org.quartz.impl.jdbcjobstore.JobStoreTX。从那以后,每当quartz尝试运行一个作业时,我就开始在日志中看到 ClassCastException

Jul 26, 2010 3:10:00 AM org.quartz.core.ErrorLogger schedulerError
SEVERE: Job (group52.52 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.ClassCastException: org.quartz.JobDetail cannot be cast to ScheduledJobDetail]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: java.lang.ClassCastException: org.quartz.JobDetail cannot be cast to ScheduledJobDetail
        at ScheduledJob.execute(ScheduledJob.java:150)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        ... 1 more

我的quartz.properties看起来像:

org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.dsjta.jndiURL=myDs
org.quartz.jobStore.useProperties=true

我使用以下代码初始化我的调度程序: org.quartz.Scheduler quartzScheduler = new StdSchedulerFactory("quartz.properties").getScheduler(); 相同的代码适用于RAMJobStore,但不适用于JobStoreTX。我在这里可以缺少什么? 我启用了日志记录,这是我在日志中找到的:

136672 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.impl.jdbcjobstore.SimpleSemaphore  - Lock 'TRIGGER_ACCESS' is desired by: DefaultQuartzScheduler_QuartzSchedulerThread
136672 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.impl.jdbcjobstore.SimpleSemaphore  - Lock 'TRIGGER_ACCESS' is being obtained: DefaultQuartzScheduler_QuartzSchedulerThread
136672 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.impl.jdbcjobstore.SimpleSemaphore  - Lock 'TRIGGER_ACCESS' given to: DefaultQuartzScheduler_QuartzSchedulerThread
136703 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.impl.jdbcjobstore.SimpleSemaphore  - Lock 'TRIGGER_ACCESS' retuned by: DefaultQuartzScheduler_QuartzSchedulerThread
136703 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.SimpleJobFactory  - Producing instance of Job 'group152.152', class=ScheduledJob
136703 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.core.JobRunShell  - Calling execute on job group152.152
136703 [DefaultQuartzScheduler_Worker-3] ERROR org.quartz.core.JobRunShell  - Job group152.152 threw an unhandled Exception:
java.lang.ClassCastException: org.quartz.JobDetail cannot be cast to ScheduledJobDetail
        at ScheduledJob.execute(SchedulerQuartzImpl.java:150)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
136703 [DefaultQuartzScheduler_Worker-3] ERROR org.quartz.core.ErrorLogger  - Job (group152.152 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.ClassCastException: org.quartz.JobDetail cannot be cast to ScheduledJobDetail]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:227)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.lang.ClassCastException: org.quartz.JobDetail cannot be cast to ScheduledJobDetail
        at ScheduledJob.execute(SchedulerQuartzImpl.java:150)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
        ... 1 more
136703 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.impl.jdbcjobstore.SimpleSemaphore  - Lock 'TRIGGER_ACCESS' is desired by: DefaultQuartzScheduler_Worker-3
136703 [DefaultQuartzScheduler_Worker-3] DEBUG org.quartz.impl.jdbcjobstore.SimpleSemaphore  - Lock 'TRIGGER_ACCESS' is being obtained: DefaultQuartzScheduler_Worker-3

更新:我尝试使用org.quartz.impl.jdbcjobstore.JobStoreCMT时仍然存在同样的问题。

1 个答案:

答案 0 :(得分:0)

问题可能在于你已经将JobDetail子类化了。

当您使用RAMJobStore时,您的ScheduledJobDetail个对象会保留在内存中,因此您可以毫无问题地从JobDetail转回来。但是,当使用数据库JobStore时,Quartz将重建JobDetail对象本身,并且它无法知道您希望它为此使用自定义类,因此您获得异常。

即使Quartz确实知道使用ScheduledJobDetail,它也无法处理您添加的其他字段。

子类化JobDetail不是要走的路。您需要找到一些其他编码此信息的方法,而无需进行子类化。