使用 Quartz 的动态作业数据

发布于 2024-10-22 14:17:59 字数 236 浏览 6 评论 0原文

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

根据我所读到的内容,我应该能够将具有相似属性的不同值的 JobDataMap 分配给单个 Job 类,但我无法找到任何文档或示例来支持此用例。也许我的谷歌能力很弱。

这有道理吗?有一个 Job 类并以某种方式存储 JobDataMap 来填充该 Job 类并在每条消息的基础上运行它?

When my process gets a message, it needs to start a timer and execute some logic in X seconds. These jobs need to be stored in a JDBC store, which as far as I can tell may be irrelevant to this question.

Based on what I've read, I should be able to assign a JobDataMap with different values for similar properties to a single Job class, but I'm unable to find any documentation or examples to back this use-case. Perhaps my Google-fu is weak.

Does that make sense? Have one Job class and somehow store a JobDataMap to populate that Job class and run it on a per-message basis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2024-10-29 14:18:00

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");
}

教程中有关 JobDataMap 的文档

org.quartz.Trigger has both getJobDataMap() (which will new up one if necessary) and setJobDataMap() to get the trigger's JobDataMap.

The simplest usage would be:

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

To init with an existing Map of values:

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

t.setJobDataMap(new JobDataMap(data));

To get the data when your job executes

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

Documentation on JobDataMap in the tutorial.

╭⌒浅淡时光〆 2024-10-29 14:18:00

嗨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("lanzadorprincipal", "lanzadores", "instruccion","instrucciones" , Calendar.getInstance().getTime(), null, 100, 10000);

您可以设置执行和重复的时间吗?

Hi cansando (you speak spanish???).
I going to give you my example of 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;
    }

}

In this line SimpleTrigger trigger2 = new SimpleTrigger("lanzador principal", "lanzadores", "instruccion","instrucciones" , Calendar.getInstance().getTime(), null, 100, 10000);

can you set the time to execute and repeat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文