用于管理任务的 Java 框架

发布于 2024-10-16 05:48:29 字数 374 浏览 6 评论 0原文

我的问题是,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等往事风中吹 2024-10-23 05:48:29

我认为没有一个管理任务的框架可以满足您的要求。使用命令模式,您走在正确的道路上。您可以查看 Akka 框架 以了解简化的并发模型。 Akka是基于Actor模型的:

演员模型是另一个非常简单的
高级并发模型:参与者
无法回复多条消息
一次(消息排队到
邮箱)并且只能通过
发送消息,而不是共享
变量。只要消息是
不可变的数据结构(即
在 Erlang 中总是正确的,但必须是
没有手段的语言约定
确保此属性),一切
是线程安全的,不需要任何
其他机制。这非常相似
请求在网络中找到的周期
开发MVC框架。
http://metaphysicaldeveloper。 wordpress.com/2010/12/16/high-level-concurrency-with-jruby-and-akka-actors/

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:

The actor model is another very simple
high level concurrency model: actors
can’t respond to more than one message
at a time (messages are queued into
mailboxes) and can only communicate by
sending messages, not sharing
variables. As long as the messages are
immutable data structures (which is
always true in Erlang, but has to be a
convention in languages without means
of ensuring this property), everything
is thread-safe, without need for any
other mechanism. This is very similar
to request cycle found in web
development MVC frameworks.
http://metaphysicaldeveloper.wordpress.com/2010/12/16/high-level-concurrency-with-jruby-and-akka-actors/

Akka is written in Scala but it exposes clean Java API.

七堇年 2024-10-23 05:48:29

我建议您检查使用 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. Although ant is known as a popular build tool it actually the XML controlled engine that runs various tasks. I think that its flag fork=true does exactly what you need: runs tasks concurrently. As any java application ant can be executed from other java application: just call its main 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.

尹雨沫 2024-10-23 05:48:29

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.

烟花易冷人易散 2024-10-23 05:48:29

有一个专门用于此目的的框架,称为 dexecutor (免责声明:我是Dexecutor

是一个非常轻量级的框架,可以以可靠的方式执行依赖/独立任务,为此它提供了最少的 API。

  • 一个用于在图中添加节点的 API(addDependency、addIndependent、addAsDependentOnAllLeafNodes、addAsDependencyToAllInitialNodes 后两个是前两个的混合版本)
  • ,另一个用于按顺序执行节点。

这是最简单的例子:

DefaultDependentTasksExecutor<Integer, Integer> executor = newTaskExecutor();

executor.addDependency(1, 2);
executor.addDependency(1, 2);
executor.addDependency(1, 3);
executor.addDependency(3, 4);
executor.addDependency(3, 5);
executor.addDependency(3, 6);
//executor.addDependency(10, 2); // cycle
executor.addDependency(2, 7);
executor.addDependency(2, 9);
executor.addDependency(2, 8);
executor.addDependency(9, 10);
executor.addDependency(12, 13);
executor.addDependency(13, 4);
executor.addDependency(13, 14);
executor.addIndependent(11);

executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

这是如何构建依赖图的
输入图片此处的描述

任务 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.

  • An API to add nodes in the graph (addDependency, addIndependent, addAsDependentOnAllLeafNodes, addAsDependencyToAllInitialNodes Later two are the hybrid version of the first two)
  • and the other to execute the nodes in order.

Here is the simplest example :

DefaultDependentTasksExecutor<Integer, Integer> executor = newTaskExecutor();

executor.addDependency(1, 2);
executor.addDependency(1, 2);
executor.addDependency(1, 3);
executor.addDependency(3, 4);
executor.addDependency(3, 5);
executor.addDependency(3, 6);
//executor.addDependency(10, 2); // cycle
executor.addDependency(2, 7);
executor.addDependency(2, 9);
executor.addDependency(2, 8);
executor.addDependency(9, 10);
executor.addDependency(12, 13);
executor.addDependency(13, 4);
executor.addDependency(13, 14);
executor.addIndependent(11);

executor.execute(ExecutionBehavior.RETRY_ONCE_TERMINATING);

Here is how the dependency graph would be constructed
enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文