Added scenario-task-3

This commit is contained in:
Corneil du Plessis
2024-05-16 09:57:40 +02:00
parent 626f0e7eb2
commit a095b97c7d
19 changed files with 1681 additions and 3 deletions

View File

@@ -304,7 +304,23 @@ jobs:
docker-username: ${{ secrets.DOCKERHUB_USERNAME }}
docker-password: ${{ secrets.DOCKERHUB_TOKEN }}
docker-images: >-
springcloudtask/scenario-task:2.0.0-SNAPSHOT
springcloudtask/scenario-task:2.0.0
scenario-task-3:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/build-sample-app
with:
app-dir: 'scenario-task-3'
mvn-build-commands: '-B clean install spring-boot:build-image'
artifactory-publish: ${{ inputs.maven-build-only != true }}
jf-artifactory-spring: ${{ secrets.JF_ARTIFACTORY_SPRING }}
docker-push: ${{ inputs.maven-build-only != true }}
docker-username: ${{ secrets.DOCKERHUB_USERNAME }}
docker-password: ${{ secrets.DOCKERHUB_TOKEN }}
docker-images: >-
springcloudtask/scenario-task:3.0.0
timestamp-task-30x:
runs-on: ubuntu-latest
@@ -395,6 +411,7 @@ jobs:
- stream-dev-guides-composed-http-transformer-kotlin-rabbitmq
- task-demo-metrics-prometheus
- scenario-task
- scenario-task-3
- timestamp-task-30x
- timestamp-batch-30x
- timestamp-task-20x

33
scenario-task-3/.gitignore vendored Normal file
View File

@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

View File

@@ -0,0 +1,117 @@
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}

Binary file not shown.

View File

@@ -0,0 +1 @@
distributionUrl=https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.zip

155
scenario-task-3/README.adoc Normal file
View File

