-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...rc/main/java/org/springframework/batch/samples/helloworld/HelloWorldJobConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.batch.samples.helloworld; | ||
|
||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.batch.samples.common.DataSourceConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.jdbc.support.JdbcTransactionManager; | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
@Import(DataSourceConfiguration.class) | ||
public class HelloWorldJobConfiguration { | ||
|
||
@Bean | ||
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) { | ||
return new StepBuilder("step", jobRepository).tasklet((contribution, chunkContext) -> { | ||
System.out.println("Hello world!"); | ||
return RepeatStatus.FINISHED; | ||
}, transactionManager).build(); | ||
} | ||
|
||
@Bean | ||
public Job job(JobRepository jobRepository, Step step) { | ||
return new JobBuilder("job", jobRepository).start(step).build(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...ch-samples/src/main/java/org/springframework/batch/samples/helloworld/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
### Hello world sample | ||
|
||
## About | ||
|
||
This sample is a single-step job that prints "Hello world!" to the standard | ||
output. It shows the basic setup to configure and use Spring Batch. | ||
|
||
## Run the sample | ||
|
||
You can run the sample from the command line as following: | ||
|
||
``` | ||
$>cd spring-batch-samples | ||
$>../mvnw -Dtest=HelloWorldJobFunctionalTests#testLaunchJob test | ||
``` |
47 changes: 47 additions & 0 deletions
47
.../test/java/org/springframework/batch/samples/helloworld/HelloWorldJobFunctionalTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.batch.samples.helloworld; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.batch.core.BatchStatus; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobExecution; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class HelloWorldJobFunctionalTests { | ||
|
||
@Test | ||
public void testLaunchJob() throws Exception { | ||
// given | ||
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldJobConfiguration.class); | ||
JobLauncher jobLauncher = context.getBean(JobLauncher.class); | ||
Job job = context.getBean(Job.class); | ||
|
||
// when | ||
JobExecution jobExecution = jobLauncher.run(job, new JobParameters()); | ||
|
||
// then | ||
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); | ||
} | ||
|
||
} |