Spring Batch(3)(2)

           

Spring Batch +Quartz 实现定时批量处理csv文件数据再保存到数据库

继上一篇文章Spring Batch (3)(1)

message_job.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"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
                http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<context:annotation-config />


<!-- Component scan to find all Spring components -->
<context:component-scan base-package="com.springbatch.hello.SpringBatch2Demo" />   
            <!-- datasource -->   
            <import resource="datasource_set.xml"/>
          
            
       <batch:job id="messageJob">
           <batch:step id="messageStep">
                   <batch:tasklet>
                        <batch:chunk reader="messageReader" processor="messageProcessor"  writer="messageWriter"
                        commit-interval="4"  chunk-completion-policy="">              
                        </batch:chunk>
                   </batch:tasklet>
           </batch:step>
      </batch:job> 
      
      
      <bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
      
       <bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
   
         <!-- reader -->         
     <bean id="fieldSetMapper" class="com.springbatch.hello.SpringBatch2Demo.UeserMapper"></bean>             
      <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"/> 
                 
      <bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper" >
         <property name="lineTokenizer" ref="lineTokenizer"></property>
         <property name="fieldSetMapper" ref="fieldSetMapper"></property>
      </bean>   
      
      <bean id="messageReader" class="org.springframework.batch.item.file.FlatFileItemReader">
         <property name="lineMapper" ref="lineMapper"></property>
         <property name="resource" value="user.csv"></property>
      </bean>         
      <!-- processor -->            
          <bean  id="messageProcessor" class="com.springbatch.hello.SpringBatch2Demo.MessageProccessor"></bean>        
        <!-- writer -->          
            <bean id="messageWriter"   class="com.springbatch.hello.SpringBatch2Demo.MessageWriter"></bean>   
                  
      <!-- quartz set  -->
     
     <!-- Spring Batch Job同一个job instance,成功执行后是不允许重新执行的【失败后是否允许重跑,可通过配置Job的restartable参数来控制,默认是true】,如果需要重新执行,可以变通处理,  
    添加一个JobParameters构建类,以当前时间作为参数,保证其他参数相同的情况下却是不同的job instance -->  
<bean id="jobParameterBulider" class="org.springframework.batch.core.JobParametersBuilder" /> 
                 
 <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                 <property name="targetObject" ref="quartzJob"/>
                 <property name="targetMethod"  value="execute"/>
 </bean>
   
 
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    
    <!-- 添加触发器 -->    
    <property name="triggers">    
          <bean id="CronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"/>
      <!-- 5秒执行一次 -->
        <property name="cronExpression" value="*/5 * * * * ?" /> 
     </bean> 
    </property>    
</bean>                     
  </beans>


datasource-set.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"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
                http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<context:annotation-config />


<!-- Component scan to find all Spring components -->
<context:component-scan base-package="com.springbatch.hello.SpringBatch2Demo" />
    
       
        
         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
             <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
             <property name="url" value="jdbc:mysql://localhost:3306/springbatchtest"/>
             <property name="username" value="root"/>
             <property name="password" value="123456liu"/>
         </bean>         
                  
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
               <property name="dataSource" ref="dataSource"/>
          </bean>        
                  
          <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
               <property name="dataSource" ref="dataSource"/>
          </bean>        
          
  </beans>



把Spring Batch(3)(1) 的JobLauncherDetails类  换成Quartz_Job类

package com.springbatch.hello.SpringBatch2Demo;


import java.util.Date;


import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 @Component("quartzJob")
public class Quratz_Job{
@Autowired
   private JobLauncher jobLauncher;

@Autowired
private Job messageJob;

@Autowired
private JobParametersBuilder jobParametersBuilder ;

public  void execute() throws Exception{

jobParametersBuilder.addDate("data", new Date());
jobLauncher.run(messageJob, jobParametersBuilder.toJobParameters());
}
}

测试类:

其他的实体类和reader 等类不变

package com.springbatch.hello.SpringBatch2Demo;


import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StartQuartz {
   public static void main(String[] args) {
  ClassPathXmlApplicationContext c =   
               new ClassPathXmlApplicationContext("message_job.xml"); 
}
}

结果

Sep 01, 2017 3:33:57 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@246b179d: startup date [Fri Sep 01 15:33:57 CST 2017]; root of context hierarchy
Sep 01, 2017 3:33:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [message_job.xml]
Sep 01, 2017 3:33:58 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
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.
Sep 01, 2017 3:33:58 PM org.springframework.context.support.DefaultLifecycleProcessor start
INFO: Starting beans in phase 2147483647
Sep 01, 2017 3:33:58 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
INFO: Starting Quartz Scheduler now
Sep 01, 2017 3:34:00 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=messageJob]] launched with the following parameters: [{data=1504251240002}]
Sep 01, 2017 3:34:00 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [messageStep]
被影响了:2 行
被影响了:4 行
Sep 01, 2017 3:34:00 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=messageJob]] completed with the following parameters: [{data=1504251240002}] and the following status: [COMPLETED]
Sep 01, 2017 3:34:05 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=messageJob]] launched with the following parameters: [{data=1504251245001}]
Sep 01, 2017 3:34:05 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [messageStep]
被影响了:2 行
被影响了:4 行
Sep 01, 2017 3:34:05 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=messageJob]] completed with the following parameters: [{data=1504251245001}] and the following status: [COMPLETED]

这个比上一个更好理解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值