java Spring Batch:如何监控当前正在运行的作业并在 jsp 页面上显示进度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13450952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:46:53  来源:igfitidea点击:

Spring Batch:How to monitor currently running jobs & show progress on jsp page

javaspring-mvcbatch-processingspring-batch

提问by Dangling Piyush

I want to know how to monitor status of my currently running batch jobs.My jobs are basically processing folder with some default steps so I want to show progress to the user step by step .I am using Taskletsand DB Job Repository.Explaining with some example code for achieving this will be more helpful.
Thank you.

我想知道如何监视当前正在运行的批处理作业的状态。我的作业基本上是处理文件夹,并带有一些默认步骤,因此我想逐步向用户显示进度。我正在使用TaskletsDB 作业存储库。解释一些实现这一点的示例代码将更有帮助。
谢谢你。

回答by Vicky

You should have a look at spring batch admin application.

您应该看看 spring 批处理管理应用程序。

Spring Batch Admin

Spring批处理管理员

To quote directly: It's a regular war file. Once it's deployed, you can browse with a web browser (e.g. localhost:8080/spring-batch-admin-sample) and check out the features for launching jobs and inspecting job executions.

直接引用:这是一个普通的War文件。部署完成后,您可以使用 Web 浏览器(例如 localhost:8080/spring-batch-admin-sample)进行浏览,并查看启动作业和检查作业执行的功能。

Surely, through some modifications, you can achieve your own requirement.

当然,通过一些修改,你可以达到你自己的要求。

回答by Adrian Shum

If you want to develop your own monitor app/webpage, you may want to look into the JobExploreror JobOperatorinterface. It provides you with methods to get the JobExecutions, and in JobExecutions, you can get StepExecutions. All these give you the status of the job and individual steps inside.

如果您想开发自己的监控应用程序/网页,您可能需要查看JobExplorerJobOperator界面。它为您提供了获取 JobExecutions 的方法,在 JobExecutions 中,您可以获取 StepExecutions。所有这些都为您提供了工作的状态和内部的各个步骤。



Edit to reply for the comment:

编辑以回复评论:

For the quoted progress information, it is not something stored in Spring Batch. However, the two interface DO help you on that. First Store the progress information in the step execution context. Then make sure your tasklet is not implemented as a synchronous task. You have to return CONTINUABLE (forgive me if I mixed up the term, it have been some time I previous used Spring Batch), so that your tasklet will be invoked by Spring batch consecutively. (This can be done in many way, either you break your work into piece and for each invocation of tasklet execute(), you work on only one piece of them and return FINISHED only when all pieces is completed, or you may spawn another thread to do the work, and your tasklet simply monitor the progress, etc).

对于引用的进度信息,它不是存储在 Spring Batch 中的。但是,这两个界面确实可以帮助您解决这个问题。首先将进度信息存储在步骤执行上下文中。然后确保您的 tasklet 没有实现为同步任务。您必须返回 CONTINUABLE(请原谅我混淆了术语,我之前使用 Spring Batch 已经有一段时间了),以便 Spring Batch 连续调用您的 tasklet。(这可以通过多种方式完成,或者你将你的工作分解成一个块,对于 tasklet execute() 的每次调用,你只处理其中的一个,只有当所有的部分都完成时才返回 FINISHED,或者你可以产生另一个线程完成工作,而您的 tasklet 只是监视进度等)。

By doing so, you will have step execution context continuously updated with your progress information, so you can use JobExplorer and JobOperator to get the Job Execution -> Step Execution -> Step's Execution Context for your progress reporting purpose.

通过这样做,您将使用进度信息不断更新步骤执行上下文,因此您可以使用 JobExplorer 和 JobOperator 来获取作业执行 -> 步骤执行 -> 步骤的执行上下文以用于您的进度报告目的。

回答by raghuram gururajan

Spring Batch ver 4.2 comes with batch monitoring and metrics capturing based on Micrometer .You can try to leverage the same. Following table list some of the metrics Spring batch metrics

Spring Batch ver 4.2 带有基于 Micrometer 的批量监控和指标捕获。您可以尝试利用相同的功能。下表列出了一些指标 Spring 批处理指标

Spring Batch also enables you to create your own metrics .You can check more details here https://docs.spring.io/spring-batch/4.2.x/reference/html/monitoring-and-metrics.html#custom-metrics

Spring Batch 还允许您创建自己的指标。您可以在此处查看更多详细信息https://docs.spring.io/spring-batch/4.2.x/reference/html/monitoring-and-metrics.html#custom-metrics