paulwong

Quartz 中的任务开始、暂停和继续

第一个TASK:
package com.paul.springbatchtest.tasklet;

import org.quartz.SchedulerException;

public class TaskOne {
   
   
   
public void execue() throws SchedulerException
   
{
        System.out.println(
"Hello Spring Schedule!"  + Thread.currentThread().getName());
    }


}

第二个TASK用来恢复第一个TASK的:
package com.paul.springbatchtest.tasklet;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;

public class DailyTask {
   
   
private Scheduler scheduler;
   
private CronTrigger taskOneCronTrigger;
   
private JobDetail taskOneJobDetail;
   
   
public void execue() throws SchedulerException
   
{
        System.out.println(
"Start taskOneCronTrigger in DailyTask " + Thread.currentThread().getName());
        scheduler.addJob(taskOneJobDetail,
true);
        scheduler.scheduleJob(taskOneCronTrigger);
    }


   
public Scheduler getScheduler() {
       
return scheduler;
    }


   
public void setScheduler(Scheduler scheduler) {
       
this.scheduler = scheduler;
    }


   
public CronTrigger getTaskOneCronTrigger() {
       
return taskOneCronTrigger;
    }


   
public void setTaskOneCronTrigger(CronTrigger taskOneCronTrigger) {
       
this.taskOneCronTrigger = taskOneCronTrigger;
    }


   
public JobDetail getTaskOneJobDetail() {
       
return taskOneJobDetail;
    }


   
public void setTaskOneJobDetail(JobDetail taskOneJobDetail) {
       
this.taskOneJobDetail = taskOneJobDetail;
    }


}


第三个TASK,用来暂停第一个TASK的:
package com.paul.springbatchtest.tasklet;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class StopTaskOne implements ApplicationContextAware {
   
   
private ApplicationContext applicationContext;
   
   
private Scheduler scheduler;
   
private CronTrigger taskOneCronTrigger;
   
private JobDetail taskOneJobDetail;
   
   
public void execue() throws SchedulerException
   
{
        System.out.println(
"Stop Task One in " + Thread.currentThread().getName());
        scheduler
= (Scheduler)this.applicationContext.getBean("scheduler");
        scheduler.unscheduleJob(taskOneCronTrigger.getName(), taskOneCronTrigger.getGroup());
    }


   
public CronTrigger getTaskOneCronTrigger() {
       
return taskOneCronTrigger;
    }


   
public void setTaskOneCronTrigger(CronTrigger taskOneCronTrigger) {
       
this.taskOneCronTrigger = taskOneCronTrigger;
    }


   
public JobDetail getTaskOneJobDetail() {
       
return taskOneJobDetail;
    }


   
public void setTaskOneJobDetail(JobDetail taskOneJobDetail) {
       
this.taskOneJobDetail = taskOneJobDetail;
    }



   
public Scheduler getScheduler() {
       
return scheduler;
    }


   
public void setScheduler(Scheduler scheduler) {
       
this.scheduler = scheduler;
    }


    @Override
   
public void setApplicationContext(ApplicationContext applicationContext)
           
throws BeansException {
       
this.applicationContext = applicationContext;
    }

}


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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    
<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        
<property name="corePoolSize" value="10" />
        
<property name="maxPoolSize" value="100" />
        
<property name="queueCapacity" value="500" />
    
</bean>
    
    
<bean id="taskOne" class="com.paul.springbatchtest.tasklet.TaskOne" />

    
<bean id="taskOneJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
<property name="targetObject" ref="taskOne" />
        
<property name="targetMethod" value="execue" />
        
<property name="concurrent" value="false" />
    
</bean>

    
<bean id="taskOneCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        
<property name="jobDetail" ref="taskOneJobDetail" />
        
<property name="cronExpression" value="0/1 * * * * ?" />
    
</bean>
    
    
<!-- //////////////////////// -->
    
    
<bean id="dailyTask" class="com.paul.springbatchtest.tasklet.DailyTask"  >
        
<property name="scheduler" ref="scheduler" />
        
<property name="taskOneCronTrigger" ref="taskOneCronTrigger" />
        
<property name="taskOneJobDetail" ref="taskOneJobDetail" />
    
</bean>

    
<bean id="dailyJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
<property name="targetObject" ref="dailyTask" />
        
<property name="targetMethod" value="execue" />
        
<property name="concurrent" value="false" />
    
</bean>

    
<bean id="dailyCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        
<property name="jobDetail" ref="dailyJobDetail" />
        
<property name="cronExpression" value="0/15 * * * * ?" />
    
</bean>
    
    
<!-- //////////////////////// -->
    
    
    
<bean id="stopTaskOne" class="com.paul.springbatchtest.tasklet.StopTaskOne" >
        
<property name="taskOneCronTrigger" ref="taskOneCronTrigger" />
        
<!-- <property name="scheduler" ref="scheduler" /> -->
        
<property name="taskOneJobDetail" ref="taskOneJobDetail" />
    
</bean>

    
<bean id="stopTaskOneJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
<property name="targetObject" ref="stopTaskOne" />
        
