zpryan的专栏

要实现每隔多长时间发送一个请求

在applicationContext.xml中配置

所需jar包:

链接: 密码:6ojg

一,,配置目标类

<pre name="code" class="html"> <bean id="backupAdListPolicyJob" class="com.hyan.jms.Test"><property name="para" value="Spring定时测试v1"></property> </bean>二,定时器配置 <bean id="scheduSer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><!– 指定 要执行的定时任务类 –><property name="targetObject"><ref bean="scheduImpl"/></property><!– 指定执行任务的方法名称–><property name="targetMethod"><value>scheduImpl</value></property></bean> 三,定时器时间间隔 <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>四,启动定时器<bean id="sfb" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="timeTrigger"/></list></property> </bean>在web.xml中的配置<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <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的实现代码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分钟触发

那些曾经以为念念不忘的事情就在我们念念不忘的过程里,被我们遗忘了。

zpryan的专栏

相关文章:

你感兴趣的文章:

标签云: