Skip to content

Commit

Permalink
Add hello world sample
Browse files Browse the repository at this point in the history
Issue #4329
  • Loading branch information
fmbenhassine committed Oct 17, 2023
1 parent db7fa32 commit 9f56a50
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spring-batch-samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | | |
Expand Down Expand Up @@ -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
Expand Down
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();
}

}
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
```
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());
}

}

0 comments on commit 9f56a50

Please sign in to comment.