问题描述
- Quartz怎么能不实现job接口而自己指定要执行的方法(就像sping中的那样)
-
如题这次代码中没用到spring 因此以前在spring中用下列方式实现
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">run
的如今不能用了 怎么才能简单的做使得quartz直接调用自己指定的方法而不是
必须要继承Job实现execute()方法
解决方案
写一个代理类,包装下,在它的execute里面由它读取配置文件反射创建对象
解决方案二:
用动态代理出错了
ErrorLogger - An error occured instantiating job to be executed. job= 'default.aa'
org.quartz.SchedulerException: Job instantiation failed
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:59) ~[spring-context-support-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:132) ~[quartz-1.6.1.jar:1.6.1]
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:386) [quartz-1.6.1.jar:1.6.1]
Caused by: java.lang.InstantiationException: com.sun.proxy.$Proxy28
at java.lang.Class.newInstance(Class.java:359) ~[na:1.7.0_51]
at org.springframework.scheduling.quartz.AdaptableJobFactory.createJobInstance(AdaptableJobFactory.java:77) ~[spring-context-support-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:55) ~[spring-context-support-3.1.3.RELEASE.jar:3.1.3.RELEASE]
... 2 common frames omitted
我的实现
Job batchJob = new BatchJob();
Job jobProxy = (Job) Proxy.newProxyInstance(batchJob.getClass().getClassLoader(),
batchJob.getClass().getInterfaces(),
new JobDetailInvocationHandler());
JobDetail jobDetail = new JobDetail();
jobDetail.setJobClass(jobProxy.getClass());
jobDetail.setName(triggerPO.getTriggerName());
jobDetail.setGroup(triggerPO.getTriggerGroupName());
jobDetail.setDescription(triggerPO.getDescription());
return jobDetail;