了解在 Web 容器中启动 Spring 批处理作业
我在阅读 Spring Batch 文档时发现,如果我们必须有效地从 Web 运行批处理作业,我们就必须使用 TaskExecutor 接口的不同实现(异步版本)容器。
我假设 Http 请求会触发批处理作业。据我了解,当客户端通过 JobLauncher 接口的 run 方法启动作业时,客户端必须等待返回 JobExecution 对象,因为典型的批处理作业最终会运行几个小时,如果作业同步执行,这可能不太可行。现在,AsyncTaskExecutor 将在单独的线程中执行每个步骤,并立即返回具有 UNKNOWN 状态的 JobExecution 对象。
首先,有人可以向我解释一下,从客户端-服务器连接的角度来看这是如何工作的吗?在每种情况下,客户端在终止会话之前是否不会等待批处理完成?或者,客户端是否不知道批处理作业的退出状态?整个问题是否与必须保持连接直到批处理结束有关?
举个例子,假设客户端有一个发送 HTTP get 请求的网页,该请求由 servlet 的 doget 方法提供服务。此方法调用作业启动器的run方法。此方法将返回JobExecution对象。故事的其余部分如上所述。
谢谢, 阿迪亚。
I was reading the Spring Batch documentation when I came across the fact that we would have to use a different implementation of the TaskExecutor interface (The Asynchronous version) if we would efficiently have to run batch jobs from a web container.
I am assuming that an Http request would trigger the batch job. And as I understand it, when the client launches the job via the run method of the JobLauncher interface, the client has to wait for the JobExecution object to be returned back and since a typical batch job would run for hours at an end, this might not be very feasible if the jobs are executed synchronously. Now, the AsyncTaskExecutor would execute each step in separate threads and would return the JobExecution object immediately with an UNKNOWN status.
Firstly, can someone please explain to me, how this works from a client-server connection perspective? In each case, would the client not wait for the batch to be finished before he terminates the session? Or, would the client not know about the exit status of the batch job? Is the whole problem to do with the connection having to remain till the batch ends?
As an example, say the client has a web page which sends an HTTP get request, which is served by a servlet's doget method. This method calls the run method of the job launcher. This method will return the JobExecution object. And the rest of the story is as above.
Thanks,
Aditya.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在一定程度上取决于 servlet 在调用 run 方法并收到 JobExecution 对象返回后执行的操作。我假设 doget 方法在调用 run 后返回。
如果您不使用异步执行器,则对作业启动器上的 run 方法的调用将同步执行。也就是说,调用将等待批处理作业完成并返回 JobExecution 对象。从连接角度来看,客户端的 HTTP 连接在整个批处理作业期间将保持打开状态。当 servlet 的 doGet 方法返回时(或者如果在某个级别遇到某种超时,例如防火墙或套接字读取超时),HTTP 连接将被关闭。
如果您确实使用异步执行器,则对 run 方法的调用将立即返回。之后 doGet 方法将返回,HTTP 响应将被发送到客户端,并且连接将被关闭(假设没有 HTTP keep-alive)。
It depends a bit on what your servlet does after it has called the run method and received the JobExecution object in return. I will assume that the doget method just returns after run is called.
If you do not use an asynchronous executor the call to the run method on the job launcher will be executed synchronously. That is, the call will wait until the batch job is done and the JobExecution object is returned. From a connection perspective, the client's HTTP connection will remain open during the entire batch job. The HTTP connection will be closed when the servlet's doGet method returns (or before if some kind of timeout is encountered on some level, e.g. firewall or a socket read timeout).
If you do use a asynchronous executor the call to the run method will return immediately. The doGet method will return after that, the HTTP response will be sent to the client and the connection will be closed (assuming there is not HTTP keep-alive).
从 Web 容器内运行作业
通常作业是从命令行启动的。然而,在很多情况下,从
HttpRequest
启动是更好的选择。许多此类用例包括报告、临时作业运行和 Web 应用程序支持。由于批处理作业根据定义需要长时间运行,因此最重要的问题是确保异步启动作业:这里 Spring MVC 控制器使用已在其中的
JobLauncher
启动一个作业配置好的异步启动,立即返回JobExecution
。该作业可能仍在运行,但是,这种非阻塞行为允许控制器立即返回,这是处理HttpRequest
时所必需的。示例如下:
源
Running Jobs from within a Web Container
Usually jobs are launched from the command-line. However, there are many cases where launching from an
HttpRequest
is a better option. Many such use cases include reporting, ad-hoc job running, and web application support. Because a batch job by definition is long running, the most important concern is ensuring to launch the job asynchronously:Here Spring MVC controller launches a Job using a
JobLauncher
that has been configured to launch asynchronously, which immediately returns aJobExecution
. The Job will likely still be running, however, this nonblocking behaviour allows the controller to return immediately, which is required when handling anHttpRequest
.An example is below:
source