diff --git a/spring-batch-samples/README.md b/spring-batch-samples/README.md index 1e0a4bf87e..046ca1adea 100644 --- a/spring-batch-samples/README.md +++ b/spring-batch-samples/README.md @@ -23,6 +23,7 @@ Here is a list of samples with checks to indicate which features each one demons | Job/Feature | skip | retry | restart | automatic mapping | asynch launch | validation | delegation | write behind | non-sequential | asynch process | filtering | |:--------------------------------------------------------------|:----:|:-----:|:-------:|:-----------------:|:-------------:|:----------:|:----------:|:------------:|:--------------:|:--------------:|:---------:| +| [Hello world Job Sample](#hello-world-job-sample) | | | | | | | | | | X | | | [Amqp Job Sample](#amqp-job-sample) | | | | | | | | | | X | | | [BeanWrapperMapper Sample](#beanwrappermapper-sample) | | | | X | | | | | | | | | [Composite ItemWriter Sample](#composite-itemwriter-sample) | | | | | | | X | | | | | @@ -78,6 +79,13 @@ $>../mvnw -Dtest=[JobName]FunctionalTests#test[JobName] test Please refer to the README of each sample for launching instructions. +### Hello world Job sample + +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. + +[Hello world sample](src/main/java/org/springframework/batch/samples/helloworld/README.md) + ### Jdbc Readers and Writers sample The purpose of this sample is to show to usage of the diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/HelloWorldJobConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/HelloWorldJobConfiguration.java new file mode 100644 index 0000000000..4811f6c965 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/HelloWorldJobConfiguration.java @@ -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(); + } + +} \ No newline at end of file diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/README.md b/spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/README.md new file mode 100644 index 0000000000..5083fbfbb2 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/samples/helloworld/README.md @@ -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 +``` diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/samples/helloworld/HelloWorldJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/samples/helloworld/HelloWorldJobFunctionalTests.java new file mode 100644 index 0000000000..9dfcc75f44 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/samples/helloworld/HelloWorldJobFunctionalTests.java @@ -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()); + } + +}