Spring+Quartz搞定web定时任务

导入依赖的包,我用的是maven,pom.xm配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<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.qiny</groupId>
	<artifactId>my</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>my</name>

	<properties>
		<struts2.version>2.3.3</struts2.version>
		<spring.version>2.5.2</spring.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>${struts2.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-config-browser-plugin</artifactId>
			<version>${struts2.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-junit-plugin</artifactId>
			<version>${struts2.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.5</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.4</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>quartz</groupId>
			<artifactId>quartz</artifactId>
			<version>1.6.0</version>
		</dependency>

		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.21</version>
				<configuration>
					<scanIntervalSeconds>10</scanIntervalSeconds>
					<scanTargets>
						<scanTarget>src/main/webapp/WEB-INF</scanTarget>
						<scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
						<scanTarget>src/main/resources/struts.xml</scanTarget>
						<scanTarget>src/main/resources/example.xml</scanTarget>
					</scanTargets>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

添加自己的类,执行定时任务

package com.qiny.quartz;

import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
//需要继承QuartzJobBean
public class MyTask extends QuartzJobBean {

	@Override
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
		System.out.println("启动了"+new Date());
	}

}

添加spring配置 文件:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName">
	<context:component-scan base-package="com.qiny"></context:component-scan>

	<bean name="contentarbitrateservicejob" class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass">
			<value>com.qiny.quartz.MyTask</value>
		</property>
		<property name="jobDataAsMap">
			<map>
				<entry key="timeout">
					<value>5</value>
				</entry>
			</map>
		</property>
	</bean>

	<!-- 配置触发器 -->
	<bean id="cronTriggerCA" class="org.springframework.scheduling.quartz.CronTriggerBean">

		<property name="jobDetail">
			<ref bean="contentarbitrateservicejob" />
		</property>
		<!-- 每分钟01秒触发 -->
		<property name="cronExpression">
			<value>1 * * * * ?</value>
		</property>

	</bean>

	<!-- 添加触发器 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref local="cronTriggerCA" />
			</list>
		</property>
	</bean>
</beans>

你可能感兴趣的:(java,spring,quartz定时器)