当另一个完成时我如何触发一项工作? [英] How do I trigger a job when another completes?

查看:137
本文介绍了当另一个完成时我如何触发一项工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两份工作,认为它们是超级简单的工作,只是打印一行,没有触发器或超时定义。当我通过控制器类调用它们时,它们工作正常:<我的类的名称> Job.triggerNow()



我曾尝试使用 quartzScheduler ,但我似乎无法从我的作业类中获得 JobDetail ,所以我不确定什么是正确的方式这样做。我也想将第一份工作的一些结果传递给第二份工作。



我知道我可以触发第二份工作作为第一份工作的执行方法的最后一行,但这是不可取的,因为它在技术上不是第一份工作的一部分,而且比我想要的更多。



任何帮助将不胜感激。谢谢

解决方案

这听起来像你之后是一个异步的管道工作,其中有不同的工作人员都在一条线并将数据从一个传递到另一个传递。这种体系结构非常灵活,适用于大量常见应用程序。



最好的方法是我发现获得这样的体系结构与Grails是使用消息队列,例如 RabbitMQ ,然后让控制器将消息放入管道的第一步。

然后,你有一名工作人员(如果你使用优秀的,只需在Grails应用程序中提供服务)即可收听为他们工作的队列。当工作进入队列时,工作人员将会关闭作业,处理作业,然后将消息放入管道中下一步的队列中。



I我们发现这是构建几乎任何异步管道的最佳方式,因为它允许您根据需要分别扩展每个部分,并且没有太多的开销。还有一种方法可以将作业与流水线中的下一步分离开来,但我发现在大多数情况下,这并不是真的需要,只会增加无用的复杂性。 p>

Quartz非常适合需要按计划进行的工作,但流水线在处理事务方面更好,因为它以可扩展的方式进行工作。


I have two jobs, consider them to be the super simple jobs that just print a line and have no triggers or timeouts defines. They work fine when I call them from a controller class through: <name of my class>Job.triggerNow()

What I want is to trigger one job and, as it as it finishes, trigger a consequent different job.

I have tried using the quartzScheduler, but I can't seem to get a JobDetail from my job classes, so I'm not sure what is the correct way for doing this. I also want to pass some results from the first job onto the second one.

I know I can trigger the second job as the last line on my first job's execute method, but this is not desirable since its technically not part of the first job and couples things more than I would like.

Any help will be greatly appreciated. thanks

解决方案

What it sounds like you are after is an asynchronous "pipeline" of work where there are different workers that are all in a line and pass data to be worked on from one to the next. This sort of architecture is amazingly flexible and applies to a large number of very common applications

The best way that I have found to get such an architecture in place with Grails is to use a message queue, like RabbitMQ for example, with a series of queues (one for each step in the pipeline), and then have the controller(s) put messages into the first step of the pipeline.

Then, you have a worker (just a service within the Grails app if you use the excellent RabbitMQ Grails plugin) listen to the queue that holds jobs for them to work on. As work comes into the queue, the worker will pop the job off, processes it, and then put a message into the queue of the next step in the pipeline.

I've found this to be the best way to architect just about any asynchronous pipeline, since it allows you to scale each piece separately as needed and doesn't have too much overhead. There are also ways to decouple the jobs from having to know about the next step in the pipeline, but I've found that in most cases this isn't really needed and just adds useless complexity.

Quartz is great for jobs that need to happen on a schedule, but a pipeline is much better at processing things as it comes in in a scaleable way

这篇关于当另一个完成时我如何触发一项工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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