SpringBoot入门八,添加定时任务

SpringBoot添加定时任务非常简单,只需要两步即可

1. SpringBoot启动类 添加@EnableScheduling注解,开启定时任务的配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * <h5>描述:springboot启动类</h5>
 */
@SpringBootApplication
@EnableScheduling   // 开启定时任务的配置
public class DemoApp {
    public static void main(String[] args) {
        // 整个程序入口,启动springboot项目
        SpringApplication.run(DemoApp.class, args);
    }
}

2. 编写定时任务类

注意在类上不要少了注解,要执行的方法上也不能少了注解,@Scheduled的使用方法请找度娘

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TaskTest {
    //输出时间格式
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    // 第一次执行前延时5秒启动,每次任务结束后15秒再次启动
    @Scheduled(initialDelay = 5000, fixedDelay = 15000)
    private void sayHello() {
        System.out.println(format.format(new Date())+"向宇宙发出了一声问候:Hello World!");
    }
}

效果

原文地址:http://blog.51cto.com/1197822/2295894

时间: 2024-07-30 11:44:55

SpringBoot入门八,添加定时任务的相关文章

SpringBoot入门二,添加JdbcTemplate数据源

项目基本配置参考上一篇文章SpringBoot入门一,使用myEclipse新建一个SpringBoot项目即可.现在来给项目添加一个JdbcTemplate数据源,添加方式非常简单,仅需两步即可,具体内容如下: 1. pom.xml添加以下配置信息 <!-- jdbcTemplate依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring

SpringBoot入门三,添加log4j2支持

项目基本配置参考文章SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可.现在来给项目添加一个log4j2支持,添加方式非常简单,仅需两步即可,具体内容如下: 1. pom.xml将原有的 logback 移除 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot

SpringBoot入门四,添加MyBatis支持

项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可.现在来给项目添加一个MyBatis支持,添加方式非常简单,仅需两步即可,具体内容如下: 1. pom.xml添加以下配置信息 数据源采用最新的hikari,据说性能相当牛X,想了解的可以去百度一下 <!-- 引入默认连接池,SpringBoot2.x采用hikari连接池 --> <dependency> <group

SpringBoot入门九,添加shiro支持

项目基本配置参考SpringBoot入门一,使用myEclipse新建一个SpringBoot项目,使用myEclipse新建一个SpringBoot项目即可.现在来给项目添加shiro支持,数据暂时硬编码,不连接数据库,具体内容如下: 1. pom.xml添加以下配置信息 <!-- 开启shiro依赖 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-s

SpringBoot入门十三,添加RabbitMq

一. 概念说明 Broker:简单来说就是消息队列服务器实体.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列.Queue:消息队列载体,每个消息都会被投入到一个或多个队列.Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来.Routing Key:路由关键字,exchange根据这个关键字进行消息投递.vhost:虚拟主机,一个broker里可以开设多个vhost,用作不同用户的权限分离.producer:消息生产者,就是投递消息的程序.co

SpringBoot入门六,添加ehcache缓存

1.pom.xml文件添加引用包 <!-- 开启cache缓存支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <!-- 引入ehcache支持 --> <dependency> <gro

SpringBoot入门十,添加junit单元测试

SpringBoot使用junit非常简单,我们来看一下,首先说明,这里使用的是springboot2.0.4的版本 一.pom.xml文件开启springboot测试包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>

SpringBoot入门 (十二) 定时任务

本文记录在SpringBoot中使用定时任务. 在我们的项目中,经常需要用到定时任务去帮我们做一些事情,比如服务状态监控,业务数据状态的更改等,SpringBoot中实现定时任务有2中方案,一种是自带的,我们只需要加上注解即可:另一种是集成Quartz来实现定时任务. 一 SpringBoot 提供的定时任务 在SpringBoot的starter包中已经提供了对定时任务的支持,我们很容易实现定时任务.修改pom.xml文件,加入如下内容,引入依赖: <dependency> <grou

SpringBoot入门七,添加默认静态首页

目录resources/static下创建index.html文件即可(路径与文件名称均不可改变) 原文地址:http://blog.51cto.com/1197822/2288018