如何在 Quartz 中获取长时间运行的作业的作业实例? [英] How to get the Job Instance of Long Running Jobs in Quartz?

查看:79
本文介绍了如何在 Quartz 中获取长时间运行的作业的作业实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过扩展默认类添加了几个成员变量和成员方法,创建了自己的作业类.我看到作业被触发并运行了很长时间.

我只是想获得 Job Instance 而不是 JobDetail,并且想调用任何由我定义或想要访问成员变量的成员方法.

能否请您告诉我我们如何实现这一目标?

谢谢,凯瑟尔

解决方案

没有这种方式,可能是因为quartz是为了兼容远程调度模式(即集群).

但是如果您在单个服务器/应用程序上下文中使用它,您可能可以实现自己的 JobFactory (http://quartz-scheduler.org/api/2.1.5/org/quartz/simpl/SimpleJobFactory.html) 将委托给超类创建实例,然后以某种方式在地图或其他东西中注册实例.然后,您必须遍历映射键才能找到您正在查看的实例.

注意这种解决方案的内存泄漏

<小时>

快速示例:

在你的 spring 配置文件中:

<bean id="orchestration-api-quartz-factory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="configLocation" value="classpath:spring/quartz.properties"/><属性名称=jobFactory"><bean class="service.scheduler.SpringServiceJobFactory"/></属性></bean>

以及你工厂的基本实现:

<前>/*** 这个作业工厂尝试定位到JobClass对应的spring bean.** @author Mathieu POUSSE*/公共类 SpringServiceJobFactory 扩展 SpringBeanJobFactory 实现 JobFactory {@自动连线私有 ApplicationContext 上下文;/*** {@inheritDoc}*/@覆盖受保护的对象 createJobInstance(最终 TriggerFiredBundle 包)抛出异常{//创建或获取 bean 实例对象 bean = this.context.getBean(bundle.getJobDetail().getJobClass());//用 bean 做你想做的事//但要记住你做了什么,否则如果你忘记了//你会有内存泄漏!!!//myConcurrentMap.put("job.key", bean);返回豆;}}

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

解决方案

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

这篇关于如何在 Quartz 中获取长时间运行的作业的作业实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