Spring Batch - where does the process run

冷暖自知 提交于 2019-12-06 08:01:22

You have a few questions here so let's go through them one at a time:

Where does the job run? Will it run in the same thread as the web request, which means the end user will have to wait for the job to finish before getting http status 200?

That depends on your configuration. If you use the defaults, then yes. The job would run in the same thread and the user would be forced to wait until the job completes in order to get the 200. This obviously isn't a good idea...

Which is why Spring Batch's SimpleJobLauncher allows you to inject a TaskExecutor. By configuring your JobLauncher to use an async TaskExecutor implementation (ThreadPoolTaskExecutor for example), the job would be executed in a different thread, allowing the controller's processing to complete.

Obviously this is all within a single JVM, which bring us to your next question.

I want all jobs to run on the Batch server, but the jobs themselves can be initiated from the Web server. Can Spring Batch do this? Do i need some kind of Queue that i can write to from the Webserver and Consume from the Batch server, where the actual job will begin?

Spring Batch contains a module called Spring Batch Integration. This module provides various capabilities including using messages to launch Spring Batch Jobs. You can use this to have a remote "batch" server that you can communicate with from the web server. The communication mechanism is Spring Integration channels so any messaging option backed by SI would be supported (JMS, AMQP, REST, etc).

Scenario 2 - Process lines in huge file, start new job for each line
This scenario makes me think you're going down the wrong path for your design. Can you post a new question that elaborates on this use case?

Additional question: Is it possible to query Jobs based on a job input parameter
Job parameters are used to identify JobInstances and are fundamental to job identification. Because of this, yes, you can identify individual job runs based on the parameters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!