问题
I have created my own job class by extending the default class added couple of member variables and member methods. I saw the job gets triggered and is running for a very long time.
I just wanted to get the Job Instance not the JobDetail and wanted to invoke any member methods which has been defined by me or wanted to access the member variables.
Could you please let me know how we can achieve this?
Thanks, Kathir
回答1:
There is not such way, probably because quartz is meant to be compatible with a remote scheduling mode (i.e. cluster).
But if you are using it in a single server / application context, you could probably implement your own JobFactory (http://quartz-scheduler.org/api/2.1.5/org/quartz/simpl/SimpleJobFactory.html) that will just delegate to the super class the creation of the instance, and then register the instance somehow in a map or something else. Then you'll have to loop over the map keys to find the instance you are looking at.
Be careful of memory leaks with such a solution
Quick example:
In your spring configuration file:
<!-- quartz scheduler -->
<bean id="orchestration-api-quartz-factory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:spring/quartz.properties"/>
<property name="jobFactory">
<bean class="service.scheduler.SpringServiceJobFactory"/>
</property>
</bean>
And the basic implementation of your Factory:
/** * This job factory tries to locate the spring bean corresponding to the JobClass. * * @author Mathieu POUSSE */ public class SpringServiceJobFactory extends SpringBeanJobFactory implements JobFactory { @Autowired private ApplicationContext context; /** * {@inheritDoc} */ @Override protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception { // create or get the bean instance Object bean = this.context.getBean(bundle.getJobDetail().getJobClass()); // do what you want with the bean // but remeber what you did, otherwise if you forget to forget // you will have memory leaks !!! // myConcurrentMap.put("job.key", bean); return bean ; } }
HIH
来源:https://stackoverflow.com/questions/13620024/how-to-get-the-job-instance-of-long-running-jobs-in-quartz