spring-batch+quartz的示例

   spring-batch是一个为批量处理面生的轻量级框架,关于它的描述不多说了,网上挺多的。下面要说的是一个spring-batch+quartz的一示例,完成的功能是从mysql中定时取数据,然后处理数据,最后再写入到mysql的另一个表里。

 pom文件如下,所需要的资源全在里面


  4.0.0

  com.batch.sample
  batchSample001
  0.0.1-SNAPSHOT
  jar

  batchSample001
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
    
        org.springframework.batch
        spring-batch-core
        3.0.6.RELEASE
        
    
    
        org.springframework
        spring-orm
        4.2.4.RELEASE
       
    
    
        mysql
        mysql-connector-java
        5.1.38
       
     
  
    org.slf4j  
    slf4j-log4j12  
    1.7.2  
 

	
		org.quartz-scheduler
		quartz
		1.8.6
	
	
	
		org.springframework
		spring-context-support
		4.0.5.RELEASE
	
	
  

spring-batch+quartz的配置文件如下:


	
	
	
		
			
				
				
			
		
	
	
	  
  
	
	  
		
		
	  
	
  
	
	

	
	
	
	  
		
		
	  
	
  
  
	
	
	
	

		
		
			
				classpath:batch-${ENVIRONMENT:hsql}.properties
			
		
		
		
		
		
		
	
		

	
	
		
		
		
		
	

	
		
	

	
		
		
	

	

	
	
	
		
		
		
	
	

	
		
		
		
        			
        				
        				
        			
        
	


	
		
		
		
	
	

	

quartz的时间调用实现如下:

package org.springframework.batch.quartz;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.sample.BillingItemProcessor;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobQuartz extends QuartzJobBean{
	public static final String RUN_MONTH_KEY = "run.month";
	@Override
	protected void executeInternal(JobExecutionContext paramJobExecutionContext)
			throws JobExecutionException {
		
		 Logger logger = LoggerFactory.getLogger(BillingItemProcessor.class);
		 logger.info("beginning");	
			
			SimpleJobLauncher launcher = new SimpleJobLauncher();
			Map jobDataMap = paramJobExecutionContext.getMergedJobDataMap();
			launcher.setJobRepository((JobRepository) jobDataMap.get("jobRepository"));
			launcher.setTaskExecutor(new SyncTaskExecutor());
			try {
				Map parameters = new HashMap();
				parameters.put(RUN_MONTH_KEY, new JobParameter("2011-2"+System.currentTimeMillis()));
				
				Long startTime=System.currentTimeMillis();
				
				JobExecution je = launcher.run((Job) jobDataMap.get("jobName"),
						new JobParameters(parameters));
				System.out.println("使用时间:"+(System.currentTimeMillis()-startTime));
				
				//System.out.println(je);
				//System.out.println(je.getJobInstance());
				//System.out.println(je.getStepExecutions());
			} catch (Exception e) {
				e.printStackTrace();
			}		
			 System.out.println("endding");
	}

}
调用的代码如下:

	public static void main(String[] args) {
	
	 Logger logger = LoggerFactory.getLogger(BillingItemProcessor.class);
	 logger.info("aaaaaaaaaaaaaaa");
		
		System.setProperty("ENVIRONMENT", "mysql");
		
		ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("parallel.xml");
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}		

	}

源码下载地址:http://download.csdn.net/detail/riapgypm/9404081


你可能感兴趣的:(java)