Skip to content

spring-projects-experimental/spring-batch-experimental

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

About this repository

This repository contains experimental features in Spring Batch. Experimental features are not intended to be used in production. They are shared here to be explored by the community and to gather feedback. Please refer to the Enabling experimental features section for more details about how to enable experimental features.

The currently available experimental features are the following:

Important note: The versioning in this repository follows the semantic versioning specification. Public APIs should not be considered as stable and may change at any time ❗

Enabling experimental features

Experimental features are not released to Maven Central, but they are available from the Spring Milestones repository:

<repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
</repositories>

You can also import the latest snapshots from the Spring Snapshots repository:

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

Experimental features are based on the latest Spring Batch 5+ release, which requires Java 17+.

To import experimental features in your project, you need to add the following dependency:

<dependency>
    <groupId>org.springframework.batch</groupId>
    <artifactId>spring-batch-experimental</artifactId>
    <version>0.3.0</version>
</dependency>

Depending on the feature you are testing, other dependencies might be required. This will be mentioned in the section describing the feature.

To build the project and install it in your local Maven repository, use the following command:

$>./mvnw clean install

MongoDB job repository

Original issue: spring-projects/spring-batch#877

This feature introduces new implementations of JobRepository and JobExplorer for MongoDB.

To test this feature, first import the spring-batch-experimental jar as described in the Enabling experimental features section.

Then, add the following dependencies as well:

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>4.2.0</version>
    </dependency>
        <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>4.11.1</version>
    </dependency>
</dependencies>

After that, you need to create the collections and sequences required by the MongoDB JobRepository implementation in your MongoDB server instance. Similar to the DDL scripts provided for relational databases, the MongoShell scripts for MongoDB are provided in schema-drop-mongodb.js and schema-mongodb.js.

Finally, you can define the MongoDB-based JobRepository and use it in your Spring Batch application as a regular JobRepository:

@Bean
public JobRepository jobRepository(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager) throws Exception {
    MongoJobRepositoryFactoryBean jobRepositoryFactoryBean = new MongoJobRepositoryFactoryBean();
    jobRepositoryFactoryBean.setMongoOperations(mongoTemplate);
    jobRepositoryFactoryBean.setTransactionManager(transactionManager);
    jobRepositoryFactoryBean.afterPropertiesSet();
    return jobRepositoryFactoryBean.getObject();
}

The implementation requires a MongoTemplate to interact with MongoDB and a MongoTransactionManager to drive Spring Batch transactions. Those can be defined as Spring beans in the application context as described in Spring Data MongoDB documentation.

You can find a complete example in the MongoDBJobRepositoryIntegrationTests file.

Composite item reader

Original issue: spring-projects/spring-batch#757

This feature introduces a composite ItemReader implementation. Similar to the CompositeItemProcessor and CompositeItemWriter, the idea is to delegate reading to a list of item readers in order. This is useful when there is a requirement to read data having the same format from different sources (files, databases, etc). Here is an example:

record Person(int id, String name) {}

@Bean
public FlatFileItemReader<Person> fileItemReader() {
	return new FlatFileItemReaderBuilder<Person>()
			.name("fileItemReader")
			.resource(new ClassPathResource("persons.csv"))
			.delimited()
			.names("id", "name")
			.targetType(Person.class)
			.build();
}

@Bean
public JdbcCursorItemReader<Person> databaseItemReader() {
	String sql = "select * from persons";
	return new JdbcCursorItemReaderBuilder<Person>()
			.name("databaseItemReader")
			.dataSource(dataSource())
			.sql(sql)
			.rowMapper(new DataClassRowMapper<>(Person.class))
			.build();
}

@Bean
public CompositeItemReader<Person> itemReader() {
	return new CompositeItemReader<>(Arrays.asList(fileItemReader(), databaseItemReader()));
}

This snippet configures a CompositeItemReader with two delegates to read the same data from a flat file and a database table.

You can find a complete example in the CompositeItemReaderIntegrationTests file.

New chunk-oriented step implementation

Original issue: spring-projects/spring-batch#3950

This is not a new feature, but rather a new implementation of the chunk-oriented processing model. The goal is to address the problems with the current implementation as explained in #3950.

The new implementation does not address fault-tolerance and concurrency features for the moment. Those will be addressed incrementally in future versions. Our main focus for now is correctness, ie simplify the code with minimal to no behavioral changes.

The new implementation is in the ChunkOrientedStep class, which can be used as follows:

@Bean
public Step chunkOrientedStep(JobRepository jobRepository, JdbcTransactionManager transactionManager,
                              ItemReader<Person> itemReader, ItemProcessor<Person, Person> itemProcessor, ItemWriter<Person> itemWriter) {
    return new ChunkOrientedStep<>("step", 2, itemReader, itemProcessor, itemWriter, jobRepository, transactionManager);
}

The first two parameters are the step name and chunk size. Other parameters are self explanatory. Once defined, this step can then be added to a Spring Batch job flow like any other step type. You can find a complete example in the ChunkOrientedStepIntegrationTests file.

Contribute

The best way to contribute to this project is by trying out the experimental features and sharing your feedback!

If you find an issue, please report it on the issue tracker. You are welcome to share your feedback on the original issue related to each feature.

License

Apache License Version 2.0.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published