如何将实例变量传递给Quartz作业? [英] How to pass instance variables into Quartz job?

查看:150
本文介绍了如何将实例变量传递给Quartz作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Quartz中外部传递一个实例变量?

I wonder how to pass an instance variable externally in Quartz?

下面是我想编写的伪代码。如何将externalInstance传递给此作业?

Below is pseudo code I would like to write. How can I pass externalInstance into this Job?

public class SimpleJob implements Job {
        @Override
        public void execute(JobExecutionContext context)
                throws JobExecutionException {

            float avg = externalInstance.calculateAvg();
        }
}


推荐答案

你可以把你的实例放在schedulerContext中。当你打算安排这份工作时,你可以在下面做:

you can put your instance in the schedulerContext.When you are going to schedule the job ,just before that you can do below:

getScheduler().getContext().put("externalInstance", externalInstance);

您的工作类如下:

public class SimpleJob implements Job {
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        SchedulerContext schedulerContext = null;
        try {
            schedulerContext = context.getScheduler().getContext();
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }
        ExternalInstance externalInstance =
            (ExternalInstance) schedulerContext.get("ExternalInstance");

        float avg = externalInstance.calculateAvg();
    }
}

如果您使用Spring,您实际上可以使用spring的支持注入整个applicationContext,如链接中所述

If you are using Spring ,you can actually using spring's support to inject the whole applicationContext like answered in the Link

这篇关于如何将实例变量传递给Quartz作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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