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 spring-cloud/spring-cloud-task#23
This commit is contained in:
committed by
Michael Minella
parent
ccd3a553da
commit
7be3a88826
@@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user