在java中执行依赖任务

发布于 2024-10-06 00:31:43 字数 296 浏览 6 评论 0原文

我需要找到一种方法来执行相互依赖的任务。

  • 第一个任务必须从远程服务器下载 zip 文件。
  • 第二个任务的目标是解压缩第一个任务下载的文件。
  • 第三个任务必须处理从 zip 中提取的文件。

因此,第三个任务依赖于第二个任务,第二个任务依赖于第一个任务。 当然,如果其中一项任务失败,则不应执行其他任务。由于第一个任务从远程服务器下载文件,因此应该有一种在服务器不可用时重新启动任务的机制。 任务必须每天执行。

有什么建议、模式或java API吗?

问候!

I need to find a way to execute mutually dependent tasks.

  • First task has to download a zip file from remote server.
  • Second tasks goal is to unzip the file downloaded by the first task.
  • Third task has to process files extracted from zip.

so, third is dependent on second and second on first task.
Naturally if one of the tasks fails, others shouldn't be executed. Since the first task downloads files from remote server, there should be a mechanism for restarting the task is server is not available.
Tasks have to be executed daily.

Any suggestions, patterns or java API?

Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

情未る 2024-10-13 00:31:44

这在一定程度上取决于外部要求。有用户参与吗?监控?警报?...

最简单的显然只是检查前一个是否已完成其应做的事情的方法。

  1. download() 将文件下载到指定位置。
  2. 如果下载的文件就位,unzip() 将文件解压到指定位置。
  3. process() 处理已提取的数据。

一种更“正式”的方法是使用工作流引擎。根据需求,您可以获得从精美的 UI 到遵循工作流的正式标准化 .XML 定义的一些功能 - 以及介于两者之间的任何功能。

http://java-source.net/open-source/workflow-engines

It depends a bit on external requirements. Is there any user involvement? Monitoring? Alerting?...

The simplest would obviously be just methods that check if the previous has done what it should.

  1. download() downloads file to specified place.
  2. unzip() extracts the file to a specified place if the downloaded file is in place.
  3. process() processes the data if it has been extracted.

A more "formal" way of doing it would be to use a workflow engine. Depending on requirements, you can get some that do everything from fancy UIs, to some that follow formal standardised .XML-definitions of the workflow - and any in between.

http://java-source.net/open-source/workflow-engines

风铃鹿 2024-10-13 00:31:44

创建一个公共方法来执行任务的完整链和私有方法:

public void doIt() {
  if (download() == false) {
     // download failed
  } else if (unzip() == false) {
     // unzip failed;
  } else (process() == false)
     // processing failed
  }
}

private boolean download() {/* ... */}
private boolean unzip() {/* ... */}
private boolean process() {/* ... */}

因此,您有一个 API 可以保证所有步骤都按正确的顺序执行,并且仅在满足某些条件时才执行步骤(上面的示例仅说明了这个图案)

Create one public method to execute the full chain and private methods for the tasks:

public void doIt() {
  if (download() == false) {
     // download failed
  } else if (unzip() == false) {
     // unzip failed;
  } else (process() == false)
     // processing failed
  }
}

private boolean download() {/* ... */}
private boolean unzip() {/* ... */}
private boolean process() {/* ... */}

So you have an API that gurantees that all steps are executed in the correct sequence and that a step is only executed if certain conditions are met (the above example just illustrates this pattern)

寄居人 2024-10-13 00:31:44

对于日常执行,您可以使用 Quartz 框架

由于任务相互依赖,我建议评估任务返回的错误代码或异常。如果之前的任务成功,则继续。

For daily execution you can use the Quartz Framework.

As the tasks are depending on each other I would recommend to evaluate the error codes or exceptions the tasks are returning. Then just continue if the previous task was successful.

秋凉 2024-10-13 00:31:44

执行这些任务的正常方法是:按顺序调用每个任务,并在出现阻止执行以下任务的失败时抛出异常。像这样的东西

try {
  download();
  unzip();
  process();
} catch(Exception failed) {
  failed.printStackTrace();
}

The normal way to perform these tasks is to; call each task in order, and throw an exception when you have a failure which prevents the following tasks being performed. Something like

try {
  download();
  unzip();
  process();
} catch(Exception failed) {
  failed.printStackTrace();
}
可是我不能没有你 2024-10-13 00:31:44

我认为您感兴趣的是某种事务定义。

- 定义TaskA(例如下载)
- 定义TaskB(例如解压缩)
- 定义TaskC(例如进程)
假设您的目的是让任务也独立工作,例如仅下载文件(不执行TaskB,TaskC),您应该定义由TaskA,TaskB,TaskCTransaction1 > 或仅由 TaskA 组成的 Transaction2
例如,关于 Transaction1 的语义,即 TaskA、TaskB 和 TaskC 应按顺序执行,并且可以在事务定义中捕获所有或没有捕获。
定义可以在 xml 配置文件中,您可以使用 Quartz 等框架进行调度。
更高的构造应检查事务并按定义执行它们。

I think what you are interested in is some kind of transaction definition.
I.e.
- Define TaskA (e.g. download)
- Define TaskB (e.g. unzip)
- Define TaskC (e.g. process)
Assuming that you intention is to have tasks working independent as well, e.g. only download a file (not execute also TaskB, TaskC) you should define Transaction1 composed of TaskA,TaskB,TaskC or Transaction2 composed of only TaskA.
The semantics e.g. concerning Transaction1 that TaskA,TaskB and TaskC should be executed sequentially and all or none could be captured in your transaction definitions.
The definitions can be in xml configuration files and you can use a framework e.g. Quartz for scheduling.
A higher construct shall check for the transactions and execute them as defined.

小梨窩很甜 2024-10-13 00:31:44

使用 Dexecutor 可以轻松执行依赖任务

免责声明:我是该库的所有者

基本上你需要以下模式

在此处输入图像描述

使用 Dexecutor.addDependency 方法

 DefaultDexecutor<Integer, Integer> executor = newTaskExecutor();
//Building
executor.addDependency(1, 2);
executor.addDependency(2, 3);
executor.addDependency(3, 4);
executor.addDependency(4, 5);
//Execution
executor.execute(ExecutionConfig.TERMINATING);

Dependent tasks execution made easy with Dexecutor

Disclaimer : I am the owner of the library

Basically you need the following pattern

enter image description here

Use Dexecutor.addDependency method

 DefaultDexecutor<Integer, Integer> executor = newTaskExecutor();
//Building
executor.addDependency(1, 2);
executor.addDependency(2, 3);
executor.addDependency(3, 4);
executor.addDependency(4, 5);
//Execution
executor.execute(ExecutionConfig.TERMINATING);
余生一个溪 2024-10-13 00:31:43

看来你不想将它们分成任务,只需这样做:

process(unzip(download(uri)));

It seems that you do not want to devide them into tasks, just do like this:

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