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.执行结果图

  • 相关阅读:
    Tomcat vs Jetty vs Undertow性能对比
    实例对象( instance)、类对象(class)、元类对象(meta-class)的内部结构分析
    isa和superclass
    iOS-weak关键字使用场景
    iOS-weak和assign区别,copy和strong的区别和应用
    iOS-class修饰符的解释及用法
    iOS-atomic修饰符原理剖析讲解 (你将会了解到什么是优先级翻转、自旋锁、互斥锁)
    @property修饰符种类
    @property、@synthesize 、@dynamic的应用
    【原创】Kafka Consumer多线程消费
  • 原文地址:https://www.cnblogs.com/curedfisher/p/11933067.html
Copyright © 2011-2022 走看看