在java中执行依赖任务
我需要找到一种方法来执行相互依赖的任务。
- 第一个任务必须从远程服务器下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这在一定程度上取决于外部要求。有用户参与吗?监控?警报?...
最简单的显然只是检查前一个是否已完成其应做的事情的方法。
download()
将文件下载到指定位置。unzip()
将文件解压到指定位置。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.
download()
downloads file to specified place.unzip()
extracts the file to a specified place if the downloaded file is in place.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
创建一个公共方法来执行任务的完整链和私有方法:
因此,您有一个 API 可以保证所有步骤都按正确的顺序执行,并且仅在满足某些条件时才执行步骤(上面的示例仅说明了这个图案)
Create one public method to execute the full chain and private methods for the tasks:
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)
对于日常执行,您可以使用 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.
执行这些任务的正常方法是:按顺序调用每个任务,并在出现阻止执行以下任务的失败时抛出异常。像这样的东西
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
我认为您感兴趣的是某种事务定义。
即
- 定义
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 ofTaskA,TaskB,TaskC
orTransaction2
composed of onlyTaskA
.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.
使用 Dexecutor 可以轻松执行依赖任务
免责声明:我是该库的所有者
基本上你需要以下模式
使用 Dexecutor.addDependency 方法
Dependent tasks execution made easy with Dexecutor
Disclaimer : I am the owner of the library
Basically you need the following pattern
Use Dexecutor.addDependency method
看来你不想将它们分成任务,只需这样做:
It seems that you do not want to devide them into tasks, just do like this: