Spring学习笔记之定时器

来源:互联网 发布:精子优化处理过程 编辑:程序博客网 时间:2024/06/05 11:31

  Spring的定时器主要是集成了Quartz框架。(Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用)。定时器在复杂的业务系统中是比较常用的,因为我们通常会把一些比耗时间的处理通过批量,在凌晨通过任务调度来处理。比如银行的非实时转账等。

Spring定时器的使用主要分为一下几部分:

1、任务调度工厂类SchedulerFactoryBean,它可以配置多个触发器,如

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="BusinessTestTrigger" /></list></property></bean>

我们只需要配置引用触发器,像

<property name="triggers"><list><ref local="BusinessTestTrigger" /><ref local="XXXXTrigger" /><ref local="XXXX2Trigger" /></list></property>

        2、触发器,触发器主要用来设置任务调度规则,分为SimpleTriggerFactoryBean和CronTriggerFactoryBean。SimpleTriggerFactoryBean主要是设置一些简单的定时器,如什么时候开始每隔多长时间执行一次。CronTriggerFactoryBean则可以设置一些更为复杂的定时器。如每天的几点触发

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><!-- see the example of method invoking job above --><property name="jobDetail" ref="jobDetail"/><!-- 10 seconds --><property name="startDelay" value="10000"/><!-- repeat every 50 seconds --><property name="repeatInterval" value="50000"/></bean><bean id="BusinessTestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><span style="white-space:pre"></span><property name="jobDetail"><span style="white-space:pre"></span><ref bean="BusinessTestDetail" /><span style="white-space:pre"></span></property><span style="white-space:pre"></span><property name="cronExpression"><span style="white-space:pre"></span><value>0/5 * * * * ?</value><span style="white-space:pre"></span></property><span style="white-space:pre"></span></bean>
      3、JobDetai,JobDetail主要是用来设置,目标执行类,执行方法,以及设置是否可并发执行Job

<bean id="BusinessTestDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="BusinessTestTime" /></property><property name="concurrent" value="false"></property><!--concurrent=false设置job不能并发  --><property name="targetMethod" value="execute"></property></bean>
4、具体的执行类

<bean id="BusinessTestTime" class="com.timer.app.SpringTriggerTest"><property name="para" value="我是一个定时器"></property></bean>

下面是详细的代码实现:

(1)、首先建立一个Maven项目,来导入需要的jar包。pom.xml配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.csii</groupId><artifactId>SpringTimerExample</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>SpringTimerExample</name><url>http://maven.apache.org</url><!-- <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties> --><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>1.8.5</version></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>7.5.4.v20111024</version><configuration><scanIntervalSeconds>10</scanIntervalSeconds><webApp><contextPath>/${project.artifactId}</contextPath></webApp></configuration></plugin></plugins></build><properties><springframework.version>3.2.3.RELEASE</springframework.version></properties></project>
     (2)、新建一个资源文件夹Source Folder——》src/main/resource,在src/main/resource下建立一个config文件夹。在config文件夹下建立一个spring-Context.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"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd">               <import resource="schedule.xml"/><bean id="myTest" class="com.csii.SpringTimerExample.MyFirstSpringApp"></bean></beans>

(3)在config文件夹下建立一个schedule.xml文件

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref local="BusinessTestTrigger" /></list></property></bean><bean id="BusinessTestTime" class="com.timer.app.SpringTriggerTest"><property name="para" value="我是一个定时器"></property></bean><bean id="BusinessTestDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="BusinessTestTime" /></property><property name="concurrent" value="false"></property><!--concurrent=false设置job不能并发  --><property name="targetMethod" value="execute"></property></bean><bean id="BusinessTestTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="BusinessTestDetail" /></property><property name="cronExpression"><value>0/5 * * * * ?</value></property></bean></beans> 

   (4)创建一个实体类SpringTriggerTest.java

package com.timer.app;import java.text.SimpleDateFormat;import java.util.Date;public class SpringTriggerTest {private String para;public void execute() {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(para + " !当前时间是:"+ format.format(new Date()));}public String getPara() {return para;}public void setPara(String para) {this.para = para;}}

(5)建立一个测试类Test.java

package com.csii.SpringTimerExample;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {/*ApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/spring-Context.xml");MyFirstSpringApp myfirstSpringApp=(MyFirstSpringApp)applicationContext.getBean("myTest");myfirstSpringApp.sayHelloWorld();*/ApplicationContext applicationContext=new ClassPathXmlApplicationContext("config/spring-Context.xml");MyFirstSpringApp myFirstSpringApp=(MyFirstSpringApp)applicationContext.getBean("myTest");myFirstSpringApp.sayHelloWorld();}}
注:只要运行载入xml文件,把容器服务起来。job中的方法会自动触发,并不需要调用。
运行结果:

我是一个定时器 !当前时间是:2015-03-21 14:24:11
Hello World
我是一个定时器 !当前时间是:2015-03-21 14:24:15
我是一个定时器 !当前时间是:2015-03-21 14:24:20
我是一个定时器 !当前时间是:2015-03-21 14:24:25
我是一个定时器 !当前时间是:2015-03-21 14:24:30
我是一个定时器 !当前时间是:2015-03-21 14:24:35


0 0
原创粉丝点击