@@ -0,0 +1,155 @@
# Introduction
Allows user to test various success and failures scenarios when task-batch apps are launched by Spring Cloud Data Flow.
## Build the project
### Maven Build
```
./mvnw clean install
```
### Docker Build
```
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=spring/scenario-task:3.0.0
```
## Launching the app
### Command Line
```
java -jar scenario-task-3.0.0-SNAPSHOT.jar
```
### Docker
```
docker run spring/scenario-task:3.0.0
```
### Properties
All properties should be prefixed with `io.spring.`
* *jobName* - The name associated with the batch job. The default is "scenario-job".
* *stepName* - The name associated with the single step for the job. The default is "scenario-step".
* *failBatch* - If true, the batch will throw a {@link ExpectedException}. Defaults to false.
* *failTask* - If true, the task will throw a {@link ExpectedException}. Defaults to false.
* *launchBatchJob* - If true, the task will launch a sample batch job. Defaults to true.
* *pauseInSeconds* - How long the batch job should pause in the step. Defaults to 0.
* *includeRunidIncrementer* - If true a runIdIncrementer will be applied to the batch job. Defaults to false.
## Scenarios
### Default Scenario
By default the scenario-task application will launch a Spring Cloud Task and launch a Batch job.
The job's name will be `scenario-job`. This job will succeed and the task will also succeed.
Subsequent launches will have the batch job produce no results and the task will succeed.
NOTE: When testing multiple launches of the scenario-task app with `launchBatchJob` enabled (the default)
be sure to give each test scenario a new job name by setting the `jobName` property to a unique value. Unless you are restarting a failed job.
### Fail Job But A Successful Task
To have the job fail and the task succeed set the `failBatch` property to `false`.
With the `failBatch` property set to `false` the job's step will throw an `ExpectedException` for the first execution of this job instance.
However, if you rerun the application using the same identifiable job parameters the second job execution will succeed.
This gives you the ability test fail and success scenarios of a batch job.
For example we will setup our environment to connect to a database and then launch our scenario-task with the settings to fail the batch for the jobName `testSamp1`.
```
export spring_datasource_url=jdbc:<your db>
export spring_datasource_username=<your username>
export spring_datasource_password=<your password>
export spring_datasource_driverClassName=<your driver>
java -jar scenario-task-2.0.0-SNAPSHOT.jar --io.spring.failBatch=true --io.spring.jobName=testSamp1
```
Now looking at our results we see that the `ExpectedException` has been thrown as expected, the Job has failed, but, the task has succeeded.
So now let's let's launch our scenario-task again with the same settings.
```
java -jar scenario-task-2.0.0-SNAPSHOT.jar --io.spring.failBatch=true --io.spring.jobName=testSamp1
```
Now looking at our results we see that job succeeded and the task succeeded.
This is because our scenario-task saw that for this job instance the first run
failed and thus it will assume you want it to succeed this time and it will not
throw the `ExpectedException` again. Thus, the job is successful.
### Fail Job And Have Task Fail Because Of The Job Failure
To have the task fail because the job failed, set the `failBatch` property to `false` and the `spring.cloud.task.batch.fail-on-job-failure` to `true`.
With the `failBatch` property set to `false` the job's step will throw an `ExpectedException` for the first execution of this job instance.
However if you rerun the application using the same identifiable job parameters the second job execution will succeed.
This gives you the ability test fail and success scenarios of a batch job.
For example we will setup our environment to connect to a database and then launch our scenario-task with the settings to fail the batch for the jobName `testSamp2`.
```
export spring_datasource_url=jdbc:<your db>
export spring_datasource_username=<your username>
export spring_datasource_password=<your password>
export spring_datasource_driverClassName=<your driver>
java -jar scenario-task-2.0.0-SNAPSHOT.jar --io.spring.failBatch=true --io.spring.jobName=testSamp2 --spring.cloud.task.batch.fail-on-job-failure=true
```
Now looking at our results we see that the `ExpectedException` has been thrown as expected, the Job has failed, and the task has failed.
So now let's let's launch our scenario-task again with the same settings.
```
java -jar scenario-task-2.0.0-SNAPSHOT.jar --io.spring.failBatch=true --io.spring.jobName=testSamp2 --spring.cloud.task.batch.fail-on-job-failure=true
```
Now looking at our results we see that job succeeded and the task succeeded.
This is because our scenario-task saw that for this job instance the first run
failed and thus it will assume you want it to succeed this time and it will not
throw the `ExpectedException` again. Thus, the job is successful.
### Successful Task Launch With No Job
For this scenario we want launch the task that terminates successfully (exit code of 0) and have no associated batch job run. Also, we want to set the name of our task-execution.
```
java -jar scenario-task-2.0.0-SNAPSHOT.jar --io.spring.launchBatchJob=false --spring.application.name=testTask1
```
We can see that the task launched and completed successfully with no associated batch job.
### Fail Task Launch With No Job
For this scenario we want launch the task that terminates unsuccessfully (exit code of 1) and have no associated batch job run. Also, we want to set the name of our task-execution.
```
java -jar scenario-task-2.0.0-SNAPSHOT.jar --io.spring.launchBatchJob=false --spring.application.name=testTask1 --io.spring.failTask=true
```
We can see that the task launched and failed because a `ExpectedException` was thrown. Also, the task execution had no associated batch job.
### Composed Task Runner Examples
For this scenario we will register the scenario-task application with Spring Cloud Data Flow and launch it within a composed task.
This will test the fail and restart capabilities of an composed task.
#### Register The Scenario Task Application
Using The Spring Cloud Data Flow shell enter the following command:
```
app register --name scenario --type task --uri maven://io.spring:scenario-task:2.0.0-SNAPSHOT
```
#### Create Composed Task Definition
Using The Spring Cloud Data Flow shell enter the following command:
```
task create mycomposedtask --definition "1: timestamp && scenario && 2: timestamp"
```
#### Launch Composed Task Definition
For this step we want the `scenario` task to fail so that the composed task execution will fail.
This gives us the opportunity to show how to restart a failed batch job using Spring Cloud Data Flow.
Using The Spring Cloud Data Flow shell enter the following command:
```
task launch --name mycomposedtask --properties "app.mycomposedtask.scenario.io.spring.failBatch=true,app.mycomposedtask.scenario.spring.cloud.task.batch.failOnJobFailure=true"
```
Once the task execution is complete check the status of the job executions by execution the job by executing the following command:
```
job execution list
```
You should see 2 jobs, one is the `mycomposedtask` job that executions the composed task and the other is the scenario-task job named `scenariojobtest` as shown below:
```
╔═══╤═══════╤═══════════════╤════════════════════════════╤═════╤══════════════════╗
║ID │Task ID│ Job Name │ Start Time │Step │Definition Status ║
╠═══╪═══════╪═══════════════╪════════════════════════════╪═════╪══════════════════╣
║117│302 │scenariojobtest│Tue Nov 24 11:58:45 EST 2020│1 │Created ║
║116│300 │mycomposedtask │Tue Nov 24 11:58:37 EST 2020│2 │Created ║
╚═══╧═══════╧═══════════════╧════════════════════════════╧═════╧══════════════════╝
```
To restart the composed task execute the `job execution restart --id <id of the mycomposed task>` as shown in the example below:
```
job execution restart --id 116
```
This will restart the job and the composed task will relaunch the `scenario` task and then upon its successful completion it will run the 2nd timestamp task app.

