Skip to content

Commit fba162f

Browse files
cppwfsmminella
authored andcommitted
Support JDBCWriter for singlestep batch
resolves 677
1 parent 80f39be commit fba162f

File tree

5 files changed

+480
-1
lines changed

5 files changed

+480
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.task.batch.autoconfigure.jdbc;
18+
19+
import java.util.Map;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.springframework.batch.item.ItemWriter;
24+
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
25+
import org.springframework.batch.item.database.ItemSqlParameterSourceProvider;
26+
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
29+
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
35+
36+
/**
37+
* Autconfiguration for a {@code JdbcBatchItemWriter}.
38+
*
39+
* @author Glenn Renfro
40+
* @since 2.3
41+
*/
42+
@Configuration
43+
@EnableConfigurationProperties(JdbcItemWriterProperties.class)
44+
@AutoConfigureAfter(BatchAutoConfiguration.class)
45+
public class JdbcItemWriterAutoConfiguration {
46+
47+
@Autowired(required = false)
48+
private ItemPreparedStatementSetter itemPreparedStatementSetter;
49+
50+
@Autowired(required = false)
51+
private ItemSqlParameterSourceProvider itemSqlParameterSourceProvider;
52+
53+
private JdbcItemWriterProperties properties;
54+
55+
private DataSource dataSource;
56+
57+
public JdbcItemWriterAutoConfiguration(DataSource dataSource,
58+
JdbcItemWriterProperties properties) {
59+
this.dataSource = dataSource;
60+
this.properties = properties;
61+
}
62+
63+
@Bean
64+
@ConditionalOnMissingBean
65+
@ConditionalOnProperty(prefix = "spring.batch.job.jdbcwriter", name = "name")
66+
public ItemWriter<Map<Object, Object>> itemWriter() {
67+
68+
JdbcBatchItemWriterBuilder<Map<Object, Object>> jdbcBatchItemWriterBuilder = new JdbcBatchItemWriterBuilder<Map<Object, Object>>()
69+
.dataSource(this.dataSource).sql(this.properties.getSql());
70+
if (this.itemPreparedStatementSetter != null) {
71+
jdbcBatchItemWriterBuilder
72+
.itemPreparedStatementSetter(this.itemPreparedStatementSetter);
73+
}
74+
else if (this.itemSqlParameterSourceProvider != null) {
75+
jdbcBatchItemWriterBuilder
76+
.itemSqlParameterSourceProvider(this.itemSqlParameterSourceProvider);
77+
}
78+
else {
79+
jdbcBatchItemWriterBuilder.columnMapped();
80+
}
81+
jdbcBatchItemWriterBuilder.assertUpdates(this.properties.isAssertUpdates());
82+
return jdbcBatchItemWriterBuilder.build();
83+
}
84+
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2020-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.task.batch.autoconfigure.jdbc;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Properties to configure a {@code JdbcItemWriter}.
23+
*
24+
* @author Glenn Renfro
25+
* @since 2.3
26+
*/
27+
@ConfigurationProperties(prefix = "spring.batch.job.jdbcwriter")
28+
public class JdbcItemWriterProperties {
29+
30+
private String name;
31+
32+
private String sql;
33+
34+
private boolean assertUpdates = true;
35+
36+
/**
37+
* @return The current sql statement used to update the database.
38+
*/
39+
public String getSql() {
40+
return sql;
41+
}
42+
43+
/**
44+
* Sets the sql statement to be used to update the database.
45+
* @param sql the sql statement to be used.
46+
*/
47+
public void setSql(String sql) {
48+
this.sql = sql;
49+
}
50+
51+
/**
52+
* @return if returns true then each insert will be confirmed to have at least one
53+
* insert in the database.
54+
*/
55+
public boolean isAssertUpdates() {
56+
return assertUpdates;
57+
}
58+
59+
/**
60+
* If set to true, confirms that every insert results in the update of at least one
61+
* row in the database. Defaults to True
62+
*/
63+
public void setAssertUpdates(boolean assertUpdates) {
64+
this.assertUpdates = assertUpdates;
65+
}
66+
67+
/**
68+
* Returns the configured value of the name used to calculate {@code ExecutionContext}
69+
* keys.
70+
* @return the name
71+
*/
72+
public String getName() {
73+
return name;
74+
}
75+
76+
/**
77+
* The name used to calculate the key within the
78+
* {@link org.springframework.batch.item.ExecutionContext}.
79+
* @param name name of the writer instance
80+
* @see org.springframework.batch.item.ItemStreamSupport#setName(String)
81+
*/
82+
public void setName(String name) {
83+
this.name = name;
84+
}
85+
86+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.task.batch.autoconfigure.FlatFileItemReaderAutoConfiguration,\
22
org.springframework.cloud.task.batch.autoconfigure.RangeConverter,\
33
org.springframework.cloud.task.batch.autoconfigure.SingleStepJobAutoConfiguration,\
4-
org.springframework.cloud.task.batch.autoconfigure.FlatFileItemWriterAutoConfiguration
4+
org.springframework.cloud.task.batch.autoconfigure.FlatFileItemWriterAutoConfiguration, \
5+
org.springframework.cloud.task.batch.autoconfigure.jdbc.JdbcItemWriterAutoConfiguration

0 commit comments

Comments
 (0)