本文介绍了Quartz JobDataMap 不适用于非原始类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Quartz JobDataMap 有以下问题.我希望当使用简单的 Quartz Job 并将非原始对象(例如 StringBuilder 的实例)传递给 JobDateMap 时,方法 execute(来自我的工作)应该总是用我放置的不同副本来调用.不幸的是,我总是得到我放入 JobDateMap 的对象实例(就像它是一个 StatefulJob).

I have a following problem with Quartz JobDataMap. I expect that when using simple Quartz Job and passing not-primitive object (e.g. instance of StringBuilder) into JobDateMap, method execute (from my job) should be always invoked with different copy of objected I put. Unfortunately I always get instance of object I put into JobDateMap (like it would be a StatefulJob).

在下面的示例中,我希望在每次调用中都得到一个*",而每次我都会得到一个*".

In bellow example I expect to get single '*' in every invocation while I get one more '*' every time.

public class MyJob implements Job {

    public static void main(String[] args) throws SchedulerException {

        SchedulerFactory schedFact = new StdSchedulerFactory();
        Scheduler sched = schedFact.getScheduler();

        JobDetail jobDetail = new JobDetail("job", Scheduler.DEFAULT_GROUP, MyJob.class);
        jobDetail.getJobDataMap().put("param", new StringBuilder());

        Trigger trigger = TriggerUtils.makeImmediateTrigger("trigger", 10, 100);
        trigger.setGroup(Scheduler.DEFAULT_GROUP);

        sched.scheduleJob(jobDetail, trigger);
        sched.start();

        try {
            Thread.sleep(1000L);
        } catch (Exception e) {}

        sched.shutdown(true);

    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        StringBuilder sb = (StringBuilder) context.getMergedJobDataMap().get("param");
        sb.append("*");
        System.out.println(sb.toString());

    }
}

我想,我缺少有关 Quartz 工作方式的一些信息.有人知道吗?

I think, I'm missing something about how Quartz is working. Anybody knows what?

推荐答案

来源:http:///www.quartz-scheduler.org/documentation/2.3.1-SNAPSHOT/best-practices.html#jobdatamap-tips

这篇关于Quartz JobDataMap 不适用于非原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 01:05