在 Quartz 的 JobDataMap 中传递 BlockingQueue [英] Pass BlockingQueue in JobDataMap of Quartz

查看:86
本文介绍了在 Quartz 的 JobDataMap 中传递 BlockingQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 BlockingQueue 传递给 Quartz 框架中的作业?我尝试使用 JobDataMap 传递 BlockingQueue 但这似乎不起作用.这里是相关的代码片段:

is there a way to pass a BlockingQueue to a job in the Quartz framework? I tried to use the JobDataMap for passing the BlockingQueue but that doesn't seem to work. Here the relevant code fragment:

JobDetail job = newJob(Jobby.class)
  .withIdentity("myJob", "group1")
  .usingJobData("buffer", queue)
  .build();

也许有人知道如何实现这一目标.

Perhaps someone has an idea on how to achieve this.

推荐答案

看起来您正在尝试实现生产者/消费者设计模式,其中生产者将工作放在队列中,而消费者使用 Quartz 定期检查该队列.这种设计还不错糟糕,但是您的实现将不起作用.通过将该队列传递给作业数据,Quartz 将对其进行序列化,并在作业触发时将其反序列化.队列不会通过引用传递,而是通过值(复制)传递.如果您意识到 Quartz 应该在分布式环境中工作,那么这是可以理解的,在该环境中,作业被调度并持久化到一个节点上的数据库中,然后反序列化并在另一个节点上执行.

Looks like you are trying to implement producer/consumer design pattern where producers are placing work in a queue and consumer checks that queue periodically using Quartz. This design isn't bad, however your implementation won't work. By passing that queue to the job data Quartz will serialize it and deserialize it back when jobs trigger. The queue won't be passed by reference but by value (copy). This is understandable if you realize that Quartz should work in a distributed environment, where job is scheduled and persisted to the database on one node and deserialized and executed on other node.

话虽如此,您需要重新评估您的实施.如果你不关心集群和分布,让你的队列以某种方式全局可用.您的选择是:

That being said you need to reevaluate your implementation. If you don't care about clustering and distribution, make your queue somehow globally available. Your options are:

  • 全局 - 请不要立>
  • 可通过
  • 获得资源
  • bean 注入生产者和作业类(Spring 很好地支持 Quartz)
  • ...
  • global singleton - please, no
  • resource available via jndi
  • spring bean injected to both producers and job class (Spring supports Quartz quite well)
  • ...

我特别推荐使用 ,

I specifically recommend using spring, jndi is also a nice option. But if we are talking about jndi - why not consider using some lightweight jms implementation instead of Quartz? You would avoid latency.

这篇关于在 Quartz 的 JobDataMap 中传递 BlockingQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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