gpt4 book ai didi

java - Spring没有运行我的@Scheduled(cron = "0,30 * * * * *")

转载 作者:行者123 更新时间:2023-12-01 09:20:02 25 4
gpt4 key购买 nike

  • 以下是代码
  • 我现在使用 spring.boot.version 1.4.1.RELEASE
  • 启动服务器时没有打印任何内容
  • 我正在使用 @Scheduled 注释来运行 cron 作业,但它从未启动
  • 如果我创建新项目并使用以下类,相同的代码可以正常工作
  • 请提出可能出现什么问题的建议?

                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(">>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
    }
    }

最佳答案

你的代码示例对我来说看起来完全没问题。此外,我使用您提供的示例代码创建了一个项目,并且它可以工作(使用 spring.boot.version 1.2.1.RELEASE )。

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

关于java - Spring没有运行我的@Scheduled(cron = "0,30 * * * * *"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40225023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com