111
scenario-task-3/deps.txt Normal file
View File

@@ -0,0 +1,111 @@
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< io.spring:scenario-task >-----------------------
[INFO] Building scenario-task 2.0.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dependency:3.3.0:tree (default-cli) @ scenario-task ---
[INFO] io.spring:scenario-task:jar:2.0.0-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.7.18:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.7.18:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.7.18:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.18:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.18:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.12:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.12:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.30:compile
[INFO] | +- com.zaxxer:HikariCP:jar:4.0.3:compile
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] | \- org.springframework:spring-jdbc:jar:5.3.31:compile
[INFO] | +- org.springframework:spring-beans:jar:5.3.31:compile
[INFO] | \- org.springframework:spring-tx:jar:5.3.31:compile
[INFO] +- org.springframework.cloud:spring-cloud-starter-task:jar:2.4.5:compile
[INFO] | +- org.springframework.cloud:spring-cloud-task-core:jar:2.4.5:compile
[INFO] | | +- org.springframework.cloud:spring-cloud-deployer-autoconfigure:jar:2.7.4:compile
[INFO] | | +- org.springframework.cloud:spring-cloud-deployer-resource-support:jar:2.7.4:compile
[INFO] | | +- org.springframework.batch:spring-batch-infrastructure:jar:4.3.10:compile
[INFO] | | | \- org.springframework.retry:spring-retry:jar:1.3.4:compile
[INFO] | | +- org.springframework.data:spring-data-commons:jar:2.7.18:compile
[INFO] | | \- io.micrometer:micrometer-core:jar:1.9.17:compile
[INFO] | | +- org.hdrhistogram:HdrHistogram:jar:2.1.12:compile
[INFO] | | \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime
[INFO] | +- org.springframework.cloud:spring-cloud-task-batch:jar:2.4.5:compile
[INFO] | \- org.springframework.cloud:spring-cloud-task-stream:jar:2.4.5:compile
[INFO] | \- org.springframework.cloud:spring-cloud-deployer-spi:jar:2.7.4:compile
[INFO] | \- io.projectreactor:reactor-core:jar:3.4.34:compile
[INFO] | \- org.reactivestreams:reactive-streams:jar:1.0.4:compile
[INFO] +- org.postgresql:postgresql:jar:42.3.8:runtime
[INFO] | \- org.checkerframework:checker-qual:jar:3.5.0:compile
[INFO] +- com.oracle.database.jdbc:ojdbc8:jar:21.1.0.0:runtime
[INFO] +- org.mariadb.jdbc:mariadb-java-client:jar:3.1.4:compile
[INFO] | \- com.github.waffle:waffle-jna:jar:3.2.0:compile
[INFO] | +- net.java.dev.jna:jna:jar:5.12.1:compile
[INFO] | +- net.java.dev.jna:jna-platform:jar:5.12.1:compile
[INFO] | +- org.slf4j:jcl-over-slf4j:jar:1.7.36:compile
[INFO] | \- com.github.ben-manes.caffeine:caffeine:jar:2.9.3:compile
[INFO] | \- com.google.errorprone:error_prone_annotations:jar:2.10.0:compile
[INFO] +- com.h2database:h2:jar:2.1.214:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.18:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.7.18:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.18:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.7.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.4.11:test
[INFO] | | \- net.minidev:accessors-smart:jar:2.4.11:test
[INFO] | | \- org.ow2.asm:asm:jar:9.3:test
[INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
[INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
[INFO] | +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
[INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | | | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
[INFO] | | | \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
[INFO] | | \- org.junit.platform:junit-platform-engine:jar:1.8.2:test
[INFO] | +- org.mockito:mockito-core:jar:4.5.1:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.12.23:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.12.23:test
[INFO] | | \- org.objenesis:objenesis:jar:3.2:test
[INFO] | +- org.mockito:mockito-junit-jupiter:jar:4.5.1:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.1:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-core:jar:5.3.31:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.3.31:compile
[INFO] | +- org.springframework:spring-test:jar:5.3.31:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.9.1:test
[INFO] +- org.springframework.boot:spring-boot-starter-batch:jar:2.7.18:compile
[INFO] | \- org.springframework.batch:spring-batch-core:jar:4.3.10:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.5:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.5:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.5:compile
[INFO] | +- javax.batch:javax.batch-api:jar:1.0:compile
[INFO] | +- org.springframework:spring-aop:jar:5.3.31:compile
[INFO] | \- org.springframework:spring-context:jar:5.3.31:compile
[INFO] | \- org.springframework:spring-expression:jar:5.3.31:compile
[INFO] +- org.assertj:assertj-core:jar:3.22.0:test
[INFO] +- org.testcontainers:postgresql:jar:1.19.7:test
[INFO] | \- org.testcontainers:jdbc:jar:1.19.7:test
[INFO] | \- org.testcontainers:database-commons:jar:1.19.7:test
[INFO] +- org.testcontainers:junit-jupiter:jar:1.19.7:test
[INFO] | \- org.testcontainers:testcontainers:jar:1.19.7:test
[INFO] | +- junit:junit:jar:4.13.2:test
[INFO] | | \- org.hamcrest:hamcrest-core:jar:2.2:test
[INFO] | +- org.apache.commons:commons-compress:jar:1.24.0:test
[INFO] | +- org.rnorth.duct-tape:duct-tape:jar:1.0.8:test
[INFO] | | \- org.jetbrains:annotations:jar:17.0.0:test
[INFO] | +- com.github.docker-java:docker-java-api:jar:3.3.6:test
[INFO] | \- com.github.docker-java:docker-java-transport-zerodep:jar:3.3.6:test
[INFO] | \- com.github.docker-java:docker-java-transport:jar:3.3.6:test
[INFO] \- org.springframework.boot:spring-boot-configuration-processor:jar:2.7.18:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.123 s
[INFO] Finished at: 2024-05-15T14:50:20+02:00
[INFO] ------------------------------------------------------------------------

310
scenario-task-3/mvnw vendored Executable file
View File

@@ -0,0 +1,310 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven Start Up Batch script
#
# Required ENV vars:
# ------------------
# JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
# M2_HOME - location of maven2's installed home dir
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
# e.g. to debug Maven itself, use
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
# ----------------------------------------------------------------------------
if [ -z "$MAVEN_SKIP_RC" ] ; then
if [ -f /etc/mavenrc ] ; then
. /etc/mavenrc
fi
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
fi
# OS specific support. $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true;;
Darwin*) darwin=true
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
if [ -z "$JAVA_HOME" ]; then
if [ -x "/usr/libexec/java_home" ]; then
export JAVA_HOME="`/usr/libexec/java_home`"
else
export JAVA_HOME="/Library/Java/Home"
fi
fi
;;
esac
if [ -z "$JAVA_HOME" ] ; then
if [ -r /etc/gentoo-release ] ; then
JAVA_HOME=`java-config --jre-home`
fi
fi
if [ -z "$M2_HOME" ] ; then
## resolve links - $0 may be a link to maven's home
PRG="$0"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
M2_HOME=`dirname "$PRG"`/..
# make it fully qualified
M2_HOME=`cd "$M2_HOME" && pwd`
cd "$saveddir"
# echo Using m2 at $M2_HOME
fi
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --unix "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For Mingw, ensure paths are in UNIX format before anything is touched
if $mingw ; then
[ -n "$M2_HOME" ] &&
M2_HOME="`(cd "$M2_HOME"; pwd)`"
[ -n "$JAVA_HOME" ] &&
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
fi
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
if $darwin ; then
javaHome="`dirname \"$javaExecutable\"`"
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
else
javaExecutable="`readlink -f \"$javaExecutable\"`"
fi
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
fi
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly." >&2
echo " We cannot execute $JAVACMD" >&2
exit 1
fi
if [ -z "$JAVA_HOME" ] ; then
echo "Warning: JAVA_HOME environment variable is not set."
fi
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
if [ -z "$1" ]
then
echo "Path not specified to find_maven_basedir"
return 1
fi
basedir="$1"
wdir="$1"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
if [ -d "${wdir}" ]; then
wdir=`cd "$wdir/.."; pwd`
fi
# end of workaround
done
echo "${basedir}"
}
# concatenates all lines of a file
concat_lines() {
if [ -f "$1" ]; then
echo "$(tr -s '\n' ' ' < "$1")"
fi
}
BASE_DIR=`find_maven_basedir "$(pwd)"`
if [ -z "$BASE_DIR" ]; then
exit 1;
fi
##########################################################################################
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
# This allows using the maven wrapper in projects that prohibit checking in binary data.
##########################################################################################
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found .mvn/wrapper/maven-wrapper.jar"
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
fi
if [ -n "$MVNW_REPOURL" ]; then
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
else
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
fi
while IFS="=" read key value; do
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
esac
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
if [ "$MVNW_VERBOSE" = true ]; then
echo "Downloading from: $jarUrl"
fi
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
if $cygwin; then
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
fi
if command -v wget > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found wget ... using wget"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
wget "$jarUrl" -O "$wrapperJarPath"
else
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
fi
elif command -v curl > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found curl ... using curl"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
curl -o "$wrapperJarPath" "$jarUrl" -f
else
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Falling back to using Java to download"
fi
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
# For Cygwin, switch paths to Windows format before running javac
if $cygwin; then
javaClass=`cygpath --path --windows "$javaClass"`
fi
if [ -e "$javaClass" ]; then
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Compiling MavenWrapperDownloader.java ..."
fi
# Compiling the Java class
("$JAVA_HOME/bin/javac" "$javaClass")
fi
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
# Running the downloader
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Running MavenWrapperDownloader.java ..."
fi
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
fi
fi
fi
fi
##########################################################################################
# End of extension
##########################################################################################
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
if [ "$MVNW_VERBOSE" = true ]; then
echo $MAVEN_PROJECTBASEDIR
fi
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --path --windows "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
fi
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

182
scenario-task-3/mvnw.cmd vendored Normal file
View File

@@ -0,0 +1,182 @@
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM set title of command window
title %0
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
)
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist %WRAPPER_JAR% (
if "%MVNW_VERBOSE%" == "true" (
echo Found %WRAPPER_JAR%
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...
echo Downloading from: %DOWNLOAD_URL%
)
powershell -Command "&{"^
"$webclient = new-object System.Net.WebClient;"^
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
"}"^
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
"}"
if "%MVNW_VERBOSE%" == "true" (
echo Finished downloading %WRAPPER_JAR%
)
)
@REM End of extension
@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
set MAVEN_CMD_LINE_ARGS=%*
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%" == "on" pause
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
exit /B %ERROR_CODE%

