diff --git a/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/configuration/TaskExecutorBatchConfiguration.java b/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/configuration/TaskExecutorBatchConfiguration.java index a1fceb1..49a6ec4 100644 --- a/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/configuration/TaskExecutorBatchConfiguration.java +++ b/batch-web-spring-boot-autoconfigure/src/main/java/de/codecentric/batch/configuration/TaskExecutorBatchConfiguration.java @@ -27,6 +27,7 @@ import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.batch.core.repository.ExecutionContextSerializer; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; @@ -72,6 +73,9 @@ public class TaskExecutorBatchConfiguration implements BatchConfigurer { private JobExplorer jobExplorer; + @Autowired(required = false) + private ExecutionContextSerializer serializer; + @Autowired public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; @@ -98,17 +102,18 @@ public JobExplorer getJobExplorer() throws Exception { } private JobLauncher createJobLauncher() throws Exception { - SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); - jobLauncher.setJobRepository(jobRepository); - jobLauncher.setTaskExecutor(taskExecutor); - jobLauncher.afterPropertiesSet(); - return jobLauncher; + SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher(); + simpleJobLauncher.setJobRepository(jobRepository); + simpleJobLauncher.setTaskExecutor(taskExecutor); + simpleJobLauncher.afterPropertiesSet(); + return simpleJobLauncher; } protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); + factory.setSerializer(serializer); String isolationLevelForCreate = batchConfig.getRepository().getIsolationLevelForCreate(); if (isolationLevelForCreate != null) { factory.setIsolationLevelForCreate(isolationLevelForCreate); @@ -146,6 +151,7 @@ public void initialize() throws Exception { JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean(); jobExplorerFactoryBean.setDataSource(this.dataSource); + jobExplorerFactoryBean.setSerializer(serializer); String tablePrefix = batchConfig.getRepository().getTablePrefix(); if (tablePrefix != null) { jobExplorerFactoryBean.setTablePrefix(tablePrefix); diff --git a/batch-web-spring-boot-docs/src/main/asciidoc/index.adoc b/batch-web-spring-boot-docs/src/main/asciidoc/index.adoc index 3185fd2..e489b85 100644 --- a/batch-web-spring-boot-docs/src/main/asciidoc/index.adoc +++ b/batch-web-spring-boot-docs/src/main/asciidoc/index.adoc @@ -113,18 +113,15 @@ Now let’s take a look at the interfaces / abstract classes you may implement t 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] -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: -[source,java] ----- -include::{sourcedir}/de/codecentric/batch/metrics/MetricsListener.java[tags=contains,indent=0] ----- +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: +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] 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] 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] +=== Custom job data de-/serialization +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). == Initscript Template diff --git a/batch-web-spring-boot-samples/batch-boot-file-to-db/src/main/java/de/codecentric/batch/filetodb/configuration/DataSourceConfiguration.java b/batch-web-spring-boot-samples/batch-boot-file-to-db/src/main/java/de/codecentric/batch/filetodb/configuration/DataSourceConfiguration.java index 6236d24..4c726fe 100755 --- a/batch-web-spring-boot-samples/batch-boot-file-to-db/src/main/java/de/codecentric/batch/filetodb/configuration/DataSourceConfiguration.java +++ b/batch-web-spring-boot-samples/batch-boot-file-to-db/src/main/java/de/codecentric/batch/filetodb/configuration/DataSourceConfiguration.java @@ -1,7 +1,16 @@ package de.codecentric.batch.filetodb.configuration; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; + import javax.sql.DataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.repository.ExecutionContextSerializer; +import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; @@ -19,4 +28,28 @@ public DataSource dataSourcePartner() { "classpath:org/springframework/batch/core/schema-hsqldb.sql", "classpath:schema-partner.sql")// .build(); } + + @Bean + public ExecutionContextSerializer serializer() { + return new CustomSerializer(); + } + + private static class CustomSerializer extends Jackson2ExecutionContextStringSerializer { + + private static final Logger LOGGER = LoggerFactory.getLogger(CustomSerializer.class); + + @Override + public void serialize(Map context, OutputStream out) throws IOException { + LOGGER.info("I will do serialization"); + super.serialize(context, out); + } + + @Override + public Map deserialize(InputStream in) throws IOException { + LOGGER.info("I will do deserialization"); + return super.deserialize(in); + } + + } + } diff --git a/pom.xml b/pom.xml index 76cde82..1dbe5a7 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,7 @@ 2.0.2-SNAPSHOT - 2.0.3.RELEASE + 2.0.4.RELEASE 1.8 ${java.version} ${java.version}