-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCT-23 Added sample apps for Spring Cloud Task
* Timestamp Demonstrates a Spring boot/task application as a single java file. * HelloWorld Demonstrates a Spring boot/task application using java configuration. resolves #23
- Loading branch information
Showing
7 changed files
with
349 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
= Timestamp Job | ||
|
||
This is a Spring Cloud Task application that logs a timestamp. | ||
|
||
== Requirements: | ||
|
||
* Java 7 or Above | ||
|
||
== Classes: | ||
|
||
* TaskApplication - the Spring Boot Main Application | ||
* TimestampTask - the module that writes the log entry as Spring Task | ||
|
||
== Build: | ||
|
||
[source,shell,indent=2] | ||
---- | ||
$ mvn clean package | ||
---- | ||
|
||
== Run: | ||
|
||
[source,shell,indent=2] | ||
---- | ||
$ java -jar target/timestamp-task-1.0.0.BUILD-SNAPSHOT.jar | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>timestamp-task</artifactId> | ||
<packaging>jar</packaging> | ||
<name>timestamp-task</name> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
<description>Spring Cloud Timestamp Task</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.3.0.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<start-class>org.springframework.cloud.task.timestamp.TaskApplication</start-class> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-task-core</artifactId> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hsqldb</groupId> | ||
<artifactId>hsqldb</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
67 changes: 67 additions & 0 deletions
67
...les/timestamp/src/main/java/org/springframework/cloud/task/timestamp/TaskApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2015 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 org.springframework.cloud.task.timestamp; | ||
|
||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.cloud.task.annotation.EnableTask; | ||
import org.springframework.cloud.task.annotation.Task; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* Spring Boot Application that has tasks enabled. | ||
*/ | ||
@SpringBootApplication | ||
@EnableTask | ||
@EnableConfigurationProperties({ TimestampTaskProperties.class }) | ||
public class TaskApplication { | ||
|
||
@Bean | ||
public TimestampTask timeStampTask() { | ||
return new TimestampTask(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(TaskApplication.class); | ||
} | ||
|
||
/** | ||
* A commandline runner that prints a timestamp and is annotated with @Task. | ||
*/ | ||
@Task("Demo Timestamp Task") | ||
public class TimestampTask implements CommandLineRunner { | ||
private final org.slf4j.Logger logger = LoggerFactory.getLogger(TimestampTask.class); | ||
|
||
@Autowired | ||
private TimestampTaskProperties config; | ||
|
||
@Override | ||
public void run(String... strings) throws Exception { | ||
DateFormat dateFormat = new SimpleDateFormat(config.getFormat()); | ||
logger.info(dateFormat.format(new Date())); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...stamp/src/main/java/org/springframework/cloud/task/timestamp/TimestampTaskProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2015 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 org.springframework.cloud.task.timestamp; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* @author Glenn Renfro | ||
*/ | ||
@ConfigurationProperties | ||
public class TimestampTaskProperties { | ||
|
||
/** | ||
* The timestamp format, "yyyy-MM-dd HH:mm:ss.SSS" by default. | ||
*/ | ||
private String format = "yyyy-MM-dd HH:mm:ss.SSS"; | ||
|
||
public String getFormat() { | ||
Assert.hasText(format, "format must not be empty nor null"); | ||
return format; | ||
} | ||
|
||
public void setFormat(String format) { | ||
this.format = format; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...imestamp/src/test/java/org/springframework/cloud/task/timestamp/TaskApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2015 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 org.springframework.cloud.task.timestamp; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertTrue; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.test.OutputCapture; | ||
|
||
/** | ||
* Verifies that the Task Application outputs the correct task log entries. | ||
* | ||
* @author Glenn Renfro | ||
*/ | ||
public class TaskApplicationTests { | ||
|
||
@Rule | ||
public OutputCapture outputCapture = new OutputCapture(); | ||
|
||
@Test | ||
public void testTimeStampApp() throws Exception { | ||
final String TEST_DATE_DOTS = "......."; | ||
final String CREATE_TASK_MESSAGE = "Creating: TaskExecution{executionId='"; | ||
final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution{executionId='"; | ||
String[] args = { "--format=yyyy" + TEST_DATE_DOTS }; | ||
|
||
assertEquals(0, SpringApplication.exit(SpringApplication | ||
.run(TaskApplication.class, args))); | ||
|
||
String output = this.outputCapture.toString(); | ||
assertTrue("Unable to find the timestamp: " + output, | ||
output.contains(TEST_DATE_DOTS)); | ||
assertTrue("Test results do not show create task message: " + output, | ||
output.contains(CREATE_TASK_MESSAGE)); | ||
assertTrue("Test results do not show success message: " + output, | ||
output.contains(UPDATE_TASK_MESSAGE)); | ||
|
||
String taskTitle = "Demo Timestamp Task"; | ||
Pattern pattern = Pattern.compile(taskTitle); | ||
Matcher matcher = pattern.matcher(output); | ||
int count = 0; | ||
while (matcher.find()) { | ||
count++; | ||
} | ||
assertEquals("The number of task titles did not match expected: ", 2, count); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
.../src/test/java/org/springframework/cloud/task/timestamp/TimestampTaskPropertiesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2015 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 org.springframework.cloud.task.timestamp; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.boot.test.EnvironmentTestUtils; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Glenn Renfro | ||
*/ | ||
public class TimestampTaskPropertiesTests { | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testEmptyFormat() { | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); | ||
EnvironmentTestUtils.addEnvironment(context, "format:"); | ||
context.register(Conf.class); | ||
context.refresh(); | ||
TimestampTaskProperties properties = context.getBean(TimestampTaskProperties.class); | ||
properties.getFormat(); | ||
} | ||
|
||
@Test | ||
public void testFormatDefault() { | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); | ||
context.register(Conf.class); | ||
context.refresh(); | ||
TimestampTaskProperties properties = context.getBean(TimestampTaskProperties.class); | ||
assertEquals("result does not match default format.", "yyyy-MM-dd HH:mm:ss.SSS", | ||
properties.getFormat()); | ||
} | ||
|
||
@Test | ||
public void testFormatSet() { | ||
final String FORMAT = "yyyy"; | ||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); | ||
context.register(Conf.class); | ||
context.refresh(); | ||
TimestampTaskProperties properties = context.getBean(TimestampTaskProperties.class); | ||
properties.setFormat(FORMAT); | ||
assertEquals("result does not match established format.", FORMAT, | ||
properties.getFormat()); | ||
} | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(TimestampTaskProperties.class) | ||
static class Conf { | ||
} | ||
} |