2016-10-24 204 views
0
  • 以下是代码
  • 我使用spring.boot.version 1.4.1.RELEASE现在
  • 没有当我启动服务器
  • 我使用@Scheduled注释运行cron作业,但它永远不会发生
  • 相同的代码工作正常,如果我创建新的项目和使用下列类
  • 请建议什么都不可能被打印出错 ?春天没有运行我的@Scheduled(的cron = “0,30 * * * * *”)

      package com.equilar.bsp; 
          import java.util.TimeZone; 
          import javax.annotation.PreDestroy; 
          import org.springframework.beans.factory.annotation.Value; 
          import org.springframework.boot.SpringApplication; 
          import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
          import org.springframework.boot.autoconfigure.SpringBootApplication; 
          import org.springframework.boot.builder.SpringApplicationBuilder; 
          import org.springframework.boot.context.web.SpringBootServletInitializer; 
          import org.springframework.boot.orm.jpa.EntityScan; 
          import org.springframework.context.annotation.Bean; 
          import org.springframework.context.annotation.ComponentScan; 
          import org.springframework.context.annotation.Configuration; 
          import org.springframework.data.jpa.repository.config.EnableJpaAuditing; 
          import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
          import org.springframework.scheduling.annotation.EnableScheduling; 
    
          import com.amazonaws.http.IdleConnectionReaper; 
          import com.cloudinary.Cloudinary; 
          import com.equilar.bsp.config.RedisConfig; 
          import com.equilar.bsp.config.SecurityConfig; 
          import com.equilar.bsp.mvc.MvcConfig; 
          import com.equilar.bsp.util.JwtTokenGenerator; 
          import com.equilar.bsp.util.Util; 
    
    
          @SpringBootApplication 
          @EnableScheduling 
          @Configuration 
          @EnableAutoConfiguration 
          @EnableJpaAuditing 
          //@ComponentScan(basePackages = "com.equilar" ,lazyInit = true) 
          @EnableJpaRepositories("com.equilar") 
          @EntityScan({"com.equilar.bsp.domain", "com.equilar.newcommon.folder.domain", "com.equilar.newcommon.pdf.domain"}) 
    
    
    
    
          public class Application extends SpringBootServletInitializer { 
    
           public static void main(String[] args) { 
            // set default timezone first thing!! 
            TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 
            SpringApplication.run(Application.class, args); 
            JwtTokenGenerator.getStartTime(); 
           } 
    
           @Override 
           protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
            return application.sources(applicationClass, SecurityConfig.class, MvcConfig.class, RedisConfig.class); 
           } 
    
           private static Class<Application> applicationClass = Application.class; 
    
           @PreDestroy 
           private void cleanUp() { 
            /* try { 
             // Shutting down AWS IdleConnectionReaper thread... 
             IdleConnectionReaper.shutdown(); 
             List<Thread> threadsList = getThreadByName("logback-loggly-appender"); 
             if(!Util.isNullOrEmptyCollection(threadsList)){ 
              for (Thread thread : threadsList) { 
               thread.interrupt(); 
              } 
             } 
    
            } catch (Throwable t) { 
             // log error 
             t.printStackTrace(); 
            }*/ 
           } 
    
           /*public List<Thread> getThreadByName(String threadName) { 
            List<Thread> threads = new ArrayList<Thread>(); 
            for (Thread t : Thread.getAllStackTraces().keySet()) { 
             if (t.getName().equals(threadName)){ 
              threads.add(t); 
             } 
            } 
            return threads; 
           }*/ 
    
           @Value("${CLOUDINARY_URL}") 
           private String cloudinaryUrl; 
    
           @Bean(name = "cloudinary") 
           public Cloudinary Instance() { 
            return new Cloudinary(cloudinaryUrl); 
           } 
          } 
    
    
    
    
         package com.equilar.bsp.calc; 
    
    
         import org.slf4j.Logger; 
         import org.slf4j.LoggerFactory; 
         import org.springframework.scheduling.annotation.Scheduled; 
    
         @org.springframework.stereotype.Component 
         public class Component { 
    
          private Logger logger = LoggerFactory.getLogger(this.getClass()); 
    
    
          @Scheduled(
            cron = "0,30 * * * * *") 
          public void cronJob() { 
           logger.info("> cronJob"); 
    
    
           logger.info("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>> In Chron Job." 
            ); 
    
           logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> "); 
          } 
         }       
    
+0

当您设置在'reportCurrentTime'方法中设置断点到达该断点,当你启动应用程序? – Josef

+0

不,它永远不会进入reportCurrentTime方法 – vk1

+0

你是如何运行应用程序的? – kuhajeyan

回答

1

您的代码示例看起来完全对我很好。此外,我已经创建了一个项目,其中包含您提供的示例代码,并且它可以工作(使用spring.boot.version 1.2.1.RELEASE)。

有上github一个类似的项目,你可能会感兴趣。

+0

我正在运行它作为春季启动应用程序,它现在工作。我刚刚重新启动我的STS.But相同的代码不起作用,当我在我现有的应用程序中使用它。我更新了我的代码。 – vk1