spring task

2 篇文章 0 订阅
1 篇文章 0 订阅

任务调度器

  1. 引用命名空间调用
  2. 配置最大线程数
  3. 编写作业任务类
  4. 测试
    以下是我的applicationContext.xml配置:
    <!-- 扫描该包及该包下的子包 -->
    <context:component-scan base-package="com.zjm"/>
    <!-- 定时任务配置:最大线程数 -->
    <task:scheduler id="qbScheduler" pool-size="10" />
    <task:annotation-driven scheduler="qbScheduler" mode="proxy" />

这个也是配置spring task的最大线程数,前后顺序不能乱,先scheduler,后annotation-driven。
当然在此之前要先配置命名空间:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.0.xsd">

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd这两个就是spring task命名空间。
配置完上面的application.xml的部分,接下来要去写任务类了
我的任务类很简单

        package com.zjm.test;

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

import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zjm.entiry.Employee;

@Component
public class SpringTaskTest {

    private static Logger logger = Logger.getLogger(SpringTaskTest.class);

    @BeforeClass
    public static void setUpBeforeClass() throws BeansException {
        try {
            ApplicationContext context =new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Scheduled(cron = "0 59 10 6 * ?")     //第二种方式
    public void log() {
        System.out.println("测试定时");
    };

    @Test
    @Scheduled(fixedRate=3*1000)     //第二种方式
    public void tst() {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("测试频路");
    };
}

Component这个注解是标识这是个任务类,@Scheduled这个注解的话,在前面的配置最大线程的时候有声明了,所以可以直接使用,任务类中又fixedRate,cron 等一些参数,这里就介绍两个了,cron 是定时用的,fixedRate是每几秒执行几次,参数是long,3*1000,代表是3秒。一旦有/,那么/的后面必将是频数。
下面给出cron的一些表达式(秒 分 时 天 月 星期 年):

0 0 10,14,16 * * ? 每天上午10点,下午2点,40 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:102:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

L在一些比较差一点的版本是用不了,可以换个思维,0 0 0 1 * ?,这样也可以,每月第一天。
这里是我测试后的截图:
这里写图片描述
这个代码我就不上传了,很简单的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值