定时器:Spring Task

使用Spring的Task配置实现简易定时器,定时触发操作,可以是数据同步、消息发送等。

1.导包

需要导入spring、commons相关包,可以使用WebProject或者Maven项目。

2.定时器执行函数

新建类:src/test/TaskApp.java

注解:@Component

执行函数创建public void execute1(){System.out.println(new Date())};

3.配置文件

配置文件一般为复用。applicationContext-task.xml

3.1开头<bean>

中必要项为xmlns:task,xmlns:context,xsi:schemaLocation中task和context。

<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-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

 ......

</beans>

3.2<!-- 配置注解扫描 -->

    <context:component-scan base-package="test"/>

3.3配置TaskApp的bean

<bean id="TaskApp" class="test.TaskApp"/>

3.4配置定时器

 任务的并行调度<task:scheduler />,多个<task:scheduled>并发执行。

    <task:scheduler id="taskScheduler" pool-size="5" />

    <task:scheduled-tasks scheduler="taskScheduler">

 <task:scheduled ref="TaskApp"method="execute1"cron="0/5 * * * * ?"/>        

 </task:scheduled-tasks>

{秒} {分} {时} {日期(具体哪天)} {月} {星期}

3.5其他触发事件写法

 

        <!-- 每年10月20号的1点10分30秒触发任务 -->
        <task:scheduled ref="app" method="execute5" cron="30 10 1 20 10 ?"/>
        <!-- 每15秒、30秒、45秒时触发任务 -->
        <task:scheduled ref="app" method="execute6" cron="15,30,45 * * * * ?"/>
        <!-- 15秒到45秒每隔1秒触发任务 -->
        <task:scheduled ref="app" method="execute7" cron="15-45 * * * * ?"/>
        <!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 -->
        <task:scheduled ref="app" method="execute9" cron="15-30/5 * * * * ?"/>    
        <!-- 星期一到星期五的10点15分0秒触发任务 -->
        <task:scheduled ref="app" method="execute11" cron="0 15 10 ? * MON-FRI"/>

http://blog.csdn.net/u011116672/article/details/52517247

 

4.web.xml配置dispatcherServlet

WebProject项目生成web.xml:建立项目时勾选,或者后期Generate Deployment Descriptor Stub。

 

 

 

配置dispatcherServlet一般为复用,配置实用的xml进行配置。

 <servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-task.xml</param-value>

</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

5.定时器配置问题

1.多个程序任务定时执行并发问题:<task:scheduler id pool-size />

2.一个包含多线程的任务定时执行时,任务控制问题:一个任务所有线程执行完之前,其后的任务等待

使用CountDownLatch:new CountDownLatch(2),la.coutnDown(),la.await()

http://blog.csdn.net/qihezhuanjia123/article/details/73604955

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值