Skip to content

Commit 65ce01a

Browse files
committed
Custom ExecutionContextSerializer for job data de-/serialization.
1 parent 485fb75 commit 65ce01a

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/configuration/TaskExecutorBatchConfiguration.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
2828
import org.springframework.batch.core.launch.JobLauncher;
2929
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
30+
import org.springframework.batch.core.repository.ExecutionContextSerializer;
3031
import org.springframework.batch.core.repository.JobRepository;
3132
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
3233
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
@@ -72,6 +73,9 @@ public class TaskExecutorBatchConfiguration implements BatchConfigurer {
7273

7374
private JobExplorer jobExplorer;
7475

76+
@Autowired(required = false)
77+
private ExecutionContextSerializer serializer;
78+
7579
@Autowired
7680
public void setDataSource(DataSource dataSource) {
7781
this.dataSource = dataSource;
@@ -98,17 +102,18 @@ public JobExplorer getJobExplorer() throws Exception {
98102
}
99103

100104
private JobLauncher createJobLauncher() throws Exception {
101-
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
102-
jobLauncher.setJobRepository(jobRepository);
103-
jobLauncher.setTaskExecutor(taskExecutor);
104-
jobLauncher.afterPropertiesSet();
105-
return jobLauncher;
105+
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
106+
simpleJobLauncher.setJobRepository(jobRepository);
107+
simpleJobLauncher.setTaskExecutor(taskExecutor);
108+
simpleJobLauncher.afterPropertiesSet();
109+
return simpleJobLauncher;
106110
}
107111

108112
protected JobRepository createJobRepository() throws Exception {
109113
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
110114
factory.setDataSource(dataSource);
111115
factory.setTransactionManager(transactionManager);
116+
factory.setSerializer(serializer);
112117
String isolationLevelForCreate = batchConfig.getRepository().getIsolationLevelForCreate();
113118
if (isolationLevelForCreate != null) {
114119
factory.setIsolationLevelForCreate(isolationLevelForCreate);
@@ -146,6 +151,7 @@ public void initialize() throws Exception {
146151

147152
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
148153
jobExplorerFactoryBean.setDataSource(this.dataSource);
154+
jobExplorerFactoryBean.setSerializer(serializer);
149155
String tablePrefix = batchConfig.getRepository().getTablePrefix();
150156
if (tablePrefix != null) {
151157
jobExplorerFactoryBean.setTablePrefix(tablePrefix);

batch-web-spring-boot-docs/src/main/asciidoc/index.adoc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,15 @@ Now let’s take a look at the interfaces / abstract classes you may implement t
113113

114114
https://github.com/codecentric/spring-boot-starter-batch-web/blob/master/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/configuration/ListenerProvider.java[ListenerProvider]
115115

116-
https://github.com/codecentric/spring-boot-starter-batch-web/blob/master/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/metrics/MetricsOutputFormatter.java[MetricsOutputFormatter]
117-
118-
Example:
119-
[source,java]
120-
----
121-
include::{sourcedir}/de/codecentric/batch/metrics/MetricsListener.java[tags=contains,indent=0]
122-
----
116+
https://github.com/codecentric/spring-boot-starter-batch-web/blob/master/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/metrics/MetricsOutputFormatter.java[MetricsOutputFormatter], Example:
117+
https://github.com/codecentric/spring-boot-starter-batch-web/blob/master/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/metrics/MetricsListener.java[MetricsListener]
123118

124119
https://github.com/codecentric/spring-boot-starter-batch-web/blob/master/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/metrics/AbstractBatchMetricsAspect.java[AbstractBatchMetricsAspect]
125120

126121
https://github.com/codecentric/spring-boot-starter-batch-web/blob/master/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/logging/JobLogFileNameCreator.java[JobLogFileNameCreator]
127122

123+
=== Custom job data de-/serialization
124+
You also be able to use your own custom database serializer for job repository. You just have to add a bean of type ExecutionContextSerializer into the application context. See example in batch-boot-file-to-db (DataSourceConfiguration.java).
128125

129126
== Initscript Template
130127

batch-web-spring-boot-samples/batch-boot-file-to-db/src/main/java/de/codecentric/batch/filetodb/configuration/DataSourceConfiguration.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package de.codecentric.batch.filetodb.configuration;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.util.Map;
7+
38
import javax.sql.DataSource;
49

10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.batch.core.repository.ExecutionContextSerializer;
13+
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
514
import org.springframework.context.annotation.Bean;
615
import org.springframework.context.annotation.Configuration;
716
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
@@ -19,4 +28,28 @@ public DataSource dataSourcePartner() {
1928
"classpath:org/springframework/batch/core/schema-hsqldb.sql", "classpath:schema-partner.sql")//
2029
.build();
2130
}
31+
32+
@Bean
33+
public ExecutionContextSerializer serializer() {
34+
return new CustomSerializer();
35+
}
36+
37+
private static class CustomSerializer extends Jackson2ExecutionContextStringSerializer {
38+
39+
private static final Logger LOGGER = LoggerFactory.getLogger(CustomSerializer.class);
40+
41+
@Override
42+
public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
43+
LOGGER.info("I will do serialization");
44+
super.serialize(context, out);
45+
}
46+
47+
@Override
48+
public Map<String, Object> deserialize(InputStream in) throws IOException {
49+
LOGGER.info("I will do deserialization");
50+
return super.deserialize(in);
51+
}
52+
53+
}
54+
2255
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</scm>
5454
<properties>
5555
<revision>2.0.2-SNAPSHOT</revision>
56-
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
56+
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
5757
<java.version>1.8</java.version>
5858
<maven.compiler.source>${java.version}</maven.compiler.source>
5959
<maven.compiler.target>${java.version}</maven.compiler.target>

0 commit comments

Comments
 (0)