一、定时器类简单代码:功能每5分钟打印一次
package meiyx.com;
import java.util.Date;
import org.springframework.stereotype.Component;
@Component
public class PrintTimer {
public void print(){
System.out.println(new Date(System.currentTimeMillis())+"------->meiyx");
}
}
二、spring的相关配置文件:
config.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 请放置在文件末尾 -->
<context:component-scan base-package="meiyx.com">
</context:component-scan>
</beans>
三、定时器相关配置 timer.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--
cronExpression的介绍:
按顺序 <value> 秒 分 小时 日期 月份 星期 年<value>
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年 (可选)留空,1970-2099 , - * /
“*”字符被用来指定所有的值。如:”*“在分钟的字段域里表示“每分钟”。
参考《http://www.cnblogs.com/xiaopeng84/archive/2009/11/26/1611427.html》
-->
<!-- 定时单期发行数据处理开始,每天02:00执行一次:0 0 2 * * ?-->
<bean id="printTimerJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="printTimer" />
<property name="targetMethod" value="print" />
</bean>
<bean id="printTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="printTimerJob" />
<property name="cronExpression" value="0 0/5 * * * ?" />
</bean>
<!-- 启动定时器 -->
<bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey" value="applicationContext"/>
<property name="triggers">
<list>
<ref bean="printTimerTrigger" />
</list>
</property>
</bean>
<!-- end -->
</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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring 服务层的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config.xml
classpath*:timer.xml
</param-value>
</context-param>
<!-- Spring 容器启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
以上是实现定时器的简单过程,测试部署到tomcat或jboss即可;
有疑问联系Email:meiyx89@163.com,qq:532030490
分享到:
相关推荐
Spring中的Quartz配置-Spring-定时器-java定时器.doc
定时器的配置文件(两种方式:springmvc自带定时,Quartz与spring结合的定时)
NULL 博文链接:https://ap0406708.iteye.com/blog/588695
spring 定时器 内含TimerTask实现和Quartz实现两种方式
Spring Quartz定时器 Spring Quartz定时器 Spring Quartz定时器 Spring Quartz定时器
spring定时器Spring定时器的两种实现方式Java的Timer类和OpenSymphony的Quartz。
NULL 博文链接:https://x-nam.iteye.com/blog/728654
spring 定时器 quartz
Spring Quartz 定时器示例(Web工程版),欢迎下载。
Spring-quartz实现定时器(含代码)
Spring两种定时器实例配置:Java的TimerTask类和OpenSymphony的Quartz。包含5种配置方式:timer普通定时器、timer特定方法定时器、quartz简单定时器、quartz精确定时器、quartz特定方法定时器。简单实用,一看就会。
使用spring 实现的两种定时任务,使用eclipse直接导入就可使用
idea 使用spring自带的定时器quartz 使用的c3p0 v0.95.2所包含的jar 亲测可用
Quartz定时器。Spring中Quartz的配置
spring定时器代码。解决多次执行问题。包含quartz-all.jar包。 说明很详细。希望能帮上哪位朋友。
spring定时器Quartz,适合学习阶段的同学学习,可以直接运行
spring+quartz配置的定时器 可以动态添加任务
Spring Quartz 定时器示例(Java工程版),欢迎下载。
spring boot集成quartz定时器,job支持spring的依赖注入