使用Quartz的动态作业数据

时间:2011-03-17 19:47:25

标签: java spring quartz-scheduler

当我的进程收到消息时,它需要启动一个计时器并在X秒内执行一些逻辑。这些作业需要存储在JDBC存储中,据我所知,这可能与此问题无关。

根据我读过的内容,我应该能够为一个Job类分配一个具有不同类似属性值的JobDataMap,但是我无法找到任何文档或示例来支持这个用例。也许我的Google-fu很弱。

这有意义吗?有一个Job类,并以某种方式存储JobDataMap来填充该Job类并按消息运行它?

2 个答案:

答案 0 :(得分:7)

org.quartz.Trigger同时包含getJobDataMap()(必要时new增加一个)和setJobDataMap()获取触发器JobDataMap

最简单的用法是:

Trigger t = new SimpleTrigger(...);
t.getJobDataMap().put("foo", "bar");

使用现有的值映射进行初始化:

Map data = new HashMap();
data.put("foo", "bar");

t.setJobDataMap(new JobDataMap(data));

在作业执行时获取数据

public void execute(JobExecutionContext context) throws JobExecutionException
{
   String fooValue = context.getMergedJobDataMap().get("foo");
}

教程中的

Documentation on JobDataMap

答案 1 :(得分:0)

嗨cansando(你讲西班牙语???)。 我将举例说明Quartz。

public static void main(String[] args) {

        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
        try {

            Scheduler sched;
            sched = schedFact.getScheduler();

            sched.start();

            JobDetail jobd = new JobDetail("instruccion", "instrucciones",
                    Mensaje.class);

//          TriggerExample trigger = new TriggerExample("instruccion",
//                  "instrucciones");

            jobd.setRequestsRecovery(true);
            SimpleTrigger trigger2 = new SimpleTrigger("lanzador principal", "lanzadores", "instruccion","instrucciones" , Calendar.getInstance().getTime(), null, 100, 10000);

            trigger2.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
            sched.scheduleJob(jobd, trigger2);

            //      
            // }
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    };


public class Mensaje implements Job {


    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Hola Mundo");

    }

}


public class TriggerExample extends Trigger {

    /**
     * 
     */
    private static final long serialVersionUID = -4926331843008217753L;

    Date fechaAEjecutar;


    public TriggerExample(){

        super();
    }

    public TriggerExample(String name){
        super(name);
    }

    public TriggerExample(String name, String group){
        super(name, group);
    }

    public TriggerExample(String name, String group,String jobname, String jobgroup){
        super(name,group,jobname,jobgroup);
    }



    @Override
    public Date computeFirstFireTime(Calendar arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int executionComplete(JobExecutionContext arg0,
            JobExecutionException arg1) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Date getEndTime() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getFinalFireTime() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getFireTimeAfter(Date arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getNextFireTime() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getPreviousFireTime() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getStartTime() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean mayFireAgain() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void setEndTime(Date arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setStartTime(Date arg0) {
        this.fechaAEjecutar = arg0;
        System.out.println("Cargue la fecha el data con valor\n");
        System.out.println(this.fechaAEjecutar.getTime());

    }

    @Override
    public void triggered(Calendar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void updateAfterMisfire(Calendar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void updateWithNewCalendar(Calendar arg0, long arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean validateMisfireInstruction(int arg0) {
        // TODO Auto-generated method stub
        return false;
    }

}

在这一行中SimpleTrigger trigger2 = new SimpleTrigger(“lanzador principal”,“lanzadores”,“instruccion”,“instrucciones”,Calendar.getInstance()。getTime(),null,100,10000);

可以设置执行和重复的时间。