简体   繁体   English

同步运行Quartz作业

[英]Run Quartz job synchronous

I have a situation that I don't know how should I handle it. 我有一种情况,我不知道该如何处理。

I have a job in quartz which should start another nested job and get some results of the nested job and process them. 我有一个quartz工作,应该开始另一个嵌套工作,并获得嵌套工作的一些结果并进行处理。

As I know, quartz run its job asynchronously. 据我所知, quartz是异步运行的。 So, the thread which start the nested job cannot wait until the result of nested job being passed. 因此,启动嵌套作业的线程不能等到传递嵌套作业的结果。

What is the solution here? 这里有什么解决方案?

First of all quartz has a thread pool for running its jobs. 首先,石英有一个用于运行其作业的线程池。 You can read about it here : 您可以在这里阅读有关内容:

In addition keep in mind that quartz can run jobs even in distributed manner. 另外请记住,石英甚至可以以分布式方式运行作业。 It allows to be run in cluster of 'active-active' servers, in this case you can't do any assumption on which of servers the job can be triggered. 它允许在“主动-主动”服务器群集中运行,在这种情况下,您无法假设可以触发作业的服务器。 So implementing a singleton as jbh has stated can be tricky (at least you have been warned now :) ) 因此,按照jbh所述实现单例可能很棘手(至少现在已经警告您了:))

In general, I think quartz should do what its good for - to run jobs. 总的来说,我认为石英应做其有益的工作。 I don't think that implementing any complicated logic on top of this is a good idea. 我认为在此基础上实现任何复杂的逻辑都不是一个好主意。 From your question I understand that you're trying to run one job from another (say job A runs job B). 根据您的问题,我了解到您正在尝试运行另一个作业(例如,作业A运行作业B)。 This makes sense if you have different trigger(s) assigned to A and B (otherwise why should B implemented as a quartz job?). 如果您为A和B分配了不同的触发器,那么这是有道理的(否则为什么B应该实现为石英作业?)。 So maybe you can refactor your code of job B so that the logic executed in B will be implemented in some class which is not related to quartz(say class C)? 因此,也许您可​​以重构作业B的代码,以便在与石英无关的某个类(例如C类)中实现在B中执行的逻辑? In this case you could assign different triggers to jobs A and B but inside just execute the code in class C? 在这种情况下,您可以为作业A和B分配不同的触发器,但是在内部仅执行类C中的代码?

Example (your approach): 示例(您的方法):

class A implements Job {
  public void execute(JobExecutionContext context) throws JobExecutionException {
      // do A's stuff
      // call Job B somehow (???)
  }
}


class B implements Job {
  public void execute(JobExecutionContext context) throws JobExecutionException {
      // do something
  }

} }

My suggestion: 我的建议:

class C {
   doSomeLogic(...) {...}
}


class A implements Job {
         public void execute(JobExecutionContext context) throws   JobExecutionException {
        // do A's stuff
        C c = new C();
        c.execute();  
  }


class B implements Job {
         public void execute(JobExecutionContext context) throws JobExecutionException {            
        C c = new C();
        c.execute();  
  }

} }

Hope this helps. 希望这可以帮助。

Few ways. 几种方法。 You can make some Static/Singleton type class that holds some sort of semaphore/session variables. 您可以创建一些Static / Singleton类型的类,其中包含某种信号量/会话变量。 Have the master job loop while checking this semaphore (synchronized for thread safety). 检查此信号量时(同步线程安全性)使主作业循环。 And then retrieve the data similarly. 然后类似地检索数据。

You can also utilize JobDataMap in a similar fashion. 您也可以以类似方式使用JobDataMap。 JobDataMap example JobDataMap示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM