spring4.2.5实现调度任务的几种方式

目前spring的最新版本是4.2.5,通过对spring scheduling的学习,总结有几种可以实现调度任务的方式

1、注解

2、Task命名空间

3、使用Quartz调度

下面分别介绍实现方式

首先引入相关jar包,pom.xml中配置:


          javax.servlet
          servlet-api
          2.3
          compile
      
      
          org.springframework
          spring-web
          4.2.5.RELEASE
      
      
          org.springframework
          spring-context
          4.2.5.RELEASE
      
      
          org.springframework
          spring-context-support
          4.2.5.RELEASE
      
      
          org.springframework
          spring-tx
          4.2.5.RELEASE
      
      
          org.quartz-scheduler
          quartz
          2.2.2
      


web.xml中配置加载spring:




  Archetype Created Web Application
    
        org.springframework.web.context.ContextLoaderListener
    
    
        contextConfigLocation
        classpath:spring-config.xml
    


1、使用注解的方式开始调度任务

配置java类


package test.spring.scheduling.annotation;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;

@Configuration
@EnableScheduling
public class AnnotationScheduleService {

    @Scheduled(cron = "0/5 * * * * ?")
    public void test(){
        System.out.println(new Date());
    }
}

 
  
配置spring.xml




    
    
    

2、使用task命名空间运行schedule

java类

package test.spring.scheduling.tasknamespace;

import java.util.Date;

/**
 * Created by yuan on 2016/3/12.
 */
public class TaskNamespaceScheduleService {
    public void test(){
        System.out.println("TaskNamespaceScheduleService "+new Date());
    }
}

spring配置




    
    
        
    

或者:




    

    
    
        
    


3、使用Quartz调度schedule

分别使用JobDetailFactoryBean和MethodInvokingJobDetailFactoryBean配置调度任务

package test.spring.scheduling.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * Created by yuan on 2016/3/12.
 */
public class QuartzOne extends QuartzJobBean {

    /**
     * Execute the actual job. The job data map will already have been
     * applied as bean property values by execute. The contract is
     * exactly the same as for the standard Quartz execute method.
     *
     * @param context
     * @see #execute
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("QuartzOne "+new Date());
    }
}

package test.spring.scheduling.quartz;

import java.util.Date;

/**
 * Created by yuan on 2016/3/12.
 */
public class QuartzTwo {

    public void test(){
        System.out.println("QuartzTwo "+new Date());
    }
}



    
    
        
    
    
    
    
        
        
    

    
    
        
        
        
        
        
        
    

    
        
        
        
    

    
    
        
            
                
                
            
        
    

参考:spring官方文档 http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#scheduling-quartz-method-invoking-job

你可能感兴趣的:(spring)