为了账号安全,请及时绑定邮箱和手机立即绑定

SpringBoot项目实战(6):开启定时任务

标签:
Java

刚巧java群里有位同学问到springboot如何开启定时任务,这个我就记录下哈。

Spring Boot特点编辑

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标,健康检查和外部配置
  6. 绝对没有代码生成和对XML没有要求配置1

以上摘录自百度百科...

springboot让开发更简单!springmvc中启用定时任务还得需要在xml中进行配置启用并且要配置扫描器,但是springboot只需要一个注解就可以。

@EnableScheduling

无需多余的jar依赖,所以pom不贴了

applaction.java

/*
 * 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
 *
 *      http://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 com.sbm;

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, args);
    }
}

定时任务类AppCoreTask.java

/*
 * 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
 *
 *      http://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 com.sbm.scheduling;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
 * spring_boot_code
 * Created by yadong.zhang on com.sbm.scheduling
 * User:yadong.zhang
 * Date:2016/12/30 15:34
 */
@Component
public class AppCoreTask {

    @Scheduled(cron = "0 53 15 * * ? ")
    public void tesk() {
        System.out.print("开启定时任务" + new Date());
    }
}

要求在15:53分打印一句话

关于cron表达式的用法,可以在我的这篇文章里查看:Spring Task 中cron表达式整理记录

运行sb程序

016-12-30 15:52:24.653  INFO 4204 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-30 15:52:24.654  INFO 4204 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-30 15:52:24.722  INFO 4204 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-30 15:52:25.183  INFO 4204 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-12-30 15:52:25.212  INFO 4204 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
2016-12-30 15:52:25.315  INFO 4204 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-12-30 15:52:25.326  INFO 4204 --- [           main] com.sbm.Application                      : Started Application in 7.124 seconds (JVM running for 8.234)
开启定时任务Fri Dec 30 15:53:00 CST 2016

我可以对一个人无限的好,前提是值得。 ——慕冬雪

Github源码 - SpringBoot多模块项目

点击查看更多内容
29人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
9133
获赞与收藏
5502

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消