Update build to create native container for timestamp-task-3.0.x and timestamp-batch-3.0.x (#200)

* Attempt native tests of timestamp-task-3.0.x and timestamp-batch-3.0.x

* Added org.graalvm.buildtools:native-maven-plugin
This commit is contained in:
Corneil du Plessis
2024-09-20 15:57:12 +02:00
committed by GitHub
parent 95747b74f1
commit 37833c8c79
16 changed files with 136 additions and 89 deletions

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring</groupId>
@@ -16,7 +16,7 @@
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.5</spring-cloud.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
</properties>
<dependencies>
@@ -51,10 +51,6 @@
<artifactId>h2</artifactId>
<version>[2.2,)</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -77,6 +73,10 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
@@ -103,11 +103,27 @@
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<pullPolicy>IF_NOT_PRESENT</pullPolicy>
<env>
<BP_JVM_VERSION>${java.version}</BP_JVM_VERSION>
</env>
<name>springcloudtask/${project.artifactId}:${project.version}</name>
</image>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,9 @@
#!/bin/bash
set +e
docker inspect springcloudtask/timestamp-batch-task:3.0.0 > /dev/null
RC=$?
set -e
if((RC>0)); then
./mvnw clean spring-boot:build-image -Pnative
fi
docker run springcloudtask/timestamp-batch-task:3.0.0

View File

@@ -0,0 +1,5 @@
#!/bin/bash
if [ ! -f ./target/timestamp-batch-task-3.0.0.jar ]; then
./mvnw clean package
fi
java -jar ./target/timestamp-batch-task-3.0.0.jar

View File

@@ -0,0 +1,5 @@
#!/bin/bash
if [ ! -f ./target/timestamp-batch-task ]; then
./mvnw clean native:compile -Pnative
fi
./target/timestamp-batch-task

View File

@@ -16,6 +16,11 @@
package io.spring;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
@@ -28,31 +33,30 @@ import org.springframework.context.annotation.Bean;
@EnableTask
@SpringBootApplication
public class BatchJobApplication {
private static final Logger logger = LoggerFactory.getLogger(BatchJobApplication.class);
public static void main(String[] args) {
SpringApplication.run(BatchJobApplication.class, args);
}
@Bean
public TimestampTask timestampTask(JobLauncher jobLauncher, Job job1, Job job2) {
return new TimestampTask(jobLauncher, job1, job2);
public TimestampTask timestampTask(JobLauncher jobLauncher, Job job1) {
return new TimestampTask(jobLauncher, job1);
}
public static class TimestampTask implements CommandLineRunner {
private final JobLauncher launcher;
private final Job job1;
private final Job job2;
public TimestampTask(JobLauncher launcher, Job job1, Job job2) {
public TimestampTask(JobLauncher launcher, Job job1) {
this.launcher = launcher;
this.job1 = job1;
this.job2 = job2;
}
@Override
public void run(String... strings) throws Exception {
logger.info("starting: {} with {}", job1.getName(), Arrays.asList(strings));
launcher.run(job1, new JobParameters());
launcher.run(job2, new JobParameters());
}
}
}

View File

@@ -16,64 +16,40 @@
package io.spring.configuration;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
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.ConditionalOnProperty;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
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 extends DefaultBatchConfiguration {
public class TimestampBatchTaskConfiguration {
private static final Logger logger = LoggerFactory.getLogger(TimestampBatchTaskProperties.class);
@Value("${spring.batch.jdbc.table-prefix:BATCH_}")
private String tablePrefix;
@Autowired
private TimestampBatchTaskProperties config;
@Bean
@ConditionalOnProperty(name = "spring.datasource.driver-class-name", matchIfMissing = true, havingValue="matchonlyifmissing")
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.addScript("/org/springframework/batch/core/schema-h2.sql")
.generateUniqueName(true).build();
}
protected String getTablePrefix() {
return tablePrefix;
}
/**
* Override default transaction isolation level 'ISOLATION_REPEATABLE_READ' which Oracle does not
* support.
*/
@Bean
@Bean(name = "job1step1")
public Step job1step1(JobRepository jobRepository, PlatformTransactionManager springCloudTaskTransactionManager) {
return new StepBuilder("job1step1", jobRepository)
.tasklet(getTasklet("Job1 was run with date %s"), springCloudTaskTransactionManager).build();
.tasklet(getTasklet("Job1 was run with format %s and result=date %s"), springCloudTaskTransactionManager).build();
}
private Tasklet getTasklet(String format) {
@@ -83,31 +59,17 @@ public class TimestampBatchTaskConfiguration extends DefaultBatchConfiguration {
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())));
logger.info(String.format(format, format, dateFormat.format(new Date())));
return RepeatStatus.FINISHED;
};
}
@Bean
public Job job1(JobRepository jobRepository, Step job1step1) {
@Bean(name = "job1")
public Job job1(JobRepository jobRepository, @Qualifier("job1step1") Step job1step1) {
return new JobBuilder("job1", jobRepository)
.start(job1step1)
.build();
}
@Bean
public Step job2step1(JobRepository jobRepository,
PlatformTransactionManager springCloudTaskTransactionManager
) {
return new StepBuilder("job2step1", jobRepository)
.tasklet(getTasklet("Job2 was run with date %s"), springCloudTaskTransactionManager)
.build();
}
@Bean
public Job job2(JobRepository jobRepository, Step job2step1) {
return new JobBuilder("job2", jobRepository)
.start(job2step1)
.build();
}
}

View File

@@ -16,11 +16,10 @@
package io.spring;
import io.spring.configuration.TimestampBatchTaskConfiguration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
@@ -33,7 +32,14 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Glenn Renfro
*/
@ExtendWith(OutputCaptureExtension.class)
@SpringBootTest(properties = "--timestamp.format=yyyy.......")
@SpringBootTest(classes = {
BatchJobApplication.class,
TimestampBatchTaskConfiguration.class
},
properties = {
"logging.level.root=debug",
"timestamp.format=yyyy......."
})
public class TimestampBatchTaskTests {
@Test
@@ -41,9 +47,9 @@ public class TimestampBatchTaskTests {
final String TEST_DATE_DOTS = ".......";
final String CREATE_TASK_MESSAGE = "Creating: TaskExecution{executionId=";
final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution with executionId=1 with the following";
final String JOB1_MESSAGE = "Job1 was run with date ";
final String JOB2_MESSAGE = "Job2 was run with date ";
final String JOB1_MESSAGE = "Job1 was run with ";
String output = capturedOutput.toString();
assertThat(output).contains(TEST_DATE_DOTS);
@@ -51,14 +57,13 @@ public class TimestampBatchTaskTests {
assertThat(output).contains(UPDATE_TASK_MESSAGE);
assertThat(output).contains(JOB1_MESSAGE);
assertThat(output).contains(JOB2_MESSAGE);
}
@SpringBootApplication
public static class TestTimestampBatchTaskApplication {
public static void main(String[] args) {
SpringApplication.run(TestTimestampBatchTaskApplication.class, args);
}
}
// @SpringBootApplication
// public static class TestTimestampBatchTaskApplication {
// public static void main(String[] args) {
// SpringApplication.run(TestTimestampBatchTaskApplication.class, args);
// }
// }
}