Spring Quartz Scheduler示例

时间:2020-02-23 14:35:58  来源:igfitidea点击:

在本教程中,我们将看到如何使用Spring Quartz调度或者如何Spring石英集成调度作业。
我们已经看到了如何使用计时器和timertask安排作业。

有两种方法,通过它可以指定石英工作。
MethodInvokingJobdetailFactoryBeanJobdetailFactoryBean.

JobDetailFactoryBean用于复杂任务。
我们需要创建扩展QuartzjobBean的作业类。

项目结构:

使用JobDetailFactoryBean创建Spring Quartz Scheduler示例的步骤是:

步骤1:

使用名称"springquartzschedulerexampleusingquartzjobbean"创建简单的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步:

中创建一个包com.igi.theitroad.spring中的src/main/java的所谓的"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步:在Package com.igi.theitroad.spring中创建名为"nobscheduler.java"的类名为"nobscheduler.java"..spring ..此类将扩展Quartzjobbean和ExecuteInternal方法将在触发中指定的特定时间调用。

package com.igi.theitroad.spring;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class JobScheduler extends QuartzJobBean {
 SendEmailTask sendEmailTask;
 
 
 public void setSendEmailTask(SendEmailTask sendEmailTask) {
  this.sendEmailTask = sendEmailTask;
 }
 
 
 @Override
 protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
  
  sendEmailTask.sendEmail();
 }
 
 
}

步骤5:在此步骤中,我们需要在SRC/Main/Resources中创建"Quartz-config.xml"。
Quartz-config.xml将拥有计划任务所需的所有配置。
我们需要在此处放入四个条目

  • 需要指定任务(sendemailtask)。
  • 需要指定JobDetailFactoryBean
  • 需要为触发器进行条目,该触发器指定时间间隔并启动延迟时间。
  • 需要指定schedulefactorybean以将触发器和作业绑定到一起。

石英-config.xml中

<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" 
 
 <!-- Spring Quartz -->
 <bean name="emailJob"
  class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
  <property name="jobClass" value="com.igi.theitroad.spring.JobScheduler" 
 
  <property name="jobDataAsMap">
   <map>
    <entry key="sendEmailTask" value-ref="sendEmailTask" 
   </map>
  </property>
  <property name="durability" value="true" 
 </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 bind,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>

步骤6:最后我们将创建主类来执行此功能。
我们将创建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");
 }
}

第7步:在上面的程序运行时,我们将获取以下输出。

Jan 24, 2015 1:09:13 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1441e2aa: startup date [Sun Jan 24 01:09:13 IST 2015]; root of context hierarchy
Jan 24, 2015 1:09:13 AM 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 24, 2015 1:09:14 AM org.springframework.context.support.DefaultLifecycleProcessor start
INFO: Starting beans in phase 2147483647
Jan 24, 2015 1:09:14 AM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
INFO: Starting Quartz Scheduler now
Sending email at Sun Jan 24 01:09:15 IST 2015
Sending email at Sun Jan 24 01:09:25 IST 2015
Sending email at Sun Jan 24 01:09:35 IST 2015
Sending email at Sun Jan 24 01:09:45 IST 2015
Sending email at Sun Jan 24 01:09:55 IST 2015