Discuss / Java / 写了@Scheduled但定时方法仍然不执行,一次也没有

写了@Scheduled但定时方法仍然不执行,一次也没有

Topic source

程序运行信息

11月 14, 2020 9:41:45 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
11月 14, 2020 9:41:45 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Tomcat]
11月 14, 2020 9:41:45 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet engine: [Apache Tomcat/9.0.26]
11月 14, 2020 9:41:46 下午 org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
信息: No global web.xml found
11月 14, 2020 9:41:49 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
11月 14, 2020 9:41:49 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11月 14, 2020 9:41:49 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
11月 14, 2020 9:41:49 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization started
11月 14, 2020 9:41:49 下午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext initialized in 298 ms
11月 14, 2020 9:41:50 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring DispatcherServlet 'dispatcher'
11月 14, 2020 9:41:50 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: Initializing Servlet 'dispatcher'
11月 14, 2020 9:41:50 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
信息: Completed initialization in 397 ms
11月 14, 2020 9:41:50 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]

Process finished with exit code -1

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

@Component
public class TaskService {

    final Logger logger = LoggerFactory.getLogger(getClass());

    @Scheduled(initialDelay = 6_000, fixedRate = 6_000)
    public void checkSystemStatusEveryMinute() {
        logger.info("Start check system status...");
        if (Math.random() > 0.8) {
            throw new RuntimeException("check system status task failed.");
        }
    }

    @Scheduled(initialDelay = 3_000, fixedDelayString = "${task.checkDiskSpace:3000}")
    public void checkDiskSpaceEveryMinute() {
        logger.info("Start check disk space...");
    }

    @Scheduled(cron = "${task.report:0 15 2 * * *}")
    public void cronDailyReport() {
        logger.info("Start daily report task...");
    }

    @Scheduled(cron = "${task.weekday:0 0 12 * * MON-FRI}")
    public void cronWeekdayTask() {
        logger.info("Start weekday task...");
    }
}
import org.apache.catalina.Context;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.io.File;

@Configuration
@ComponentScan
@EnableWebMvc
@EnableScheduling
@EnableTransactionManagement
public class AppConfig {

    public static void main(String[] args) throws Exception {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.getConnector();
        Context context = tomcat.addWebapp("", new File("web").getAbsolutePath());
        WebResourceRoot resourceRoot = new StandardRoot(context);
        resourceRoot.addPreResources(
                new DirResourceSet(resourceRoot, "/WEB-INF/classes", new File("target/classes").getAbsolutePath(), "/")
        );
        context.setResources(resourceRoot);
        tomcat.start();
        tomcat.getServer().await();
    }
}

我只写了这2个文件,运行发现定时任务并不会执行

问题解决了,这是因为项目没有在web.xml打开注解配置spring的支持


  • 1

Reply