spring4注释方式定时器的使用

这个可以


1.下载定时器生成工具,百度搜索:cron生成器下载

2.搭建maven war 项目

pom.xml 部分代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<properties>
         <spring4.version> 4.2 . 2 .RELEASE</spring4.version>
     </properties>
     
     <dependencies>
 
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-beans</artifactId>
             <version>${spring4.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>${spring4.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-core</artifactId>
             <version>${spring4.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${spring4.version}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>${spring4.version}</version>
         </dependency>
 
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version> 3.8 . 1 </version>
             <scope>test</scope>
         </dependency>
     </dependencies>

web.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <!-- Spring配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>
复制代码

applicationContext.xml

复制代码
 
  
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

     <bean id="firstCron" class="com.hhly.trend.lottery.config.DataCron"/>
     <task:scheduler id="qbScheduler" pool-size="10"/>
     <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
     <!--简单来说,我们只需要<task:annotation-driven/>这一句即可,这些参数不是必须的 -->
 </beans>

复制代码

测试类BaseJobs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package  springjobs.simplejob;
 
import  java.util.Date;
 
import  org.springframework.scheduling.annotation.Scheduled;
import  org.springframework.stereotype.Component;
@Component
public  class  BaseJobs {
 
     /**
      * 每5秒钟执行一次
      */
     @Scheduled (cron =  "*/5 * * * * ?" )
     public  void  doJob(){
         System.out.println( new  Date() +  "-----------------doJob" );
     }
     /**
      * 固定每分钟执行一次
      */
     @Scheduled (fixedRate =  60 * 1000 )
     public  void  doJob1(){
         System.out.println( new  Date() +  "-----------------doJob1" );
     }
     /**
      * 上次任务结束后一分钟后再次执行
      */
     @Scheduled (fixedDelay =  60 * 1000 )
     public  void  doJob2(){
         System.out.println( new  Date() +  "-----------------doJob2" );
     }
 
}

maven buid形成war包

部署到tomcat等容器中,查看日志,ok!


猜你喜欢

转载自blog.csdn.net/HUXU981598436/article/details/80068139
今日推荐