2017-12-18 243 views
0

我正在瀏覽彈簧調度程序指南(https://spring.io/guides/gs/scheduling-tasks/),但我的彈簧引導應用程序在啓動後立即終止。我檢查了所有的依賴關係,他們似乎很好。我的輸出是彈簧引導應用程序在啓動後立即關閉(schedular)

"C:\Program Files\Java\jdk1.8.0_111\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:60501,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;C:\my-data\code\spring-guides\spring-schedular\target\classes;C:\Users\ashis\.m2\repository\org\springframework\boot\spring-boot-starter\1.5.9.RELEASE\spring-boot-starter-1.5.9.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\boot\spring-boot\1.5.9.RELEASE\spring-boot-1.5.9.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\spring-context\4.3.13.RELEASE\spring-context-4.3.13.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\spring-aop\4.3.13.RELEASE\spring-aop-4.3.13.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\spring-beans\4.3.13.RELEASE\spring-beans-4.3.13.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\spring-expression\4.3.13.RELEASE\spring-expression-4.3.13.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.9.RELEASE\spring-boot-autoconfigure-1.5.9.RELEASE.jar;C:\Users\ashis\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.5.9.RELEASE\spring-boot-starter-logging-1.5.9.RELEASE.jar;C:\Users\ashis\.m2\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;C:\Users\ashis\.m2\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;C:\Users\ashis\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;C:\Users\ashis\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\ashis\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;C:\Users\ashis\.m2\repository\org\springframework\spring-core\4.3.13.RELEASE\spring-core-4.3.13.RELEASE.jar;C:\Users\ashis\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;C:\Users\ashis\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\ashis\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\172.3544.35\lib\idea_rt.jar" com.ashish.springschedular.SpringSchedularApplication 
Connected to the target VM, address: '127.0.0.1:60501', transport: 'socket' 

    . ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.9.RELEASE) 

2017-12-18 06:24:30.882 INFO 11836 --- [   main] c.a.s.SpringSchedularApplication   : Starting SpringSchedularApplication on DESKTOP-SP4170I with PID 11836 (C:\my-data\code\spring-guides\spring-schedular\target\classes started by ashish in C:\my-data\code\spring-guides\spring-schedular) 
2017-12-18 06:24:30.882 INFO 11836 --- [   main] c.a.s.SpringSchedularApplication   : No active profile set, falling back to default profiles: default 
2017-12-18 06:24:31.053 INFO 11836 --- [   main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]1d8bd0de: startup date [Mon Dec 18 06:24:31 EST 2017]; root of context hierarchy 
2017-12-18 06:24:33.115 INFO 11836 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
Disconnected from the target VM, address: '127.0.0.1:60501', transport: 'socket' 
2017-12-18 06:24:33.161 INFO 11836 --- [   main] c.a.s.SpringSchedularApplication   : Started SpringSchedularApplication in 2.997 seconds (JVM running for 4.005) 
2017-12-18 06:24:33.161 INFO 11836 --- [  Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]1d8bd0de: startup date [Mon Dec 18 06:24:31 EST 2017]; root of context hierarchy 
2017-12-18 06:24:33.161 INFO 11836 --- [  Thread-2] o.s.j.e.a.AnnotationMBeanExporter  : Unregistering JMX-exposed beans on shutdown 

Process finished with exit code 0 

我主要的應用程序代碼:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class SpringSchedularApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(SpringSchedularApplication.class, args); 
    } 
} 

我的雙響炮:

<?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>com.ashish</groupId> 
    <artifactId>spring-schedular</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>spring-schedular</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.9.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <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> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

    <repositories> 
     <repository> 
      <id>spring-snapshots</id> 
      <name>Spring Snapshots</name> 
      <url>https://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
     <repository> 
      <id>spring-milestones</id> 
      <name>Spring Milestones</name> 
      <url>https://repo.spring.io/milestone</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 
    </repositories> 

    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-snapshots</id> 
      <name>Spring Snapshots</name> 
      <url>https://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </pluginRepository> 
     <pluginRepository> 
      <id>spring-milestones</id> 
      <name>Spring Milestones</name> 
      <url>https://repo.spring.io/milestone</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </pluginRepository> 
    </pluginRepositories> 


</project> 

任我調度文件:

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.scheduling.annotation.Scheduled; 
import org.springframework.stereotype.Component; 

import java.text.DateFormat; 
import java.util.Date; 

import static java.text.DateFormat.LONG; 
import static java.text.DateFormat.getDateInstance; 
import static java.util.Locale.US; 

@Component 
public class ScheduledTask { 
    private static final Logger log = LoggerFactory.getLogger(ScheduledTask.class); 
    private static final DateFormat dateFormat = getDateInstance(LONG, US); 

    @Scheduled(fixedRate = 5000) 
    public void readCurrentTime(){ 
     log.info("The time is now[]", dateFormat.format(new Date())); 
    } 
} 
+3

將'@ EnableScheduling'添加到您的應用程序類中。 –

+0

@ M.Deinum非常感謝。有效 – Ashish

回答

1

雖然春季啓動系統能夠大量的東西取決於你的依賴關係對於調度和異步不這樣做。您仍然需要分別通過添加@EnableScheduling@EnableAsync來手動啓用該功能。

在您的情況下,將@EnableScheduling添加到您的應用程序類就足夠了。

@SpringBootApplication 
@EnableScheduling 
public class SpringSchedularApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(SpringSchedularApplication.class, args); 
    } 
} 

有了這個調度將被啓用,因爲有一個調度線程仍在運行應用程序不會關閉。