You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
+47-15Lines changed: 47 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2104,9 +2104,13 @@ The preceding example overrides the default factory, and it should be applied to
2104
2104
2105
2105
[[howto-batch-applications]]
2106
2106
== Batch Applications
2107
-
This section answers questions that arise from using Spring Batch with Spring Boot.
2107
+
A number of questions often arise when people use Spring Batch from within a Spring Boot application.
2108
+
This section addresses those questions.
2108
2109
2109
-
NOTE: By default, batch applications require a `DataSource` to store job details.
2110
+
[[howto-spring-batch-specifying-a-data-source]]
2111
+
=== Specifying a Batch Data Source
2112
+
2113
+
By default, batch applications require a `DataSource` to store job details.
2110
2114
Batch autowires a single `DataSource` in your context and uses that for processing.
2111
2115
To have Batch use a `DataSource` other than the application’s main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@BatchDataSource`.
2112
2116
If you do so and want two data sources, remember to create another one and mark it as `@Primary`.
@@ -2115,25 +2119,53 @@ See {spring-batch-api}/core/configuration/annotation/EnableBatchProcessing.html[
2115
2119
2116
2120
For more about Spring Batch, see the {spring-batch}[Spring Batch project page].
2117
2121
2118
-
2119
-
2120
-
[[howto-execute-spring-batch-jobs-on-startup]]
2121
-
=== Execute Spring Batch Jobs on Startup
2122
+
[[howto-running-spring-batch-jobs-on-startup]]
2123
+
=== Running Spring Batch Jobs on Startup
2122
2124
Spring Batch auto-configuration is enabled by adding `@EnableBatchProcessing` (from Spring Batch) somewhere in your context.
2123
2125
2124
-
By default, it executes *all* `Jobs` in the application context on startup (see {spring-boot-autoconfigure-module-code}/batch/JobLauncherCommandLineRunner.java[JobLauncherCommandLineRunner] for details).
2126
+
By default, it executes *all* `Jobs` in the application context on startup (see {spring-boot-autoconfigure-module-code}/batch/JobLauncherCommandLineRunner.java[`JobLauncherCommandLineRunner`] for details).
2125
2127
You can narrow down to a specific job or jobs by specifying `spring.batch.job.names` (which takes a comma-separated list of job name patterns).
2126
2128
2127
-
[TIP]
2128
-
.Specifying job parameters on the command line
2129
-
====
2130
-
Unlike command line option arguments that <<spring-boot-features.adoc#boot-features-external-config-command-line-args,set properties in the `Environment`>> (i.e. by starting with `--`, such as `--my-property=value`), job parameters have to be specified on the command line without dashes (e.g. `jobParam=value`).
2131
-
====
2129
+
See {spring-boot-autoconfigure-module-code}/batch/BatchAutoConfiguration.java[BatchAutoConfiguration] and https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java[@EnableBatchProcessing] for more details.
2132
2130
2133
-
If the application context includes a `JobRegistry`, the jobs in `spring.batch.job.names` are looked up in the registry instead of being autowired from the context.
2134
-
This is a common pattern with more complex systems, where multiple jobs are defined in child contexts and registered centrally.
2131
+
[[howto-spring-batch-running-command-line]]
2132
+
=== Running from the Command Line
2135
2133
2136
-
See {spring-boot-autoconfigure-module-code}/batch/BatchAutoConfiguration.java[BatchAutoConfiguration] and https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/EnableBatchProcessing.java[@EnableBatchProcessing] for more details.
2134
+
Running Spring Batch with Spring Boot from the command line differs from running Spring Batch by itself from the command line.
2135
+
Spring Boot uses a class called `JobLaunchingCommandLineRunner`.
2136
+
Spring Batch uses a class called `CommandLineJobRunner`.
2137
+
They work a bit differently. However, for the most part, the differences do not cause trouble.
2138
+
However, they parse command line arguments differently, which can cause trouble, as described in the next section.
2139
+
2140
+
==== Passing Command-line Arguments
2141
+
2142
+
Spring Boot uses `--` (two hyphens) to signal application arguments.
2143
+
Spring Batch uses a single hyphen as a special marker on the `jobParameters` argument.
2144
+
This section explains how to reconcile that difference when you use the `jobParameters` argument for Spring Batch within a Spring Boot application.
2145
+
2146
+
If you run Spring Batch with Spring Boot, Spring Boot strips the first `-` character from each command line argument.
2147
+
For example, `--exampleArgument` becomes `-exampleArgument`.
2148
+
Whether a command-line option has one hyphen or two often makes no difference in Spring Boot.
2149
+
However, in Spring Batch, putting a single `-` character before the `jobParameters` parameter indicates that Spring Batch should not use the `jobParameters` value as the identifier for the `Job`.
2150
+
Best practice is to use the `jobParameters` value as the identifier for the `Job`, so this issue may cause problems.
2151
+
To avoid the issue, you should generally use no `-` characters for the command-line options that you pass to Spring Boot on behalf of Spring Batch, as shown in the following example:
2152
+
2153
+
[source]
2154
+
someParameter="someValue"
2155
+
2156
+
However, if you mean to not use the `someValue` value as the identifier for the `Job`, use two hyphens, as shown in the following example:
2157
+
2158
+
[source]
2159
+
--jobParameters="someValue"
2160
+
2161
+
In the second example, Spring Boot passes the parameter to Spring Batch as `-jobParameters="someValue"`, and `someValue` is used as a non-identifying job parameter.
2162
+
2163
+
[[howto-spring-batch-storing-job-repository]]
2164
+
=== Storing the Job Repository
2165
+
2166
+
Spring Batch requires a memory store for the `Job` repository.
2167
+
If you use Spring Boot, you must use an actual database. Note that it can be an in-memory database.
2168
+
See https://docs.spring.io/spring-batch/trunk/reference/html/configureJob.html#configuringJobRepository[Configuring a Job Repository].
0 commit comments