简体   繁体   English

了解在Web容器中启动Spring批处理作业

[英]Understanding launching of Spring batch Jobs in a web container

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. 我正在阅读Spring Batch文档,当我遇到一个事实,即如果我们有效地必须从Web容器运行批处理作业,我们将不得不使用TaskExecutor接口(异步版本)的不同实现。

I am assuming that an Http request would trigger the batch job. 我假设一个Http请求会触发批处理作业。 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. 而且据我了解,当客户端通过JobLauncher接口的run方法启动作业时,客户端必须等待JobExecution对象被返回,并因为一个典型的批处理作业将在结束运行几个小时,这可能如果同步执行作业,则不太可行。 Now, the AsyncTaskExecutor would execute each step in separate threads and would return the JobExecution object immediately with an UNKNOWN status. 现在, AsyncTaskExecutor将在单独的线程中执行每个步骤,并立即返回具有UNKNOWN状态的JobExecution对象。

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. 例如,假设客户端有一个发送HTTP get请求的Web页面,该请求由servlet的doget方法提供。 This method calls the run method of the job launcher. 此方法调用作业启动程序的run方法。 This method will return the JobExecution object. 此方法将返回JobExecution对象。 And the rest of the story is as above. 其余的故事如上所述。

Thanks, Aditya. 谢谢,Aditya。

It depends a bit on what your servlet does after it has called the run method and received the JobExecution object in return. 它取决于你的servlet在调用run方法并接收JobExecution对象之后所做的事情。 I will assume that the doget method just returns after run is called. 我将假设doget方法只是在调用run之后返回。

If you do not use an asynchronous executor the call to the run method on the job launcher will be executed synchronously. 如果使用异步执行程序,则将同步执行对作业启动程序上的run方法的调用。 That is, the call will wait until the batch job is done and the JobExecution object is returned. 也就是说,调用将等到批处理作业完成并返回JobExecution对象。 From a connection perspective, the client's HTTP connection will remain open during the entire batch job. 从连接的角度来看,客户端的HTTP连接将在整个批处理作业期间保持打开状态。 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, eg firewall or a socket read timeout). 当servlet的doGet方法返回时(或者如果在某个级别遇到某种超时,例如防火墙或套接字读取超时),将关闭HTTP连接。

If you do use a asynchronous executor the call to the run method will return immediately. 如果使用异步执行程序,则会立即返回对run方法的调用。 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). 之后,doGet方法将返回,HTTP响应将被发送到客户端,连接将被关闭(假设没有HTTP keep-alive)。

Running Jobs from within a Web Container 从Web容器中运行作业

Usually jobs are launched from the command-line. 通常,作业是从命令行启动的。 However, there are many cases where launching from an HttpRequest is a better option. 但是,在很多情况下,从HttpRequest启动是一个更好的选择。 Many such use cases include reporting, ad-hoc job running, and web application support. 许多此类用例包括报告,临时作业运行和Web应用程序支持。 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 a JobExecution . 这里,Spring MVC控制器使用已配置为异步启动的JobLauncher启动Job,该JobLauncher会立即返回JobExecution The Job will likely still be running, however, this nonblocking behaviour allows the controller to return immediately, which is required when handling an HttpRequest . Job可能仍在运行,但是,这种非阻塞行为允许控制器立即返回,这在处理HttpRequest时是必需的。

An example is below: 一个例子如下:

@Controller
public class JobLauncherController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @RequestMapping("/jobLauncher.html")
    public void handle() throws Exception{
        jobLauncher.run(job, new JobParameters());
    }
}

source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM