第一篇:第一个spring定时执行任务

1.创建一个名为testJob的项目

项目结构为:

2.导入依赖的jar包
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.test</groupId>
	<artifactId>testJob</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>testJob Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<springframework.version>3.0.5.RELEASE</springframework.version>
	</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>testJob</finalName>
	</build>
</project>

3.配置spring与quartz的配置文件
applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 配置调度程序quartz ,其中配置JobDetail有两种方式 -->
	<!--方式一:使用JobDetailBean,任务类必须实现Job接口  运行时请将方式一注释掉!
	<bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="name" value="exampleJob"></property>
		<property name="jobClass" value="com.test.job.xml.TestXml"></property>
		<property name="jobDataAsMap">
			<map>
				<entry key="service">
					<value>simple is the beat</value>
				</entry>
			</map>
		</property>
	</bean>
	 -->
	<!-- 方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法 -->
	<!-- 定义目标bean和bean中的方法 -->
	<bean id="testXml" class="com.test.job.xml.TestXml" />
	<bean id="jobMethod"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="testXml" />
		</property>
		<property name="targetMethod">  <!-- 要执行的方法名称 -->
			<value>execute</value>
		</property>
	</bean>

	<!-- ======================== 调度触发器 此处为定时方式 ======================== 
	<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobMethod"></property>
		<property name="cronExpression" value="0/5 * * * * ?"></property>
	</bean> -->

	<!-- 调度时间设置 -->
	<bean id="testTaskJobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail">
			<ref bean="jobMethod" />
		</property>
		<!-- 延时启动时间,单位ms -->
		<property name="startDelay" value="0"></property>
		<!-- 重复间隔时间,单位ms -->
		<property name="repeatInterval" value="5000">
		</property>
	</bean>

	<!-- ======================== 调度工厂 ======================== -->
	<bean lazy-init="false" id="SpringJobSchedulerFactoryBean"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="testTaskJobTrigger" />
			</list>
		</property>
	</bean>
</beans>
4.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>testJob</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

5.java代码
在com.test.job.xml包下创建TestXml.java类
package com.test.job.xml;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestXml {

	 private static int counter = 0;  
//此处为处理方法
	    protected void execute()  {  
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日    HH:mm:ss");
	        long ms = System.currentTimeMillis();  
	        System.out.println("\t\t" + sdf.format(new Date(ms)));  
	        System.out.println("=======================第(" + counter++ + ")条数据================================");  
	    }  
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值