diff --git a/intelliJ/remote-debugging/CONTRIBUTING.adoc b/intelliJ/remote-debugging/CONTRIBUTING.adoc new file mode 100644 index 0000000000..a97e428a48 --- /dev/null +++ b/intelliJ/remote-debugging/CONTRIBUTING.adoc @@ -0,0 +1,2 @@ +If you have not previously done so, please fill out and +submit the https://cla.pivotal.io/sign/spring[Contributor License Agreement]. \ No newline at end of file diff --git a/intelliJ/remote-debugging/LICENSE.code.txt b/intelliJ/remote-debugging/LICENSE.code.txt new file mode 100644 index 0000000000..4b5cde9fd2 --- /dev/null +++ b/intelliJ/remote-debugging/LICENSE.code.txt @@ -0,0 +1,16 @@ + All code in this repository is: + ======================================================================= + Copyright (c) 2013 GoPivotal, Inc. All Rights Reserved + + 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. + diff --git a/intelliJ/remote-debugging/LICENSE.writing.txt b/intelliJ/remote-debugging/LICENSE.writing.txt new file mode 100644 index 0000000000..9d21229f84 --- /dev/null +++ b/intelliJ/remote-debugging/LICENSE.writing.txt @@ -0,0 +1 @@ +Except where otherwise noted, this work is licensed under https://creativecommons.org/licenses/by-nd/3.0/ diff --git a/intelliJ/remote-debugging/README.adoc b/intelliJ/remote-debugging/README.adoc new file mode 100644 index 0000000000..6a4b80959f --- /dev/null +++ b/intelliJ/remote-debugging/README.adoc @@ -0,0 +1,84 @@ +:toc: +:spring_version: current +:icons: font +:source-highlighter: prettify +:project_id: gs-scheduling-tasks +This guide walks you through the steps for scheduling tasks with Spring. + +== What you'll build + +You'll build an application that prints out the current time every five seconds using Spring's `@Scheduled` annotation. + +== What you'll need + +:java_version: 1.8 +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[] + + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[] + + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-gradle.adoc[] + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-maven.adoc[] + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[] + + + +[[initial]] +== Create a scheduled task +Now that you've set up your project, you can create a scheduled task. + +`src/main/java/hello/ScheduledTasks.java` +[source,java] +---- +include::complete/src/main/java/hello/ScheduledTasks.java[] +---- + +The `Scheduled` annotation defines when a particular method runs. +NOTE: This example uses `fixedRate`, which specifies the interval between method invocations measured from the start time of each invocation. There are https://docs.spring.io/spring/docs/{spring_version}/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled[other options], like `fixedDelay`, which specifies the interval between invocations measured from the completion of the task. You can also https://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html[use `@Scheduled(cron=". . .")` expressions for more sophisticated task scheduling]. + +== Enable Scheduling + +Although scheduled tasks can be embedded in web apps and WAR files, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java `main()` method. + +`src/main/java/hello/Application.java` +[source,java] +---- +include::complete/src/main/java/hello/Application.java[] +---- + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[] + +https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#scheduling-enable-annotation-support[`@EnableScheduling`] ensures that a background task executor is created. Without it, nothing gets scheduled. + + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[] + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[] + + + +Logging output is displayed and you can see from the logs that it is on a background thread. You should see your scheduled task fire every 5 seconds: + +.... +[...] +2016-08-25 13:10:00.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:00 +2016-08-25 13:10:05.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:05 +2016-08-25 13:10:10.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:10 +2016-08-25 13:10:15.143 INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:15 +.... + +== Summary + +Congratulations! You created an application with a scheduled task. Heck, the actual code was shorter than the build file! This technique works in any type of application. + +== See Also + +The following guides may also be helpful: + +* https://spring.io/guides/gs/spring-boot/[Building an Application with Spring Boot] +* https://spring.io/guides/gs/batch-processing/[Creating a Batch Service] + +include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[] diff --git a/intelliJ/remote-debugging/pom.xml b/intelliJ/remote-debugging/pom.xml new file mode 100644 index 0000000000..d18625e8f6 --- /dev/null +++ b/intelliJ/remote-debugging/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + com.baeldung + gs-scheduling-tasks + 0.1.0 + gs-scheduling-tasks + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.awaitility + awaitility + 3.1.2 + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/intelliJ/remote-debugging/src/main/java/hello/Application.java b/intelliJ/remote-debugging/src/main/java/hello/Application.java new file mode 100644 index 0000000000..73b55f933b --- /dev/null +++ b/intelliJ/remote-debugging/src/main/java/hello/Application.java @@ -0,0 +1,14 @@ +package hello; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableScheduling +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } +} diff --git a/intelliJ/remote-debugging/src/main/java/hello/ScheduledTasks.java b/intelliJ/remote-debugging/src/main/java/hello/ScheduledTasks.java new file mode 100644 index 0000000000..de1f6eebeb --- /dev/null +++ b/intelliJ/remote-debugging/src/main/java/hello/ScheduledTasks.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-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 + * + * 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. + */ + +package hello; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class ScheduledTasks { + + private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); + + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); + + @Scheduled(fixedRate = 5000) + public void reportCurrentTime() { + log.info("The time is now {}", dateFormat.format(new Date())); + } +}