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
+30-23Lines changed: 30 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2107,38 +2107,40 @@ The preceding example overrides the default factory, and it should be applied to
2107
2107
A number of questions often arise when people use Spring Batch from within a Spring Boot application.
2108
2108
This section addresses those questions.
2109
2109
2110
+
2111
+
2110
2112
[[howto-spring-batch-specifying-a-data-source]]
2111
2113
=== Specifying a Batch Data Source
2112
-
2113
2114
By default, batch applications require a `DataSource` to store job details.
2114
-
Batch autowires a single `DataSource` in your context and uses that for processing.
2115
-
To have Batch use a `DataSource` other than the application’s main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@BatchDataSource`.
2116
-
If you do so and want two data sources, remember to create another one and mark it as `@Primary`.
2115
+
Spring Batch expects a single `DataSource` by default.
2116
+
To have it use a `DataSource` other than the application’s main `DataSource`, declare a `DataSource` bean, annotating its `@Bean` method with `@BatchDataSource`.
2117
+
If you do so and want two data sources, remember to mark the other one `@Primary`.
2117
2118
To take greater control, implement `BatchConfigurer`.
2118
2119
See {spring-batch-api}/core/configuration/annotation/EnableBatchProcessing.html[The Javadoc of `@EnableBatchProcessing`] for more details.
2119
2120
2120
-
For more about Spring Batch, see the {spring-batch}[Spring Batch project page].
2121
+
For more info about Spring Batch, see the {spring-batch}[Spring Batch project page].
2122
+
2123
+
2121
2124
2122
-
[[howto-running-spring-batch-jobs-on-startup]]
2125
+
[[howto-spring-batch-running-jobs-on-startup]]
2123
2126
=== Running Spring Batch Jobs on Startup
2124
-
Spring Batch auto-configuration is enabled by adding `@EnableBatchProcessing` (from Spring Batch) somewhere in your context.
2127
+
Spring Batch auto-configuration is enabled by adding `@EnableBatchProcessing` to one of your `@Configuration` classes.
2125
2128
2126
2129
By default, it executes *all* `Jobs` in the application context on startup (see {spring-boot-autoconfigure-module-code}/batch/JobLauncherCommandLineRunner.java[`JobLauncherCommandLineRunner`] for details).
2127
2130
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).
2128
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
+
See {spring-boot-autoconfigure-module-code}/batch/BatchAutoConfiguration.java[BatchAutoConfiguration] and {spring-batch-api}/core/configuration/annotation/EnableBatchProcessing.html[@EnableBatchProcessing] for more details.
2133
+
2134
+
2130
2135
2131
2136
[[howto-spring-batch-running-command-line]]
2132
2137
=== Running from the Command Line
2133
-
2134
2138
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
+
The most important difference is that they parse command line arguments differently, which can cause trouble, as described in the next section.
2140
+
2139
2141
2140
-
==== Passing Command-line Arguments
2141
2142
2143
+
==== Passing Command-line Arguments
2142
2144
Spring Boot uses `--` (two hyphens) to signal application arguments.
2143
2145
Spring Batch uses a single hyphen as a special marker on the `jobParameters` argument.
2144
2146
This section explains how to reconcile that difference when you use the `jobParameters` argument for Spring Batch within a Spring Boot application.
@@ -2150,22 +2152,27 @@ However, in Spring Batch, putting a single `-` character before the `jobParamete
2150
2152
Best practice is to use the `jobParameters` value as the identifier for the `Job`, so this issue may cause problems.
2151
2153
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
2154
2153
-
[source]
2154
-
someParameter="someValue"
2155
+
[source,properties,indent=0]
2156
+
----
2157
+
someParameter=someValue
2158
+
----
2155
2159
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:
2160
+
However, if you mean to not use `someValue` value as the identifier for the `Job`, use two hyphens, as shown in the following example:
2157
2161
2158
-
[source]
2159
-
--jobParameters="someValue"
2162
+
[source,properties,indent=0]
2163
+
----
2164
+
--jobParameters=someValue
2165
+
----
2160
2166
2161
2167
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
2168
2169
+
2170
+
2163
2171
[[howto-spring-batch-storing-job-repository]]
2164
2172
=== 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].
2173
+
Spring Batch requires a data store for the `Job` repository.
2174
+
If you use Spring Boot, you must use an actual database.
2175
+
Note that it can be an in-memory database, see {spring-batch-docs}job.html#configuringJobRepository[Configuring a Job Repository].
0 commit comments