144
scenario-task-3/pom.xml Normal file
View File

@@ -0,0 +1,144 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.spring</groupId>
<artifactId>scenario-task</artifactId>
<version>3.0.0-SNAPSHOT</version>
<name>scenario-task</name>
<description>Exercises failure and success conditions for task and batch.</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
<test-container-version>1.19.7</test-container-version>
<oracle-jdbc.version>21.1.0.0</oracle-jdbc.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${test-container-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${test-container-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-bom</artifactId>
<version>${oracle-jdbc.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<compilerVersion>17</compilerVersion>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>springcloudtask/${project.artifactId}:${project.version}</name>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,38 @@
<!--
~ Copyright 2020 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 https://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>scenario-task</id>
<dependencySets>
<dependencySet>
<includes>
<include>io.spring:scenario-task</include>
</includes>
<outputDirectory>maven</outputDirectory>
<outputFileNameMapping>scenario-task.jar</outputFileNameMapping>
</dependencySet>
</dependencySets>
<files>
<file>
<source>./target/scenario-task-${project.version}.jar</source>
<outputDirectory>.</outputDirectory>
<destName>scenario-task.jar</destName>
</file>
</files>
</assembly>

View File

@@ -0,0 +1,15 @@
package io.spring.scenariotask;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
@SpringBootApplication
@EnableTask
public class ScenarioTaskApplication {
public static void main(String[] args) {
SpringApplication.run(ScenarioTaskApplication.class, args);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.scenariotask.configuration;
/**
* Exception thrown when user requests an error to occur during Task or Batch executions.
*
* @author Glenn Renfro
*/
public class ExpectedException extends Exception{
public ExpectedException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.scenariotask.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties that allow the user to specify how they want the Batch/Task app to behave for a test.
*
* @author Glenn Renfro
*/
@ConfigurationProperties(prefix = "io.spring")
public class ScenarioProperties {
/**
* The name associated with the batch job. The default is "scenario-job".
*/
private String jobName = "scenario-job";
/**
* The name associated with the single step for the job. The default is "scenario-step".
*/
private String stepName = "scenario-step";
/**
* If true, the batch will throw a {@link ExpectedException}. Defaults to false.
*/
private boolean failBatch;
/**
* If true, the task will throw a {@link ExpectedException}. Defaults to false.
*/
private boolean failTask;
/**
* If true, the task will launch a sample batch job. Defaults to true.
*/
private boolean launchBatchJob = true;
/**
* How long the batch job should pause in the step. Defaults to 0.
*/
private int pauseInSeconds = 0;
/**
* If true a runIdIncrementer will be applied to the batch job. Defaults to false.
*/
private boolean includeRunidIncrementer;
public String getJobName() {
return jobName;
}
public void setJobName(String jobName) {
this.jobName = jobName;
}
public boolean isFailBatch() {
return failBatch;
}
public void setFailBatch(boolean failBatch) {
this.failBatch = failBatch;
}
public boolean isFailTask() {
return failTask;
}
public void setFailTask(boolean failTask) {
this.failTask = failTask;
}
public int getPauseInSeconds() {
return pauseInSeconds;
}
public void setPauseInSeconds(int pauseInSeconds) {
this.pauseInSeconds = pauseInSeconds;
}
public String getStepName() {
return stepName;
}
public void setStepName(String stepName) {
this.stepName = stepName;
}
public boolean isLaunchBatchJob() {
return launchBatchJob;
}
public void setLaunchBatchJob(boolean launchBatchJob) {
this.launchBatchJob = launchBatchJob;
}
public boolean isIncludeRunidIncrementer() {
return includeRunidIncrementer;
}
public void setIncludeRunidIncrementer(boolean includeRunidIncrementer) {
this.includeRunidIncrementer = includeRunidIncrementer;
}
@Override
public String toString() {
return "ScenarioProperties{" +
"jobName='" + jobName + '\'' +
", stepName='" + stepName + '\'' +
", failBatch=" + failBatch +
", failTask=" + failTask +
", launchBatchJob=" + launchBatchJob +
", pauseInSeconds=" + pauseInSeconds +
", includeRunidIncrementer=" + includeRunidIncrementer +
'}';
}
}

View File

@@ -0,0 +1,162 @@
/*
* Copyright 2020-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.scenariotask.configuration;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
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.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
/**
* Configure the Task and or Batch components of the test application.
*
* @author Glenn Renfro
*/
@Configuration
@EnableConfigurationProperties(ScenarioProperties.class)
public class ScenarioTaskConfiguration {
private static final Log logger = LogFactory.getLog(ScenarioTaskConfiguration.class);
@Configuration
@ConditionalOnProperty(value = "io.spring.launch-batch-job", havingValue = "true", matchIfMissing = true)
static class BatchConfig {
private int jobExecutionCount(ScenarioProperties properties, JobExplorer jobExplorer) {
JobInstance jobInstance = jobExplorer.getLastJobInstance(properties.getJobName());
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
return jobExecutions.size();
}
Tasklet getTaskLet(
ScenarioProperties properties,
JobExplorer jobExplorer
) {
return (contribution, chunkContext) -> {
logger.info(String.format("%s is starting", properties.getStepName()));
if (properties.getPauseInSeconds() > 0) {
logger.info(String.format("%s is pausing for %d seconds", properties.getStepName(), properties.getPauseInSeconds()));
Thread.sleep(properties.getPauseInSeconds() * 1000);
}
logger.info(String.format("%s is completing", properties.getStepName()));
if (jobExecutionCount(properties, jobExplorer) == 1 && properties.isFailBatch()) {
throw new ExpectedException("Exception thrown during Batch Execution");
}
return RepeatStatus.FINISHED;
};
}
@Bean
public Step jobStep(
ScenarioProperties properties,
JobRepository jobRepository,
JobExplorer jobExplorer,
@Qualifier("springCloudTaskTransactionManager") PlatformTransactionManager platformTransactionManager
) {
return new StepBuilder(properties.getStepName(), jobRepository)
.allowStartIfComplete(true)
.tasklet(getTaskLet(properties, jobExplorer), platformTransactionManager)
.build();
}
@Bean
public Job job(ScenarioProperties properties, JobRepository jobRepository, Step jobStep) {
logger.info("properties=:" + properties);
SimpleJobBuilder jobBuilder = new JobBuilder(properties.getJobName(), jobRepository).start(jobStep);
if (properties.isIncludeRunidIncrementer()) {
jobBuilder.incrementer(new RunIdIncrementer());
}
return jobBuilder.build();
}
/**
* Override default transaction isolation level 'ISOLATION_REPEATABLE_READ' which Oracle does not
* support.
*/
@Configuration
@ConditionalOnProperty(value = "spring.datasource.driver", havingValue = "oracle.jdbc.OracleDriver")
@EnableBatchProcessing
static class OracleBatchConfig {
@Bean
JobRepository jobRepositoryFactoryBean(DataSource dataSource) {
JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
factoryBean.setDatabaseType("ORACLE");
factoryBean.setDataSource(dataSource);
factoryBean.setTransactionManager(platformTransactionManager(dataSource));
factoryBean.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
try {
return factoryBean.getObject();
} catch (Exception e) {
throw new BeanCreationException(e.getMessage(), e);
}
}
@Bean
PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
}
/**
* Displays simple log message. If user specifies {@code io.spring.fail-task=true} a {@link ExpectedException} is thrown.
*
* @return ApplicationRunner instance for the app.
*/
@Bean
public ApplicationRunner applicationRunner(ScenarioProperties properties) {
return args -> {
logger.info("ApplicationRunner Executing for ScenarioTaskApplication");
if (!properties.isLaunchBatchJob() && properties.getPauseInSeconds() > 0) {
logger.info(String.format("Task is pausing for %d seconds", properties.getPauseInSeconds()));
Thread.sleep(properties.getPauseInSeconds() * 1000);
}
if (properties.isFailTask()) {
throw new ExpectedException("Exception thrown during Task Execution");
}
};
}
}

View File

@@ -0,0 +1 @@
configuration-properties.classes=io.spring.scenariotask.configuration.ScenarioProperties

View File

@@ -0,0 +1,2 @@
logging.level.org.springframework.cloud.task=debug

View File

@@ -0,0 +1,229 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.scenariotask;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.spring.scenariotask.configuration.ExpectedException;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.batch.core.DefaultJobKeyGenerator;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.repository.dao.DefaultExecutionContextSerializer;
import org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.task.listener.TaskException;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Testcontainers
public class ScenarioTaskApplicationTests {
private static DataSource dataSource;
private static JobExplorer jobExplorer;
private static TaskExplorer taskExplorer;
@Container
private static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
.withDatabaseName("integration-tests-db")
.withUsername("sa")
.withPassword("sa");
@BeforeAll
public static void initializeDB() throws Exception {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl(postgreSQLContainer.getJdbcUrl());
driverManagerDataSource.setUsername(postgreSQLContainer.getUsername());
driverManagerDataSource.setPassword(postgreSQLContainer.getPassword());
dataSource = driverManagerDataSource;
jobExplorer = jobExplorer();
taskExplorer = taskExplorer();
}
private static JobExplorer jobExplorer() throws Exception {
JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setSerializer(new DefaultExecutionContextSerializer());
factoryBean.setJdbcOperations(new JdbcTemplate(dataSource));
factoryBean.setJobKeyGenerator(new DefaultJobKeyGenerator());
factoryBean.setConversionService(new DefaultConversionService());
return factoryBean.getObject();
}
private static TaskExplorer taskExplorer() {
TaskExecutionDaoFactoryBean taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean(dataSource);
return new SimpleTaskExplorer(taskExecutionDaoFactoryBean);
}
@Test
void testSuccessfulTask() {
String taskName = "taskSuccessfulTask";
List<String> args = getTaskLaunchArgs(taskName);
SpringApplication.run(ScenarioTaskApplication.class,
args.toArray(new String[0]));
List<TaskExecution> taskExecutions = getTaskExecutions(taskName);
assertThat(taskExecutions.size()).isEqualTo(1);
assertThat(taskExecutions.get(0).getExitCode()).isEqualTo(0);
}
@Test
void testFailTask() {
String taskName = "testFailTask";
List<String> args = getTaskLaunchArgs(taskName);
args.add("--io.spring.fail-task=true");
launchAppVerifyTaskFails(args, IllegalStateException.class);
List<TaskExecution> taskExecutions = getTaskExecutions(taskName);
assertThat(taskExecutions.size()).isEqualTo(1);
assertThat(taskExecutions.get(0).getExitCode()).isEqualTo(1);
}
@Test
void testSuccessTaskWithFailedBatchAndRestart() {
final String jobName = "testSuccessTaskWithFailedBatchAndRestart";
SpringApplication.run(ScenarioTaskApplication.class,
getFailBatchArgs(jobName).toArray(new String[0])
);
List<JobExecution> jobExecutions = getJobExecutionsForLastJobInstance(jobName);
assertThat(jobExecutions.size()).isEqualTo(1);
assertThat(jobExecutions.get(0).getExitStatus().getExitCode()).isEqualTo(ExitStatus.FAILED.getExitCode());
assertThat(jobExecutions.get(0).getExitStatus().getExitDescription()).startsWith("io.spring.scenariotask.configuration.ExpectedException");
SpringApplication.run(ScenarioTaskApplication.class,
getFailBatchArgs(jobName).toArray(new String[0]));
jobExecutions = getJobExecutionsForLastJobInstance(jobName);
assertThat(jobExecutions.size()).isEqualTo(2);
assertThat(jobExecutions.get(0).getExitStatus().getExitCode()).isEqualTo(ExitStatus.COMPLETED.getExitCode());
}
@Test
void testSuccessTaskSuccessBatchAndRestartFailure() throws Exception{
final String jobName = "testSuccessTaskSuccessBatchAndRestartFailure";
List<String> args = getSuccessBatchArgs(jobName);
args.add("--io.spring.include-runid-incrementer=true");
SpringApplication.run(ScenarioTaskApplication.class, args.toArray(new String[0]));
List<JobExecution> jobExecutions = getJobExecutionsForLastJobInstance(jobName);
assertThat(jobExecutions.size()).isEqualTo(1);
assertThat(jobExecutions.get(0).getExitStatus().getExitCode()).isEqualTo(ExitStatus.COMPLETED.getExitCode());
SpringApplication.run(ScenarioTaskApplication.class,
args.toArray(new String[0]));
assertThat(jobExplorer.getJobInstanceCount(jobName)).isEqualTo(2);
}
@Test
void testFailTaskWithFailedBatchAndRestart() {
final String jobName = "testFailTaskWithFailedBatchAndRestart";
final String taskName = "testFailTaskWithFailedBatchAndRestartTask";
List<String> args = getFailBatchArgs(jobName);
args.add("--spring.application.name=" + taskName);
args.add("--spring.cloud.task.batch.fail-on-job-failure=true");
launchAppVerifyTaskFails(args, TaskException.class);
List<JobExecution> jobExecutions = getJobExecutionsForLastJobInstance(jobName);
assertThat(jobExecutions.size()).isEqualTo(1);
assertThat(jobExecutions.get(0).getExitStatus().getExitCode()).isEqualTo(ExitStatus.FAILED.getExitCode());
assertThat(jobExecutions.get(0).getExitStatus().getExitDescription()).startsWith("io.spring.scenariotask.configuration.ExpectedException");
List<TaskExecution> taskExecutions = getTaskExecutions(taskName);
assertThat(taskExecutions.size()).isEqualTo(1);
assertThat(taskExecutions.get(0).getExitCode()).isEqualTo(1);
SpringApplication.run(ScenarioTaskApplication.class,
args.toArray(new String[0]));
jobExecutions = getJobExecutionsForLastJobInstance(jobName);
assertThat(jobExecutions.size()).isEqualTo(2);
assertThat(jobExecutions.get(0).getExitStatus().getExitCode()).isEqualTo(ExitStatus.COMPLETED.getExitCode());
}
@Test
void testSuccessfulTaskBatch() {
final String jobName = "testSuccessfulTaskBatch";
SpringApplication.run(ScenarioTaskApplication.class,
getSuccessBatchArgs(jobName).toArray(new String[0]));
List<JobExecution> jobExecutions = getJobExecutionsForLastJobInstance(jobName);
assertThat(jobExecutions.size()).isEqualTo(1);
assertThat(jobExecutions.get(0).getExitStatus().getExitCode()).isEqualTo(ExitStatus.COMPLETED.getExitCode());
}
private void launchAppVerifyTaskFails(List<String> args, Class clazz) {
assertThatThrownBy(() -> {
SpringApplication.run(ScenarioTaskApplication.class, args.toArray(new String[0]));
}).isInstanceOf(clazz);
}
private List<JobExecution> getJobExecutionsForLastJobInstance(String jobName) {
long instanceId = jobExplorer.getLastJobInstance(jobName).getInstanceId();
return jobExplorer.getJobExecutions(jobExplorer.getJobInstance(instanceId));
}
private List<String> getTaskLaunchArgs(String taskName) {
List<String> args = new ArrayList<>(getDatabaseArgs());
args.add("--io.spring.launchBatchJob=false");
args.add("--spring.application.name=" + taskName);
return args;
}
private List<String> getDatabaseArgs() {
List<String> args = Arrays.asList(
"--spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"--spring.datasource.driverClassName=" + "org.postgresql.Driver",
"--spring.datasource.username=" + postgreSQLContainer.getUsername(),
"--spring.datasource.password=" + postgreSQLContainer.getPassword());
return args;
}
private List<String> getSuccessBatchArgs(String jobName) {
List<String> args = new ArrayList<>(getDatabaseArgs());
args.add("--io.spring.job-name=" + jobName);
args.add("--spring.batch.jdbc.initialize-schema=always");
return args;
}
private List<String> getFailBatchArgs(String jobName) {
List<String> args = new ArrayList<>(getSuccessBatchArgs(jobName));
args.add("--io.spring.fail-batch=true");
return args;
}
private List<TaskExecution> getTaskExecutions(String taskName) {
Page<TaskExecution> taskExecutionPage = taskExplorer.findTaskExecutionsByName(taskName, PageRequest.of(0, 5));
return taskExecutionPage.getContent();
}
}

View File

@@ -12,7 +12,7 @@ Allows user to test various success and failures scenarios when task-batch apps
### Docker Build
```
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=spring/scenario-task:latest
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=spring/scenario-task:2.0.0
```
## Launching the app
@@ -24,7 +24,7 @@ java -jar scenario-task-2.0.0-SNAPSHOT.jar
### Docker
```
docker run spring/scenario-task:latest
docker run spring/scenario-task:2.0.0
```