Pass BlockingQueue in JobDataMap of Quartz

送分小仙女□ 提交于 2019-12-24 10:56:49

问题


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.


回答1:


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:

  • 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.



来源:https://stackoverflow.com/questions/10880926/pass-blockingqueue-in-jobdatamap-of-quartz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!