<property name="targetMethod" value="execue" />
        
<property name="concurrent" value="false" />
    
</bean>

    
<bean id="stopTaskOneCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        
<property name="jobDetail" ref="stopTaskOneJobDetail" />
        
<property name="cronExpression" value="10,25,40,55 * * * * ?" />
    
</bean> <!-- -->

    
<bean id="scheduler"
        class
="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        
<property name="triggers">
            
<list>
                
<!-- <ref bean="taskOneCronTrigger" /> -->
                
<ref bean="dailyCronTrigger" />
                
<ref bean="stopTaskOneCronTrigger"/>
            
</list>
        
</property>
        
<property name="taskExecutor" ref="executor" />
    
</bean>

</beans>


MAVEN的POM文件:
<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.paul</groupId>
   
<artifactId>springbatchtest</artifactId>
   
<version>1.0</version>
   
<packaging>jar</packaging>

   
<name>springbatchtest</name>
   
<url>http://maven.apache.org</url>

   
<properties>
       
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       
<spring.batch.version>
            2.1.8.RELEASE
       
</spring.batch.version>
       
<maven.compiler.source>1.6</maven.compiler.source>
       
<maven.compiler.target>1.6</maven.compiler.target>
       
<spring.version>3.0.5.RELEASE</spring.version>
   
</properties>
   
<dependencies>
       
<dependency>
           
<groupId>org.springframework.batch</groupId>
           
<artifactId>spring-batch-core</artifactId>
           
<version>${spring.batch.version}</version>
           
<exclusions>
               
<exclusion>
                   
<groupId>commons-logging</groupId>
                   
<artifactId>commons-logging</artifactId>
               
</exclusion>
           
</exclusions>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework.batch</groupId>
           
<artifactId>spring-batch-infrastructure</artifactId>
           
<version>${spring.batch.version}</version>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework.batch</groupId>
           
<artifactId>spring-batch-test</artifactId>
           
<version>${spring.batch.version}</version>
           
<scope>test</scope>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-beans</artifactId>
           
<version>${spring.version}</version>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-context</artifactId>
           
<version>${spring.version}</version>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-core</artifactId>
           
<version>${spring.version}</version>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-jdbc</artifactId>
           
<version>${spring.version}</version>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-tx</artifactId>
           
<version>${spring.version}</version>
       
</dependency>
       
<dependency>
           
<groupId>org.slf4j</groupId>
           
<artifactId>slf4j-api</artifactId>
           
<version>1.6.1</version>
       
</dependency>
       
<dependency>
           
<groupId>org.slf4j</groupId>
           
<artifactId>jcl-over-slf4j</artifactId>
           
<version>1.6.1</version>
       
</dependency>
       
<dependency>
           
<groupId>ch.qos.logback</groupId>
           
<artifactId>logback-classic</artifactId>
           
<version>0.9.29</version>
       
</dependency>
       
<dependency>
           
<groupId>commons-io</groupId>
           
<artifactId>commons-io</artifactId>
           
<version>2.0.1</version>
       
</dependency>
       
<dependency>
           
<groupId>com.h2database</groupId>
           
<artifactId>h2</artifactId>
           
<version>1.3.168</version>
       
</dependency>
       
<dependency>
           
<groupId>junit</groupId>
           
<artifactId>junit</artifactId>
           
<version>4.8.2</version>
           
<scope>test</scope>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-test</artifactId>
           
<version>${spring.version}</version>
           
<scope>test</scope>
       
</dependency>
       
<dependency>
           
<groupId>javax.mail</groupId>
           
<artifactId>mail</artifactId>
           
<version>1.4.1</version>
       
</dependency>
       
<dependency>
           
<groupId>org.springframework</groupId>
           
<artifactId>spring-context-support</artifactId>
           
<version>${spring.version}</version>
       
</dependency>
       
<!-- <dependency>
            <groupId>org.opensymphony.quartz</groupId>
            <artifactId>quartz</artifactId>
            <version>1.6.1</version>
        </dependency>
-->
       
<dependency>
           
<groupId>mysql</groupId>
           
<artifactId>mysql-connector-java</artifactId>
           
<version>5.1.21</version>
       
</dependency>
       
<dependency>
           
<groupId>org.quartz-scheduler</groupId>
           
<artifactId>quartz</artifactId>
           
<version>1.8.4</version>
       
</dependency>
   
</dependencies>
</project>


测试文件:
package com.paul.springbatch.test;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.
class)
@ContextConfiguration(locations
= { "/schedule/quartz.xml"})
public class QuartzTaskOneTest {
   

   
    @Test
   
public void simpleTest() throws Exception{
       
       
while(true)
       
{
            Assert.assertTrue(
true);
        }

    }

}

posted on 2012-10-13 16:18 paulwong 阅读(3287) 评论(1)  编辑  收藏 所属分类: SRPING BATCH

Feedback

# re: Quartz 中的任务开始、暂停和继续 2014-04-14 10:45 最代码

可以参考下最代码网站上的这个代码:http://www.zuidaima.com/share/1748776162790400.htm  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航: