使用Quartz-scheduler在Jobs中的自定义对象参数 [英] Custom Object parameters within Jobs using Quartz-scheduler

查看:837
本文介绍了使用Quartz-scheduler在Jobs中的自定义对象参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试Quartz来安排工作。但是,该作业包含3个不可序列化的参数。

I am testing out Quartz to schedule a job. However the job contains 3 non-serializable parameters.

我在下面创建了一个示例应用程序,指示我如何实现该功能。有谁知道如何使用Quartz将自定义对象用作参数?

I have created a sample application below indicating how I am implementing the functionality. Does anyone know how I can use custom objects as parameters using Quartz?

下面是调度作业的触发器,我已经评论了给我发问题的区域。 / p>

Below is the trigger which schedules the job, I have commented the area which is giving me issues.

public class Trigger {

public void run() throws Exception {

    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();
    Date startTime = DateBuilder.nextGivenSecondDate(null, 15);


    JobDetail job = newJob(SimpleJob.class)
            .withIdentity("job6", "group1")
            .build();

    SimpleTrigger trigger = newTrigger()
            .withIdentity("trigger6", "group1")
            .startAt(startTime)
            .withSchedule(simpleSchedule()
                    .withIntervalInSeconds(60)
                    .repeatForever())
            .build();

    Date ft = sched.scheduleJob(job, trigger);

    TestObject testObject = new TestObject();

    // This is the part giving trouble!
    job.getJobDataMap().put(SimpleJob.test,testObject);

    sched.start();
}

}

这是我要安排的工作。

public class SimpleJob implements Job {

public static final TestObject test = null;

public SimpleJob() {

}

public void execute(JobExecutionContext context) throws JobExecutionException {

    test.saySomething();
}

}

最后,TestObject类。

And finally, the TestObject class.

public class TestObject {

public TestObject() {

}

public void saySomething() {

    System.out.println("Test Object initialized");
}

}

请注意,我只是想找到一种方法让Quartz允许非序列化对象用作参数(请不要评论上面执行的实际任务或工作)

Please notice, I am only looking for a way to get Quartz to allow non-serializable objects to be used as a paramater (please do not comment on the actual task or job that is being carried out above)

我也试过为TestObject实现Serializable接口,并没有快乐。

I have also tried implementing the Serializable interface for the TestObject aswell, and no joy.

任何帮助都将不胜感激。
谢谢。

Any help would be greatly appreciated. Thanks you.

推荐答案

实施自己的 JobFactory 。它将自定义注入所需属性的底层Job。

Implement your own JobFactory. It will customize the underlying Job injecting any properties you need.

class MyJobFactory extends SimpleJobFactory {
    @Override
    public Job newJob(TriggerFiredBundle bundle, Scheduler Scheduler) throws SchedulerException {
        SimpleJob job = (SimpleJob) super.newJob(bundle, Scheduler);
        job.setTestObject(testObject);
        return job;
    }
}

您仍需要使用JobDetails通知班级你的工作,你需要修改调度程序以使用你的工厂。

You still need to use JobDetails to inform the class of your job, and you need to modify the scheduler to use your factory.

Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.setJobFactory(new MyJobFactory());
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();

这篇关于使用Quartz-scheduler在Jobs中的自定义对象参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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