Skip to content

Commit

Permalink
Update build to create native container for timestamp-task-3.0.x and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
corneil authored Sep 20, 2024
1 parent 95747b7 commit 37833c8
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 89 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ jobs:
java-version: '17'
artifactory-publish: ${{ inputs.maven-build-only != true }}
jf-artifactory-spring: ${{ secrets.JF_ARTIFACTORY_SPRING }}
jf-mvn-build-commands: '-B clean install -DskipTests'
docker-push: ${{ inputs.maven-build-only != true }}
docker-username: ${{ secrets.DOCKERHUB_USERNAME }}
docker-password: ${{ secrets.DOCKERHUB_TOKEN }}
Expand All @@ -330,7 +331,7 @@ jobs:
- uses: ./.github/actions/build-sample-app
with:
app-dir: 'timestamp-task-3.0.x'
mvn-build-commands: '-B clean install spring-boot:build-image'
mvn-build-commands: '-B clean install spring-boot:build-image -Pnative'
artifactory-publish: ${{ inputs.maven-build-only != true }}
jf-artifactory-spring: ${{ secrets.JF_ARTIFACTORY_SPRING }}
artifactory-repo-deploy-releases: 'libs-milestone-local'
Expand All @@ -347,7 +348,7 @@ jobs:
- uses: ./.github/actions/build-sample-app
with:
app-dir: 'timestamp-batch-3.0.x'
mvn-build-commands: '-B clean install spring-boot:build-image'
mvn-build-commands: '-B clean install spring-boot:build-image -Pnative'
artifactory-publish: ${{ inputs.maven-build-only != true }}
jf-artifactory-spring: ${{ secrets.JF_ARTIFACTORY_SPRING }}
artifactory-repo-deploy-releases: 'libs-milestone-local'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Job job2() {
logger.info("jobstep1:{}", contribution.getStepExecution().getExecutionContext());
logger.info("job2:{}", contribution.getStepExecution().getJobExecution().getExecutionContext());
DateFormat dateFormat = new SimpleDateFormat(config.getFormat());
logger.info("Job2 was run with date {}", dateFormat.format(new Date()));
logger.info("Job2 was run with {}", dateFormat.format(new Date()));
return RepeatStatus.FINISHED;
})
.build())
Expand Down
30 changes: 23 additions & 7 deletions 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.2.3</version>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring</groupId>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
9 changes: 9 additions & 0 deletions timestamp-batch-3.0.x/run-docker.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions timestamp-batch-3.0.x/run-jar.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions timestamp-batch-3.0.x/run-native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
if [ ! -f ./target/timestamp-batch-task ]; then
./mvnw clean native:compile -Pnative
fi
./target/timestamp-batch-task
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,31 +33,30 @@
@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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -83,31 +59,17 @@ private Tasklet getTasklet(String format) {
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();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,32 +32,38 @@
* @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
public void testTimeStampApp(CapturedOutput capturedOutput) throws Exception {
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);
assertThat(output).contains(CREATE_TASK_MESSAGE);
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);
// }
// }

}
22 changes: 19 additions & 3 deletions timestamp-task-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.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring</groupId>
Expand All @@ -16,7 +16,7 @@

<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.4</spring-cloud.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -90,11 +90,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>
Loading

0 comments on commit 37833c8

Please sign in to comment.