Refactor samples to use a common datasource configuration

Issue #4329
This commit is contained in:
Mahmoud Ben Hassine
2023-10-17 15:30:04 +02:00
parent 746ffab09d
commit db7fa32753
34 changed files with 111 additions and 512 deletions

View File

@@ -16,8 +16,6 @@
package org.springframework.batch.samples.amqp;
import javax.sql.DataSource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
@@ -29,10 +27,10 @@ import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.amqp.builder.AmqpItemReaderBuilder;
import org.springframework.batch.item.amqp.builder.AmqpItemWriterBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -43,6 +41,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class AmqpJobConfiguration {
@Bean
@@ -60,20 +59,6 @@ public class AmqpJobConfiguration {
.build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.addScript("/business-schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
/**
* Reads from the designated queue.
* @param rabbitInputTemplate the template to be used by the {@link ItemReader}.

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.integration.chunk.RemoteChunkingManagerStepBuilderFactory;
import org.springframework.batch.integration.config.annotation.EnableBatchIntegration;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

View File

@@ -23,6 +23,7 @@ import org.springframework.batch.integration.chunk.RemoteChunkingWorkerBuilder;
import org.springframework.batch.integration.config.annotation.EnableBatchIntegration;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 the original author or authors.
* Copyright 2021-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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.batch.samples.chunking;
package org.springframework.batch.samples.common;
import javax.sql.DataSource;
@@ -33,6 +33,7 @@ public class DataSourceConfiguration {
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.addScript("/org/springframework/batch/samples/common/business-schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}

View File

@@ -1,7 +1,5 @@
package org.springframework.batch.samples.file.delimited;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -14,19 +12,20 @@ import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class DelimitedJobConfiguration {
@Bean
@@ -63,20 +62,4 @@ public class DelimitedJobConfiguration {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -1,7 +1,5 @@
package org.springframework.batch.samples.file.fixed;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -15,19 +13,20 @@ import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.file.transform.Range;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class FixedLengthJobConfiguration {
@Bean
@@ -66,20 +65,4 @@ public class FixedLengthJobConfiguration {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.batch.samples.file.json;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -30,13 +28,14 @@ import org.springframework.batch.item.json.JsonFileItemWriter;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.Trade;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -44,6 +43,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JsonJobConfiguration {
@Bean
@@ -80,19 +80,4 @@ public class JsonJobConfiguration {
return new JobBuilder("job", jobRepository).start(step).build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -1,7 +1,5 @@
package org.springframework.batch.samples.file.multiline;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -16,17 +14,18 @@ import org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class MultiLineJobConfiguration {
@Bean
@@ -65,20 +64,4 @@ public class MultiLineJobConfiguration {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -17,8 +17,6 @@ package org.springframework.batch.samples.file.multirecordtype;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -34,6 +32,7 @@ import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.FixedLengthTokenizer;
import org.springframework.batch.item.file.transform.FormatterLineAggregator;
import org.springframework.batch.item.file.transform.Range;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.Trade;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditFieldSetMapper;
@@ -41,10 +40,9 @@ import org.springframework.batch.samples.domain.trade.internal.TradeFieldSetMapp
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -52,6 +50,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class MultiRecordTypeJobConfiguration {
@Bean
@@ -137,20 +136,4 @@ public class MultiRecordTypeJobConfiguration {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.batch.samples.file.multiresource;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
@@ -33,15 +31,15 @@ import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder;
import org.springframework.batch.item.file.builder.MultiResourceItemWriterBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -49,6 +47,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class MultiResourceJobConfiguration {
@Bean
@@ -101,20 +100,4 @@ public class MultiResourceJobConfiguration {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -3,8 +3,6 @@ package org.springframework.batch.samples.file.xml;
import java.math.BigDecimal;
import java.util.Map;
import javax.sql.DataSource;
import com.thoughtworks.xstream.security.ExplicitTypePermission;
import org.springframework.batch.core.Job;
@@ -19,20 +17,21 @@ import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.batch.item.xml.builder.StaxEventItemReaderBuilder;
import org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.oxm.xstream.XStreamMarshaller;
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class XmlJobConfiguration {
@Bean
@@ -78,20 +77,4 @@ public class XmlJobConfiguration {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -12,6 +12,7 @@ import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.football.internal.GameFieldSetMapper;
import org.springframework.batch.samples.football.internal.JdbcGameDao;
import org.springframework.batch.samples.football.internal.JdbcPlayerDao;
@@ -21,13 +22,13 @@ import org.springframework.batch.samples.football.internal.PlayerItemWriter;
import org.springframework.batch.samples.football.internal.PlayerSummaryMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class FootballJobConfiguration {
// step 1 configuration
@@ -43,19 +44,20 @@ public class FootballJobConfiguration {
}
@Bean
public PlayerItemWriter playerWriter() {
public PlayerItemWriter playerWriter(DataSource dataSource) {
PlayerItemWriter playerItemWriter = new PlayerItemWriter();
JdbcPlayerDao playerDao = new JdbcPlayerDao();
playerDao.setDataSource(dataSource());
playerDao.setDataSource(dataSource);
playerItemWriter.setPlayerDao(playerDao);
return playerItemWriter;
}
@Bean
public Step playerLoad(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
public Step playerLoad(JobRepository jobRepository, JdbcTransactionManager transactionManager,
FlatFileItemReader<Player> playerFileItemReader, PlayerItemWriter playerWriter) {
return new StepBuilder("playerLoad", jobRepository).<Player, Player>chunk(2, transactionManager)
.reader(playerFileItemReader())
.writer(playerWriter())
.reader(playerFileItemReader)
.writer(playerWriter)
.build();
}
@@ -73,24 +75,25 @@ public class FootballJobConfiguration {
}
@Bean
public JdbcGameDao gameWriter() {
public JdbcGameDao gameWriter(DataSource dataSource) {
JdbcGameDao jdbcGameDao = new JdbcGameDao();
jdbcGameDao.setDataSource(dataSource());
jdbcGameDao.setDataSource(dataSource);
return jdbcGameDao;
}
@Bean
public Step gameLoad(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
public Step gameLoad(JobRepository jobRepository, JdbcTransactionManager transactionManager,
FlatFileItemReader<Game> gameFileItemReader, JdbcGameDao gameWriter) {
return new StepBuilder("gameLoad", jobRepository).<Game, Game>chunk(2, transactionManager)
.reader(gameFileItemReader())
.writer(gameWriter())
.reader(gameFileItemReader)
.writer(gameWriter)
.build();
}
// step 3 configuration
@Bean
public JdbcCursorItemReader<PlayerSummary> playerSummarizationSource() {
public JdbcCursorItemReader<PlayerSummary> playerSummarizationSource(DataSource dataSource) {
String sql = """
SELECT GAMES.player_id, GAMES.year_no, SUM(COMPLETES),
SUM(ATTEMPTS), SUM(PASSING_YARDS), SUM(PASSING_TD),
@@ -102,24 +105,25 @@ public class FootballJobConfiguration {
return new JdbcCursorItemReaderBuilder<PlayerSummary>().name("playerSummarizationSource")
.ignoreWarnings(true)
.sql(sql)
.dataSource(dataSource())
.dataSource(dataSource)
.rowMapper(new PlayerSummaryMapper())
.build();
}
@Bean
public JdbcPlayerSummaryDao summaryWriter() {
public JdbcPlayerSummaryDao summaryWriter(DataSource dataSource) {
JdbcPlayerSummaryDao jdbcPlayerSummaryDao = new JdbcPlayerSummaryDao();
jdbcPlayerSummaryDao.setDataSource(dataSource());
jdbcPlayerSummaryDao.setDataSource(dataSource);
return jdbcPlayerSummaryDao;
}
@Bean
public Step summarizationStep(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
public Step summarizationStep(JobRepository jobRepository, JdbcTransactionManager transactionManager,
JdbcCursorItemReader<PlayerSummary> playerSummarizationSource, JdbcPlayerSummaryDao summaryWriter) {
return new StepBuilder("summarizationStep", jobRepository)
.<PlayerSummary, PlayerSummary>chunk(2, transactionManager)
.reader(playerSummarizationSource())
.writer(summaryWriter())
.reader(playerSummarizationSource)
.writer(summaryWriter)
.build();
}
@@ -133,18 +137,4 @@ public class FootballJobConfiguration {
.build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.addScript("/org/springframework/batch/samples/football/sql/schema.sql")
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -26,12 +26,12 @@ import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -39,12 +39,13 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JdbcReaderBatchWriterSampleJob {
@Bean
public JdbcBatchItemWriter<CustomerCredit> itemWriter() {
public JdbcBatchItemWriter<CustomerCredit> itemWriter(DataSource dataSource) {
String sql = "UPDATE CUSTOMER set credit = :credit where id = :id";
return new JdbcBatchItemWriterBuilder<CustomerCredit>().dataSource(dataSource())
return new JdbcBatchItemWriterBuilder<CustomerCredit>().dataSource(dataSource)
.sql(sql)
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
.assertUpdates(true)
@@ -63,20 +64,4 @@ public class JdbcReaderBatchWriterSampleJob {
.build();
}
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.addScript("/org/springframework/batch/samples/jdbc/sql/schema.sql")
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -27,12 +27,12 @@ import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder;
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
@@ -46,7 +46,8 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
* @author Mahmoud Ben Hassine
*/
@Configuration
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT")
@Import(DataSourceConfiguration.class)
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT", transactionManagerRef = "jpaTransactionManager")
public class JpaJobConfiguration {
@Bean
@@ -63,10 +64,11 @@ public class JpaJobConfiguration {
}
@Bean
public Job job(JobRepository jobRepository, JpaTransactionManager transactionManager,
public Job job(JobRepository jobRepository, JpaTransactionManager jpaTransactionManager,
JpaPagingItemReader<CustomerCredit> itemReader, JpaItemWriter<CustomerCredit> itemWriter) {
return new JobBuilder("ioSampleJob", jobRepository)
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
.start(new StepBuilder("step1", jobRepository)
.<CustomerCredit, CustomerCredit>chunk(2, jpaTransactionManager)
.reader(itemReader)
.processor(new CustomerCreditIncreaseProcessor())
.writer(itemWriter)
@@ -77,16 +79,7 @@ public class JpaJobConfiguration {
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.addScript("/org/springframework/batch/samples/jpa/sql/schema.sql")
.build();
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
public JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

View File

@@ -31,15 +31,15 @@ import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder;
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.CustomerCredit;
import org.springframework.batch.samples.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
@@ -53,7 +53,8 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
* @author Mahmoud Ben Hassine
*/
@Configuration
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT")
@Import(DataSourceConfiguration.class)
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_DEFAULT", transactionManagerRef = "jpaTransactionManager")
@EnableJpaRepositories(basePackages = "org.springframework.batch.samples.jpa")
public class JpaRepositoryJobConfiguration {
@@ -76,10 +77,11 @@ public class JpaRepositoryJobConfiguration {
}
@Bean
public Job job(JobRepository jobRepository, JpaTransactionManager transactionManager,
public Job job(JobRepository jobRepository, JpaTransactionManager jpaTransactionManager,
RepositoryItemReader<CustomerCredit> itemReader, RepositoryItemWriter<CustomerCredit> itemWriter) {
return new JobBuilder("ioSampleJob", jobRepository)
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
.start(new StepBuilder("step1", jobRepository)
.<CustomerCredit, CustomerCredit>chunk(2, jpaTransactionManager)
.reader(itemReader)
.processor(new CustomerCreditIncreaseProcessor())
.writer(itemWriter)
@@ -90,16 +92,7 @@ public class JpaRepositoryJobConfiguration {
// Infrastructure beans
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.addScript("/org/springframework/batch/samples/jpa/sql/schema.sql")
.build();
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
public JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

View File

@@ -18,8 +18,6 @@ package org.springframework.batch.samples.loom;
import java.util.Arrays;
import java.util.concurrent.Future;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -30,10 +28,11 @@ import org.springframework.batch.integration.async.AsyncItemProcessor;
import org.springframework.batch.integration.async.AsyncItemWriter;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.support.ListItemReader;
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.core.task.VirtualThreadTaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -44,6 +43,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JobConfigurationForAsynchronousItemProcessingWithVirtualThreads {
@Bean
@@ -85,16 +85,4 @@ public class JobConfigurationForAsynchronousItemProcessingWithVirtualThreads {
return new JobBuilder("job", jobRepository).start(step).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.batch.samples.loom;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -25,11 +23,12 @@ import org.springframework.batch.core.launch.JobLauncher;
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.core.task.TaskExecutor;
import org.springframework.core.task.VirtualThreadTaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -40,6 +39,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JobConfigurationForLaunchingJobsWithVirtualThreads {
@Bean
@@ -52,18 +52,6 @@ public class JobConfigurationForLaunchingJobsWithVirtualThreads {
return new JobBuilder("job", jobRepository).start(step).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
@Bean
public TaskExecutor taskExecutor() {
return new VirtualThreadTaskExecutor("spring-batch-");

View File

@@ -19,8 +19,6 @@ import java.util.Arrays;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -30,10 +28,11 @@ import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
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.core.task.VirtualThreadTaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -44,6 +43,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JobConfigurationForRunningConcurrentStepsWithVirtualThreads {
@Bean
@@ -86,16 +86,4 @@ public class JobConfigurationForRunningConcurrentStepsWithVirtualThreads {
return new JobBuilder("job", jobRepository).start(step).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -15,8 +15,6 @@
*/
package org.springframework.batch.samples.loom;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -26,10 +24,11 @@ import org.springframework.batch.core.job.flow.Flow;
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.core.task.VirtualThreadTaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -40,6 +39,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JobConfigurationForRunningParallelStepsWithVirtualThreads {
@Bean
@@ -64,18 +64,6 @@ public class JobConfigurationForRunningParallelStepsWithVirtualThreads {
return new JobBuilder("job", jobRepository).start(splitFlow).build().build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
private Step createStep(String stepName, JobRepository jobRepository, JdbcTransactionManager transactionManager) {
return new StepBuilder(stepName, jobRepository).tasklet((contribution, chunkContext) -> {
System.out.println(Thread.currentThread() + ": running " + stepName);

View File

@@ -18,8 +18,6 @@ package org.springframework.batch.samples.loom;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -31,11 +29,12 @@ import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.task.VirtualThreadTaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -46,6 +45,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JobConfigurationForRunningPartitionedStepsWithVirtualThreads {
@Bean
@@ -90,16 +90,4 @@ public class JobConfigurationForRunningPartitionedStepsWithVirtualThreads {
return new JobBuilder("job", jobRepository).start(managerStep).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -19,8 +19,6 @@ import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -29,10 +27,11 @@ import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.JvmCommandRunner;
import org.springframework.batch.core.step.tasklet.SystemCommandTasklet;
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.core.task.VirtualThreadTaskExecutor;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -43,6 +42,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class JobConfigurationForRunningSystemCommandTaskletsWithVirtualThreads {
@Bean
@@ -68,16 +68,4 @@ public class JobConfigurationForRunningSystemCommandTaskletsWithVirtualThreads {
return new JobBuilder("job", jobRepository).start(step).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -15,23 +15,20 @@
*/
package org.springframework.batch.samples.metrics;
import javax.sql.DataSource;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@EnableScheduling
@EnableBatchProcessing
@Import({ Job1Configuration.class, Job2Configuration.class, JobScheduler.class, PrometheusConfiguration.class })
@Import({ Job1Configuration.class, Job2Configuration.class, JobScheduler.class, PrometheusConfiguration.class,
DataSourceConfiguration.class })
@PropertySource("metrics-sample.properties")
public class BatchMetricsApplication {
@@ -48,16 +45,4 @@ public class BatchMetricsApplication {
return threadPoolTaskScheduler;
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -15,21 +15,19 @@
*/
package org.springframework.batch.samples.retry;
import javax.sql.DataSource;
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.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.domain.trade.Trade;
import org.springframework.batch.samples.domain.trade.internal.GeneratingTradeItemReader;
import org.springframework.batch.samples.support.RetrySampleItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -39,6 +37,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class RetrySampleConfiguration {
@Bean
@@ -69,17 +68,4 @@ public class RetrySampleConfiguration {
return new RetrySampleItemWriter<>();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -1,41 +0,0 @@
/*
* Copyright 2021-2022 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
*
* http://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.skip;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
@Configuration
public class DataSourceConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
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;

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
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;

View File

@@ -28,6 +28,7 @@ import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
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;

View File

@@ -18,8 +18,6 @@ package org.springframework.batch.samples.validation;
import java.util.Arrays;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
@@ -29,10 +27,11 @@ import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.item.support.ListItemWriter;
import org.springframework.batch.item.validator.BeanValidatingItemProcessor;
import org.springframework.batch.samples.common.DataSourceConfiguration;
import org.springframework.batch.samples.validation.domain.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.support.JdbcTransactionManager;
/**
@@ -40,6 +39,7 @@ import org.springframework.jdbc.support.JdbcTransactionManager;
*/
@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class ValidationSampleConfiguration {
@Bean
@@ -64,8 +64,8 @@ public class ValidationSampleConfiguration {
}
@Bean
public Step step(JobRepository jobRepository) throws Exception {
return new StepBuilder("step", jobRepository).<Person, Person>chunk(1, transactionManager(dataSource()))
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) throws Exception {
return new StepBuilder("step", jobRepository).<Person, Person>chunk(1, transactionManager)
.reader(itemReader())
.processor(itemValidator())
.writer(itemWriter())
@@ -73,21 +73,8 @@ public class ValidationSampleConfiguration {
}
@Bean
public Job job(JobRepository jobRepository) throws Exception {
return new JobBuilder("job", jobRepository).start(step(jobRepository)).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true)
.build();
}
@Bean
public JdbcTransactionManager transactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
public Job job(JobRepository jobRepository, Step step) throws Exception {
return new JobBuilder("job", jobRepository).start(step).build();
}
}

View File

@@ -1,45 +0,0 @@
DROP TABLE PLAYERS IF EXISTS;
DROP TABLE GAMES IF EXISTS;
DROP TABLE PLAYER_SUMMARY IF EXISTS;
CREATE TABLE PLAYERS (
PLAYER_ID CHAR(8) NOT NULL PRIMARY KEY,
LAST_NAME VARCHAR(35) NOT NULL,
FIRST_NAME VARCHAR(25) NOT NULL,
POS VARCHAR(10) ,
YEAR_OF_BIRTH BIGINT NOT NULL,
YEAR_DRAFTED BIGINT NOT NULL
) ;
CREATE TABLE GAMES (
PLAYER_ID CHAR(8) NOT NULL,
YEAR_NO BIGINT NOT NULL,
TEAM CHAR(3) NOT NULL,
WEEK BIGINT NOT NULL,
OPPONENT CHAR(3) ,
COMPLETES BIGINT ,
ATTEMPTS BIGINT ,
PASSING_YARDS BIGINT ,
PASSING_TD BIGINT ,
INTERCEPTIONS BIGINT ,
RUSHES BIGINT ,
RUSH_YARDS BIGINT ,
RECEPTIONS BIGINT ,
RECEPTIONS_YARDS BIGINT ,
TOTAL_TD BIGINT
) ;
CREATE TABLE PLAYER_SUMMARY (
ID CHAR(8) NOT NULL,
YEAR_NO BIGINT NOT NULL,
COMPLETES BIGINT NOT NULL ,
ATTEMPTS BIGINT NOT NULL ,
PASSING_YARDS BIGINT NOT NULL ,
PASSING_TD BIGINT NOT NULL ,
INTERCEPTIONS BIGINT NOT NULL ,
RUSHES BIGINT NOT NULL ,
RUSH_YARDS BIGINT NOT NULL ,
RECEPTIONS BIGINT NOT NULL ,
RECEPTIONS_YARDS BIGINT NOT NULL ,
TOTAL_TD BIGINT NOT NULL
) ;

View File

@@ -1,19 +0,0 @@
DROP TABLE CUSTOMER_SEQ IF EXISTS;
DROP TABLE CUSTOMER IF EXISTS;
CREATE TABLE CUSTOMER_SEQ (
ID BIGINT IDENTITY
);
INSERT INTO CUSTOMER_SEQ (ID) values (5);
CREATE TABLE CUSTOMER (
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
VERSION BIGINT ,
NAME VARCHAR(45) ,
CREDIT DECIMAL(10,2)
) ;
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (1, 0, 'customer1', 100000);
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (2, 0, 'customer2', 100000);
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (3, 0, 'customer3', 100000);
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (4, 0, 'customer4', 100000);

View File

@@ -63,6 +63,6 @@
<jdbc:embedded-database id="dataSource" generate-name="true">
<jdbc:script location="org/springframework/batch/core/schema-drop-hsqldb.sql"/>
<jdbc:script location="org/springframework/batch/core/schema-hsqldb.sql"/>
<jdbc:script location="org/springframework/batch/samples/jpa/sql/schema.sql"/>
<jdbc:script location="org/springframework/batch/samples/common/business-schema-hsqldb.sql"/>
</jdbc:embedded-database>
</beans>

View File

@@ -83,7 +83,7 @@
<jdbc:embedded-database id="dataSource" generate-name="true">
<jdbc:script location="org/springframework/batch/core/schema-drop-hsqldb.sql"/>
<jdbc:script location="org/springframework/batch/core/schema-hsqldb.sql"/>
<jdbc:script location="org/springframework/batch/samples/jpa/sql/schema.sql"/>
<jdbc:script location="org/springframework/batch/samples/common/business-schema-hsqldb.sql"/>
</jdbc:embedded-database>
</beans>

View File

@@ -1,19 +0,0 @@
DROP TABLE CUSTOMER_SEQ IF EXISTS;
DROP TABLE CUSTOMER IF EXISTS;
CREATE TABLE CUSTOMER_SEQ (
ID BIGINT IDENTITY
);
INSERT INTO CUSTOMER_SEQ (ID) values (5);
CREATE TABLE CUSTOMER (
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
VERSION BIGINT ,
NAME VARCHAR(45) ,
CREDIT DECIMAL(10,2)
) ;
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (1, 0, 'customer1', 100000);
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (2, 0, 'customer2', 100000);
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (3, 0, 'customer3', 100000);
INSERT INTO CUSTOMER (ID, VERSION, NAME, CREDIT) VALUES (4, 0, 'customer4', 100000);

View File

@@ -73,6 +73,8 @@ public abstract class RemotePartitioningJobFunctionalTests {
.setJMXManagementEnabled(false)
.setJournalDatasync(false);
this.brokerService = new EmbeddedActiveMQ().setConfiguration(configuration).start();
// FIXME Does not work when importing
// org.springframework.batch.samples.common.DataSourceConfiguration?
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
databasePopulator.addScript(new ClassPathResource("/org/springframework/batch/core/schema-drop-hsqldb.sql"));
databasePopulator.addScript(new ClassPathResource("/org/springframework/batch/core/schema-hsqldb.sql"));