Spring 配置 定时器

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

在applicationContext.xml中配置

所需jar包:

链接:http://pan.baidu.com/s/1jGL4kzO 密码: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="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的实现代码

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分钟触发

时间: 2025-01-09 22:46:59

Spring 配置 定时器的相关文章

Spring 配置定时器

<!-- 定时器:定时调用的类引用 --> <bean id="jobScheduller" class="cn.com.caissa.util.TimerTask" /> <!-- 触发类级方法配置 targetMethod指定定时调用的方法--> <bean id="jobSchedullerMethod" class="org.springframework.scheduling.quar

Spring 配置定时器(注解+xml)方式—整理

一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 1 xmlns:task="http://www.springframework.org/schema/task" 2 http://www.springframework.org/schema/task 3 http://www.springframework.org/schema /task/springtask3.1.xsd 4 2. 最后是我们的task任务扫描注解 1

spring配置定时器的时间设置

时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年    *为任意 ?为无限制. 具体如下: "0/10 * * * * ?" 每10秒触发 "0 0 12 * * ?" 每天中午12点触发 "0 15 10 ? * *" 每天上午10:15触发 "0 15 10 * * ?" 每天上午10:15触发 "0 15 10 * * ? *" 每天上午10:15触发 "0 15 10 * *

分享Spring Scheduled定时器的用法

摘要:在coding中经常会用到定时器,指定每隔1个小时,或是每天凌晨2点执行一段代码段,若是使用java.util.Timer来做这种事情,未免重复造轮子.幸亏Spring中封装有定时器,而且非常好用,采用注解的形式配置某时某刻执行一段代码段.在之前的项目中使用过一次,下面就把代码.配置一并分享与大家. 关键词:Spring, JAVA, Scheduled, 定时器 一. 首先写一个Handler接口(“定时器Handler”),用以说明实现这一接口的类做的处理逻辑都是由定时器驱动的. 1

spring 配置

SpringMVC配置 引入占位符<context:property-placeholder location="classpath:config.properties" /><util:properties id="properties" location="classpath:config.properties"/> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --&

spring的定时器

spring的定时器 搞了一两个小时,忘了导包Quartz.  下面的博客又没有说,开始报错,后面再外边逛了逛,回来理清思路,导包后就没有报错了. 定时器还是很简单的. com.ovaltech.controller.TaskJob  是一个普普通通的类,可以安静的写自己的方法.当然,<value>0 0 7,12,16 * * ? </value>时间的触发规律还是要贴一贴的. 这是时间的设置规则 org.springframework.scheduling.quartz.Cro

Spring quartz定时器动态多任务实现

项目中经常会碰到需要定时执行的任务,并且需要执行什么任务,以及任务执行的时间都由用户自定义的需求.quartz是比较常用的定时器工具,并且在spring框架中也已经做了很好的集成,所以在以spring+hibernate+struts的主流架构中,我们可以采用quartz来做定时器任务的解决方案,下面,我们来看下如何在项目中使用quartz来做动态多任务定时器功能. 1.简单单任务定时器的spring配置 <!-- 配置定时任务,用于初始化定时器 -->       <bean id=&

spring配置hibernate的sessionFactory的几种方法

分类: JAVA Spring Hibernate 2013-01-27 20:47  1851人阅读  评论(0)  收藏  举报 spring配置hibernate的sessionFactory 之前用spring2+hibernate3+struts2开发了一个彩信发布系统,由于第一次使用此架构,造成applicationContext.xml中的配置非常冗长,而且经常因为更改一个小配置项(例:数据库ip.用户名.密码等)将此文件作修改,这及不利于项目维护,万一粗心造成其他地方变动,会对本

.嵌入式jetty启动spring(java配置方式),junit测试用.标准spring 配置(java config) 嵌入式jetty9启动

package com.doctor.embeddedjetty; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.web.con