Skip to content

Commit

Permalink
Custom ExecutionContextSerializer for job data de-/serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
dickerpulli committed Aug 6, 2018
1 parent 485fb75 commit 65ce01a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions batch-web-spring-boot-docs/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, Object> context, OutputStream out) throws IOException {
LOGGER.info("I will do serialization");
super.serialize(context, out);
}

@Override
public Map<String, Object> deserialize(InputStream in) throws IOException {
LOGGER.info("I will do deserialization");
return super.deserialize(in);
}

}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</scm>
<properties>
<revision>2.0.2-SNAPSHOT</revision>
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down

0 comments on commit 65ce01a

Please sign in to comment.