如何正确的使用spring定时器?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
第一种,使用XML配置的方法
前期工作,配置spring的开发环境(这里用到了spring的web应用包,需要导入)
首先创建定时器的任务类,定时器要做什么工作,就在这里写什么方法。
package org.time;
import java.util.TimerTask;
public class MainTask extends TimerTask{
@Override
public void run() {
System.out.println("检测用户是否掉线");
}
}
接着在配置文件中对定时器进行配置。
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mainTask" class="org.time.MainTask"></bean>
<!-- 注册定时器信息 -->
<bean id="springTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- 延迟1秒执行首次任务 -->
<property name="delay" value="1000"></property>
<!-- 每隔2秒执行一次任务 -->
<property name="period" value="2000"></property>
<!-- 具体执行的任务 -->
<property name="timerTask" ref="mainTask"></property>
</bean>
<!-- 配置任务调度器工厂 -->
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="springTask"/>
</list>
</property>
</bean>
</beans>
最后还需要在web.xml中对配置信息进行注册:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
这样就完成了定时器的配置,这时启动tomcat,观察控制台输出的结果:
第二种,使用注解的形式
(spring中一使用注解,感觉就是比其他方法方便了很多,代码减少了很多)
这里需要用到AOP,需要引入AOP类库
先看定时器的任务类:
package org.time;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MainTask01{
@Scheduled(cron = "0/3 * * * * ?")
public void run() {
System.out.println("推送消息来了");
}
}
@Scheduled(cron = "0/3 * * * * ?") 表示三秒推送一次
corn可以配置各种时段任务:
字段 | 值 | 特殊表示字符 |
年 | 一般为空,1970-2099 | , - * / |
月 | 1-12 或者 JAN-DEC | , - * / |
星期 | 1-7 或者 SUN-SAT | , - * ? / L C # |
日 | 1-31 | , - * ? / L W C |
时 | 0-23 | , - * / |
分 | 0-59 | , - * / |
秒 | 0-59 | , - * / |
如: 配置每个工作日的10:20触发 :"0 20 10 ? * MON-FRI"
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<context:annotation-config />
<!-- spring扫描注解的配置 -->
<context:component-scan base-package="org.time" />
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>
配置文件的头部信息中比上一个引入了
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
这两句配置信息是必须要写的,这是spring识别@Scheduled注解的关键
这这样简单的几句配置之后,开启服务,运行结果:
s
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。