• Spring的简单定时任务的实现


    搭建最简单的Spring定时任务工程:

    1.把Spring通过web.xml注册进来:

    1 <!-- 加载spring容器 -->
    2     <context-param>
    3         <param-name>contextConfigLocation</param-name>
    4         <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
    5     </context-param>
    6     <listener>
    7         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    8     </listener>

    2.需要告诉Spring去哪儿扫描组件,在此我使用的是注解的方式,所以要告诉Spring我们是使用注解方式注册任务的,我的配置文件是applicationContext-service.xml

     1 <beans xmlns="http://www.springframework.org/schema/beans"
     2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns:mvc="http://www.springframework.org/schema/mvc"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:aop="http://www.springframework.org/schema/aop"
     6     xmlns:tx="http://www.springframework.org/schema/tx"
     7     xmlns:task="http://www.springframework.org/schema/task"
     8     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
     9         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    10         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
    11         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    13         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    14     <!-- 新闻的service -->
    15     <!-- <bean id="newsService" class="com.ssm.service.impl.NewsServiceImpl" /> -->
    16     <context:component-scan base-package="com.ssm.service.*"></context:component-scan>
    17     <context:annotation-config />
    18     <task:annotation-driven/>//使用的注解方式来实现Spring的定时任务
    19     
    20     <!-- 开启这个配置,spring才能识别@Scheduled注解     -->
    21     <!-- <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    22     <task:scheduler id="qbScheduler" pool-size="10"/>  --> 
    23 </beans>

    3.注册一个简单的任务,在service下新建一个包task,然后创建一个Spider类,类的的内容表示每逢10秒执行,打印一个语句

     1 package com.ssm.service.task;
     2 
     3 import java.util.Date;
     4 
     5 import org.springframework.scheduling.annotation.Scheduled;
     6 import org.springframework.stereotype.Component;
     7 import org.springframework.stereotype.Service;
     8 
     9 
    10 @Component("spider")
    11 public class Spider {
    12 
    13     @Scheduled(fixedRate=10*1000)
    14     public void getNews(){
    15         System.out.println(new Date()+"-------getNews");
    16     }
    17 }

    4.运行结果:

  • 相关阅读:
    用了7年做到项目经理,女朋友却离开了我
    手把手教你建网站--程序小白适用篇
    燃尽图的学习与理解
    每周进度
    四人组队
    读构建之法之感
    两人组队,小学生的四则运算
    词汇统计
    AMQP中的架构和组成元素
    MSSQL机制
  • 原文地址:https://www.cnblogs.com/lfeng1205/p/5701699.html
Copyright © 2020-2023  润新知