Spring 配置 定时器
2015-05-12 14:46
288 查看
要实现每隔多长时间发送一个请求
在applicationContext.xml中配置
所需jar包:
链接:http://pan.baidu.com/s/1jGL4kzO 密码:6ojg
一,配置目标类
[javascript]
view plaincopy
<pre name="code" class="html"> <bean id="backupAdListPolicyJob" class="com.hyan.jms.Test">
<property name="para" value="Spring定时测试v1"></property>
</bean>
二,定时器配置
[html]
view plaincopy
<bean id="scheduSer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定 要执行的定时任务类 -->
<property name="targetObject">
<ref bean="scheduImpl"/>
</property>
<!-- 指定执行任务的方法名称-->
<property name="targetMethod">
<value>scheduImpl</value>
</property>
</bean>
三,定时器时间间隔
[html]
view plaincopy
<bean id="timeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="scheduSer"/>
</property>
<property name="cronExpression">
<!-- 在每天10点到下午10:59期间的每1分钟触发 -->
<value>0 * 10 * * ?</value>
</property>
</bean>
四,启动定时器
[html]
view plaincopy
<bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="timeTrigger"/>
</list>
</property>
</bean>
在web.xml中的配置
[html]
view plaincopy
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
java的实现代码
[java]
view plaincopy
package com.hyan.jms;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
private String para;
public void scheduImpl(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(para+" Time is :"+format.format(new Date()));
}
public String getPara() {
return para;
}
public void setPara(String para) {
this.para = para;
}
}
启动tomcat 实现效果
关于定时器的表达式
时间的配置如下:
<value>0 * 10 * * ?</value>
时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年 *为任意 ?为无限制。由此上面所配置的内容就是,在每天10点到下午10:59期间的每1分钟触发
-----------------------------------------------------------------------------------------------------------------
定时任务设置
<!--启用spring @Transactional注解 -->
<tx:annotation-driven />
<!-- 定时任务设置 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
</bean>
<!-- 定时任务 -->
<!-- 调用工作对象和工作方法 -->
<bean id="jobBean" class="com.gamexun.gm.common.RemovetTemp" />
<bean id="jobWeekly" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="jobBean" />
<!-- 调用类的方法 -->
<property name="targetMethod" value="executeWeekly" />
<!-- 不能并行 -->
<property name="concurrent" value="false" />
</bean>
<!-- 每隔2小时执行一次 -->
<bean id="triggerWeekly" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobWeekly" />
<property name="cronExpression" value="0 0 0/2 * * ?"/><!-- 0 0/1 * * * ? --><!-- 0 */2 * * * -->
</bean>
<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true" >
<property name="triggers">
<list>
<ref bean="triggerWeekly" />
</list>
</property>
</bean>
在applicationContext.xml中配置
所需jar包:
链接:http://pan.baidu.com/s/1jGL4kzO 密码:6ojg
一,配置目标类
[javascript]
view plaincopy
<pre name="code" class="html"> <bean id="backupAdListPolicyJob" class="com.hyan.jms.Test">
<property name="para" value="Spring定时测试v1"></property>
</bean>
二,定时器配置
[html]
view plaincopy
<bean id="scheduSer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定 要执行的定时任务类 -->
<property name="targetObject">
<ref bean="scheduImpl"/>
</property>
<!-- 指定执行任务的方法名称-->
<property name="targetMethod">
<value>scheduImpl</value>
</property>
</bean>
三,定时器时间间隔
[html]
view plaincopy
<bean id="timeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 声明要运行的实体 -->
<property name="jobDetail">
<ref bean="scheduSer"/>
</property>
<property name="cronExpression">
<!-- 在每天10点到下午10:59期间的每1分钟触发 -->
<value>0 * 10 * * ?</value>
</property>
</bean>
四,启动定时器
[html]
view plaincopy
<bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="timeTrigger"/>
</list>
</property>
</bean>
在web.xml中的配置
[html]
view plaincopy
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
java的实现代码
[java]
view plaincopy
package com.hyan.jms;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
private String para;
public void scheduImpl(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(para+" Time is :"+format.format(new Date()));
}
public String getPara() {
return para;
}
public void setPara(String para) {
this.para = para;
}
}
启动tomcat 实现效果
关于定时器的表达式
时间的配置如下:
<value>0 * 10 * * ?</value>
时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年 *为任意 ?为无限制。由此上面所配置的内容就是,在每天10点到下午10:59期间的每1分钟触发
-----------------------------------------------------------------------------------------------------------------
定时任务设置
<!--启用spring @Transactional注解 -->
<tx:annotation-driven />
<!-- 定时任务设置 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
</bean>
<!-- 定时任务 -->
<!-- 调用工作对象和工作方法 -->
<bean id="jobBean" class="com.gamexun.gm.common.RemovetTemp" />
<bean id="jobWeekly" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="jobBean" />
<!-- 调用类的方法 -->
<property name="targetMethod" value="executeWeekly" />
<!-- 不能并行 -->
<property name="concurrent" value="false" />
</bean>
<!-- 每隔2小时执行一次 -->
<bean id="triggerWeekly" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobWeekly" />
<property name="cronExpression" value="0 0 0/2 * * ?"/><!-- 0 0/1 * * * ? --><!-- 0 */2 * * * -->
</bean>
<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true" >
<property name="triggers">
<list>
<ref bean="triggerWeekly" />
</list>
</property>
</bean>
相关文章推荐
- 【spring配置】——spring整合Quartz定时器
- Spring中Quartz的配置 定时器 示例
- 配置spring-quartz定时器
- spring定时器的配置
- 转spring配置定时器的时间设置
- spring中配置定时器
- 基于maven的spring boot 项目porm文件配置(含定时器,数据抓取,分词器依赖配置)
- spring 定时器时间配置
- Spring3.1.1 quartz1.8.6 定时器配置
- spring配置quartz定时器任务
- spring定时器一些时间配置例子参考
- spring定时器配置
- Spring 定时器注解 与配置文件
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(十一)之 spring中定时器quartz的整合续集(Quartz集群配置)
- Spring中配置定时器 转
- Spring定时器配置
- 定时器的实现、java定时器Timer和Quartz介绍与Spring中定时器的配置
- spring 定时器配置
- Spring Quartz定时器源码 配置文件 多个触发器
- Spring 配置定时器(注解+xml)方式—整理