zoukankan      html  css  js  c++  java
  • scheduled定时任务+实例请求数据库

    1.scheduled定时任务类:ScheduledDemo.java

    package com.nantian.scheduled;

    import java.util.Date;

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

    /**
    * scheduled定时任务
    *@author xjj13
    *@component (把普通pojo实例化到spring容器中,相当于配置文件中的
    *<bean id="" class=""/>)
    *泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
    */
    @Component
    public class ScheduledDemo {
    /**
    * 定时任务方法
    * @Scheduled:设置定时任务
    * 方法上添加该注解,表示方法会以定时任务的方式出现,一旦时间到达后,该方法就会被触发
    * cron:(设置触发时间)cron表达式,定时任务触发是时间的一个字符表达式
    */
    @Scheduled(cron="0/2 * * * * ?")//每两秒触发一次方法
    public void scheduledMethod() {
    System.out.println("定时器被触发"+new Date());
    }
    }
    2.在启动类中开启定时任务的启用

    springBoot中默认是对scheduled是不开启的

    @SpringBootApplication
    @EnableScheduling//开启scheduled定时任务
    public class Application {
    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }

    }

    ================================================================================

    实例请求数据库

     1.Application.java

    package com.nantian;

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

    }

    2.

    package com.nantian.scheduled;

    import java.util.Date;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    import com.nantian.dao.UserDao;
    import com.nantian.pojo.User;

    /**
    * scheduled定时任务
    *@author xjj13
    *@component (把普通pojo实例化到spring容器中,相当于配置文件中的
    *<bean id="" class=""/>)
    *泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类
    */
    @Component
    public class ScheduledDemo {
    @Autowired
    private UserDao userDao;
    /**
    * 定时任务方法
    * @Scheduled:设置定时任务
    * 方法上添加该注解,表示方法会以定时任务的方式出现,一旦时间到达后,该方法就会被触发
    * cron:(设置触发时间)cron表达式,定时任务触发是时间的一个字符表达式
    */
    @Scheduled(cron="0/1 * * * * ?")//每两秒触发一次方法
    public void scheduledMethod() {
    List<User> users=(List<User>) userDao.findAllUsers();
    System.out.println(users);
    System.out.println("定时器被触发"+new Date());
    }

    }

    3.执行结果图

  • 相关阅读:
    ngx_lua_waf完整安装说明
    Linux(CentOS)下的JDK的安装和环境配置
    Genymotion的2个问题及解决方法
    Appscan的第一个测试请求就是提交MAC地址
    oracle相关知识
    数据结构之树
    kafka的写入内存?硬盘
    算法的时间复杂度和空间复杂度
    Java线程池
    mapReduce和spark的shuffle
  • 原文地址:https://www.cnblogs.com/curedfisher/p/11933067.html
Copyright © 2011-2022 走看看