Skip to content

Commit

Permalink
Added generation of random execution context values.
Browse files Browse the repository at this point in the history
  • Loading branch information
corneil committed Dec 14, 2023
1 parent 0fda00a commit 76fce1d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
3 changes: 2 additions & 1 deletion timestamp-batch-2.0.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.16</version>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring</groupId>
Expand Down Expand Up @@ -38,6 +38,7 @@
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>[3.1.2,)</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@

package io.spring.configuration;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
Expand All @@ -39,15 +32,18 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@EnableConfigurationProperties({ TimestampBatchTaskProperties.class })
public class TimestampBatchTaskConfiguration {

private static final Log logger = LogFactory.getLog(TimestampBatchTaskProperties.class);
private static final Logger logger = LoggerFactory.getLogger(TimestampBatchTaskProperties.class);

@Autowired
public JobBuilderFactory jobBuilderFactory;
Expand Down Expand Up @@ -96,7 +92,11 @@ public Job job1() {
.start(stepBuilderFactory.get("job1step1")
.tasklet((contribution, chunkContext) -> {
DateFormat dateFormat = new SimpleDateFormat(config.getFormat());
logger.info(String.format("Job1 was run with date %s", dateFormat.format(new Date())));
contribution.getStepExecution().getExecutionContext().put("ctx1", "exec1");
contribution.getStepExecution().getJobExecution().getExecutionContext().put("job-ctx1", "exec-job1");
logger.info("jobstep1:{}", contribution.getStepExecution().getExecutionContext());
logger.info("job1:{}", contribution.getStepExecution().getJobExecution().getExecutionContext());
logger.info("Job1 was run with date {}", dateFormat.format(new Date()));
return RepeatStatus.FINISHED;
})
.build())
Expand All @@ -108,8 +108,12 @@ public Job job2() {
return jobBuilderFactory.get("job2")
.start(stepBuilderFactory.get("job2step1")
.tasklet((contribution, chunkContext) -> {
contribution.getStepExecution().getExecutionContext().put("ctx2", "exec2");
contribution.getStepExecution().getJobExecution().getExecutionContext().put("job-ctx2", "exec-job2");
logger.info("jobstep1:{}", contribution.getStepExecution().getExecutionContext());
logger.info("job2:{}", contribution.getStepExecution().getJobExecution().getExecutionContext());
DateFormat dateFormat = new SimpleDateFormat(config.getFormat());
logger.info(String.format("Job2 was run with date %s", dateFormat.format(new Date())));
logger.info("Job2 was run with date {}", dateFormat.format(new Date()));
return RepeatStatus.FINISHED;
})
.build())
Expand Down
2 changes: 1 addition & 1 deletion timestamp-batch-3.0.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<version>3.1.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package io.spring.configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
Expand All @@ -28,7 +28,6 @@
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -46,7 +45,7 @@
@EnableConfigurationProperties({ TimestampBatchTaskProperties.class })
public class TimestampBatchTaskConfiguration extends DefaultBatchConfiguration {

private static final Log logger = LogFactory.getLog(TimestampBatchTaskProperties.class);
private static final Logger logger = LoggerFactory.getLogger(TimestampBatchTaskProperties.class);


@Value("${spring.batch.jdbc.table-prefix:BATCH_}")
Expand Down Expand Up @@ -80,6 +79,10 @@ public Step job1step1(JobRepository jobRepository, PlatformTransactionManager sp
private Tasklet getTasklet(String format) {
return (contribution, chunkContext) -> {
DateFormat dateFormat = new SimpleDateFormat(config.getFormat());
contribution.getStepExecution().getExecutionContext().put("ctx1", "exec1");
contribution.getStepExecution().getJobExecution().getExecutionContext().put("job-ctx1", "exec-job1");
logger.info("{}:{}", contribution.getStepExecution().getStepName(), contribution.getStepExecution().getExecutionContext());
logger.info("{}:{}", contribution.getStepExecution().getJobExecution().getJobInstance().getJobName(), contribution.getStepExecution().getJobExecution().getExecutionContext());
logger.info(String.format(format, dateFormat.format(new Date())));
return RepeatStatus.FINISHED;
};
Expand Down

0 comments on commit 76fce1d

Please sign in to comment.