用于管理任务的 Java 框架
我的问题是,Java中是否存在一个框架来管理和并发运行具有逻辑依赖性的任务。
我的任务如下: 我有很多独立的任务(比方说 A、B、C、D...),它们作为命令实现(就像命令模式一样)。我想要一种执行器,它将接受所有这些任务并以并行方式执行它们。 这些任务可以相互依赖(例如,在运行 A 之前我无法运行 C)、同步或异步。
我还想合并自定义启发式方法来影响调度程序的执行,例如,如果任务 A 和 B 是 CPU 密集型的,而 C 具有高内存消耗,则并行运行 A 和 C 是有意义的,而不是运行 A 和 B。
在自己深入构建这些东西之前(我正在考虑 java.util.concurrent + 基于注释的约束/规则),我想知道是否有人可以为我指出一些可以满足我的需求的项目。 预先非常感谢
my question is, whether there exists a framework in Java for managing and concurrently running Tasks that have logical dependencies.
My Task is as follows:
I have a lot of independent tasks (Let's say A,B,C,D...), They are implemented as Commands (like in Command pattern). I would like to have a kind of executor which will accept all these tasks and execute them in a parallel manner.
The tasks can be dependent one on another (For example, I can't run C, Before I run A), synchronous or asynchronous.
I would also like to incorporate the custom heuristics to affect the scheduler execution, for example if tasks A and B are CPU-intensive and C is, say, has high Memory consumption, It makes sense to run A and C in parallel, rather than running A and B.
Before diving into building this stuff by myself (i'm thinking about java.util.concurrent + annotation based constraints/rules), I was wondering, if someone could point me on some project that could suit my needs.
Thanks a lot in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为没有一个管理任务的框架可以满足您的要求。使用命令模式,您走在正确的道路上。您可以查看 Akka 框架 以了解简化的并发模型。 Akka是基于Actor模型的:
Akka 是用 Scala 编写的,但它公开了干净的 Java API。
I don't think that a there is a framework for managing tasks that could fulfill your requirements. You are on the right path using the Command pattern. You could take a look at the Akka framework for a simplified concurrency model. Akka is based on the Actor model:
Akka is written in Scala but it exposes clean Java API.
我建议您检查使用
ant
来实现此目的的可能性。尽管ant
被认为是一种流行的构建工具,但它实际上是运行各种任务的 XML 控制引擎。我认为它的标志fork=true
正是您所需要的:同时运行任务。由于任何 Java 应用程序ant
都可以从其他 Java 应用程序执行:只需调用其main
方法即可。在这种情况下,您可以使用 ant API 包装您的任务,即将它们实现为 Ant 任务。我从未尝试过这种方法,但我相信它应该有效。几年前我就考虑过这个问题,并向我的管理层建议它作为与您类似的问题的可能解决方案。
I'd recommend you to examine possibility to use
ant
for this purpose. Althoughant
is known as a popular build tool it actually the XML controlled engine that runs various tasks. I think that its flagfork=true
does exactly what you need: runs tasks concurrently. As any java applicationant
can be executed from other java application: just call itsmain
method. In this case you can wrap your tasks using ant API, i.e. implement them as Ant tasks.I have never try this approach but I believe it should work. I thought about it several years ago and suggested it to my management as a possible solution for problem similar to yours.
Eclipse 的作业调度模块能够处理相互依赖的任务。看看 http://www.eclipse.org/articles/Article -Concurrency/jobs-api.html。
Eclipse's job scheduling module is able to handle interdependent tasks. Take a look at http://www.eclipse.org/articles/Article-Concurrency/jobs-api.html.
有一个专门用于此目的的框架,称为 dexecutor (免责声明:我是Dexecutor
是一个非常轻量级的框架,可以以可靠的方式执行依赖/独立任务,为此它提供了最少的 API。
这是最简单的例子:
这是如何构建依赖图的
任务 1,12,11 将并行运行,一旦其中一个任务完成,相关任务就会运行,例如,假设任务 1 完成,一旦任务 12 完成,任务 2 和 3 将类似地运行,完成任务 13 将运行,依此类推。
There is a framework specifically for this purpose called dexecutor (Disclaimer : I am the owner)
Dexecutor is a very light weight framework to execute dependent/independent tasks in a reliable way, to do this it provides the minimal API.
Here is the simplest example :
Here is how the dependency graph would be constructed
Tasks 1,12,11 would run in parallel, once on of these tasks finishes dependent tasks would run, for example, lets say task 1 finishes, tasks 2 and 3 would run similarly once task 12, finishes task 13 would run and so on.