spring整合task

1、基于注解配置的task
(1)task 配置

<?xml version="1.0" encoding="UTF-8"?>
<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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 基于注解的spring task -->

    <!-- 配置扫描的包 -->
    <context:component-scan base-package="com.kexq.common.task" />

    <!-- spring tack-->
    <task:annotation-driven/>
</beans>

(2)实体类

@Component
public class TaskTest {
    /**
     * 基于注解
     */
    @Scheduled(cron="0 0/5 * * * ?")
    public void myJob(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sdf.format(new Date());

        System.out.println("定时任务taskTest:"+s+"------------");
    }
}

注:有关corn表达式,参考:https://www.cnblogs.com/javahr/p/8318728.html

2、基于xml配置的task
(1)xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task.xsd">
    
    <!-- 基于xml配置的spring task -->
    <bean class="com.kexq.common.task.TaskTest" id="task" />

    <!-- 定时器开关 -->
    <task:executor id="executor" pool-size="5"   />
    <task:annotation-driven executor="executor" scheduler="scheduler"/>

    <!-- cron表达式前后不得有空格 -->
    <task:scheduled-tasks>
        <task:scheduled ref="task" method="myJob" cron="0 0/5 * * * ?"/>
        <task:scheduled ref="task" method="myJob1" cron="0 0/5 * * * ?"/>
    </task:scheduled-tasks>
    <task:scheduler id="scheduler" pool-size="10"/>

    <!--
        任务调度器的配置详细参数说明:
        task:scheduler/@pool-size:调度线程池的大小,调度线程在被调度任务完成前不会空闲
        task:scheduled/@cron:cron表达式,注意,若上次任务未完成,即使到了下一次调度时间,任务也不会重复调度
    -->

</beans>

(2)实体类

public class TaskTest {
    /**
     * 基于mxl配置
     */
    public void myJob1(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sdf.format(new Date());

        System.out.println("定时任务taskTest1:"+s+"------------");
    }
}

猜你喜欢

转载自blog.csdn.net/cccp_2009/article/details/84335585