Spring 使用MethodInvokingJobdetailFactoryBean的Quartz Scheduler示例
在本教程中,我们将看到如何使用Spring Quartz Scheduler或者如何使用Quartz进行Spring的作业。
我们已经看到了如何使用计时器和timertask安排作业。
我们可以使用两种方式指定石英作业。
MethodInvokingJobdetailFactoryBeanJobdetailFactoryBean.
MethodInVokingJobdetailFactoryBean用于更简单的任务。
它指定目标对象和目标方法,因此将在使用触发器指定的时间执行目标对象的方法。
项目结构:
使用MethodInvokingJobdetailFactoryBean创建Spring Quartz Scheduler示例的步骤:
步骤1:
使用名称"springquartzschedulemexample"创建简单的Java Maven项目。
步骤2:更新POM.xml以放置Spring Core和Spring Quartz依赖项。
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.igi.theitroad</groupId> <artifactId>SpringQuartzSchedulerExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringQuartzSchedulerExample</name> <description>It provide spring quartz scheduler example using MethodInvokingJobDetailFactoryBean</description> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- Quartz framework dependencies --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.0</version> </dependency> </dependencies> <build> <finalName>SpringQuartzSchedulerExample</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> <properties> <spring.version>4.2.1.RELEASE</spring.version> <jdk.version>1.7</jdk.version> </properties> </project>
第3步:
在package com.igi.theitroad.spring中创建名为"sendemailtask.java"的类。
它是我们想要在特定时间或者时间间隔重复执行的代码的类。
package com.igi.theitroad.spring; import java.util.Calendar; public class SendEmailTask { public void sendEmail() { //You Jan want to put some condition for sending email //Actual email send logic System.out.println("Sending email at "+ Calendar.getInstance().getTime()); } }
步骤4:在此步骤中,我们需要在SRC/MAIN/RESOUMENT中创建"Quartz-Config.xml"。
Quartz-config.xml将拥有安排任务所需的所有配置。
我们需要在这里放进四个条目
- 需要指定任务(sendemailtask)。
- 需要指定MethodInVokingJobdetailFactoryBean,它指定将执行的类和方法。
- 需要为触发器进行条目,该触发器指定时间间隔并启动延迟时间。
- 需要指定schedulefactorybean以将触发器和methodInvokingjobdetailFactoryBean一起绑定。
quartz-config.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--Specify Task bean --> <bean id="sendEmailTask" class="com.igi.theitroad.spring.SendEmailTask" <!-- specifing class and method that is going to be called on a specified time basis --> <bean id="emailJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="sendEmailTask" <property name="targetMethod" value="sendEmail" </bean> <!-- simple trigger specify repeat interval and delay time --> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="emailJob" <property name="repeatInterval" value="10000" <property name="startDelay" value="1000" </bean> <!-- scheduler factory bean to put,the executing code and time intervals together --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="emailJob" </list> </property> <property name="triggers"> <list> <ref bean="simpleTrigger" </list> </property> </bean> </beans>
第5步:最后我们将创建主类来执行此功能。
我们将创建ClassPathxMlApplicationContext的对象加载Quartz-config.xml在package com.igi.theitroad.spring中的src/main/java中创建类名名称application.java
package com.igi.theitroad.spring; /** * @author igi Mandliya * */ import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) throws Exception { new ClassPathXmlApplicationContext("quartz-config.xml"); } }
步骤6:当我们运行上面的程序时,我们将得到以下输出。
Jan 17, 2015 3:15:52 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1441e2aa: startup date [Sun Jan 17 15:15:52 IST 2015]; root of context hierarchy Jan 17, 2015 3:15:53 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [quartz-config.xml] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Jan 17, 2015 3:15:53 PM org.springframework.context.support.DefaultLifecycleProcessor start INFO: Starting beans in phase 2147483647 Jan 17, 2015 3:15:53 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler INFO: Starting Quartz Scheduler now Sending email at Sun Jan 17 15:15:54 IST 2015 Sending email at Sun Jan 17 15:16:04 IST 2015 Sending email at Sun Jan 17 15:16:14 IST 2015 Sending email at Sun Jan 17 15:16:24 IST 2015 Sending email at Sun Jan 17 15:16:34 IST 2015