@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.sample.jdbc;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
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.item.database.BeanPropertyItemSqlParameterSourceProvider;
|
||||
import org.springframework.batch.item.database.JdbcBatchItemWriter;
|
||||
import org.springframework.batch.item.database.JdbcCursorItemReader;
|
||||
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
|
||||
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper;
|
||||
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.jdbc.support.JdbcTransactionManager;
|
||||
|
||||
/**
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@Configuration
|
||||
@EnableBatchProcessing
|
||||
public class JdbcCursorReaderBatchWriterSampleJob {
|
||||
|
||||
@Bean
|
||||
public JdbcCursorItemReader<CustomerCredit> itemReader() {
|
||||
String sql = "select ID, NAME, CREDIT from CUSTOMER";
|
||||
return new JdbcCursorItemReaderBuilder<CustomerCredit>().name("customerReader")
|
||||
.dataSource(dataSource())
|
||||
.sql(sql)
|
||||
.rowMapper(new CustomerCreditRowMapper())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcBatchItemWriter<CustomerCredit> itemWriter() {
|
||||
String sql = "UPDATE CUSTOMER set credit = :credit where id = :id";
|
||||
return new JdbcBatchItemWriterBuilder<CustomerCredit>().dataSource(dataSource())
|
||||
.sql(sql)
|
||||
.itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
|
||||
.assertUpdates(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
|
||||
return new JobBuilder("ioSampleJob", jobRepository)
|
||||
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
|
||||
.reader(itemReader())
|
||||
.processor(new CustomerCreditIncreaseProcessor())
|
||||
.writer(itemWriter())
|
||||
.build())
|
||||
.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/sample/jdbc/sql/schema.sql")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JdbcTransactionManager transactionManager(DataSource dataSource) {
|
||||
return new JdbcTransactionManager(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
### Jdbc Cursor and Batch Update sample
|
||||
|
||||
## About
|
||||
|
||||
The purpose of this sample is to show to usage of the
|
||||
`JdbcCursorItemReader` and the `JdbcBatchItemWriter` to make
|
||||
efficient updates to a database table.
|
||||
|
||||
The `JdbcBatchItemWriter` accepts a special form of
|
||||
`PreparedStatementSetter` as a (mandatory) dependency. This is
|
||||
responsible for copying fields from the item to be written to a
|
||||
`PreparedStatement` matching the SQL query that has been
|
||||
injected. The implementation of the
|
||||
`CustomerCreditUpdatePreparedStatementSetter` shows best
|
||||
practice of keeping all the information needed for the execution in
|
||||
one place, since it contains a static constant value (`QUERY`)
|
||||
which is used to configure the query for the writer.
|
||||
|
||||
## Run the sample
|
||||
|
||||
You can run the sample from the command line as following:
|
||||
|
||||
```
|
||||
$>cd spring-batch-samples
|
||||
# Launch the sample using the XML configuration
|
||||
$>../mvnw -Dtest=JdbcCursorFunctionalTests#testLaunchJobWithXmlConfiguration test
|
||||
# Launch the sample using the Java configuration
|
||||
$>../mvnw -Dtest=JdbcCursorFunctionalTests#testLaunchJobWithJavaConfiguration test
|
||||
```
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="sql" value="select ID, NAME, CREDIT from CUSTOMER"/>
|
||||
<property name="verifyCursorPosition" value="${batch.verify.cursor.position}"/>
|
||||
<property name="rowMapper">
|
||||
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
|
||||
<property name="assertUpdates" value="true" />
|
||||
<property name="itemSqlParameterSourceProvider">
|
||||
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
|
||||
</property>
|
||||
<property name="sql" value="UPDATE CUSTOMER set credit = :credit where id = :id" />
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:batch="http://www.springframework.org/schema/batch"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch">
|
||||
<batch:step id="step1">
|
||||
<batch:tasklet>
|
||||
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
|
||||
commit-interval="2"/>
|
||||
</batch:tasklet>
|
||||
</batch:step>
|
||||
</batch:job>
|
||||
|
||||
<bean id="itemProcessor"
|
||||
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor"/>
|
||||
|
||||
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="sql" value="select ID, NAME, CREDIT from CUSTOMER"/>
|
||||
<property name="verifyCursorPosition" value="true"/>
|
||||
<property name="rowMapper">
|
||||
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
|
||||
<property name="assertUpdates" value="true"/>
|
||||
<property name="itemSqlParameterSourceProvider">
|
||||
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider"/>
|
||||
</property>
|
||||
<property name="sql" value="UPDATE CUSTOMER set credit = :credit where id = :id"/>
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,19 @@
|
||||
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);
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-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
|
||||
*
|
||||
* 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.sample.iosample;
|
||||
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
* @author Glenn Renfro
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 2.0
|
||||
*/
|
||||
@SpringJUnitConfig(locations = "/jobs/iosample/jdbcCursor.xml")
|
||||
class JdbcCursorFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2006-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.sample.jdbc;
|
||||
|
||||
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.batch.test.JobLauncherTestUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
* @author Glenn Renfro
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 2.0
|
||||
*/
|
||||
@SpringJUnitConfig(locations = { "/org/springframework/batch/sample/jdbc/job/jdbcCursor.xml",
|
||||
"/simple-job-launcher-context.xml", "/job-runner-context.xml" })
|
||||
class JdbcCursorFunctionalTests {
|
||||
|
||||
@Autowired
|
||||
private JobLauncherTestUtils jobLauncherTestUtils;
|
||||
|
||||
@Test
|
||||
void testLaunchJobWithXmlConfig() throws Exception {
|
||||
// when
|
||||
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();
|
||||
|
||||
// then
|
||||
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLaunchJobWithJavaConfig() throws Exception {
|
||||
// given
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(JdbcCursorReaderBatchWriterSampleJob.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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user