Merge pull request #8142 from smokeyrobot/bael-3391
Bael-3391 - Remote Debugger
This commit is contained in:
commit
225c7367bc
|
@ -0,0 +1,15 @@
|
||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Remote Debugger" type="Remote">
|
||||||
|
<option name="USE_SOCKET_TRANSPORT" value="true" />
|
||||||
|
<option name="SERVER_MODE" value="false" />
|
||||||
|
<option name="SHMEM_ADDRESS" />
|
||||||
|
<option name="HOST" value="localhost" />
|
||||||
|
<option name="PORT" value="5005" />
|
||||||
|
<option name="AUTO_RESTART" value="false" />
|
||||||
|
<RunnerSettings RunnerId="Debug">
|
||||||
|
<option name="DEBUG_PORT" value="5005" />
|
||||||
|
<option name="LOCAL" value="false" />
|
||||||
|
</RunnerSettings>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
|
@ -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].
|
|
@ -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.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Except where otherwise noted, this work is licensed under https://creativecommons.org/licenses/by-nd/3.0/
|
|
@ -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[]
|
|
@ -0,0 +1,48 @@
|
||||||
|
<?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>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>gs-scheduling-tasks</artifactId>
|
||||||
|
<version>0.1.0</version>
|
||||||
|
<name>gs-scheduling-tasks</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.1.6.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</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.awaitility</groupId>
|
||||||
|
<artifactId>awaitility</artifactId>
|
||||||
|
<version>3.1.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue