您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何将实例变量传递给Quartz作业?

如何将实例变量传递给Quartz作业?

您可以将您的实例放在schedulerContext中。要计划作业时,可以在执行以下操作之前:

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,就像在Link中回答的那样

其他 2022/1/1 18:36:29 有403人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