springbatch 2.18 加spring3.1 写的helloWorld

借鉴

http://www.cnblogs.com/gulvzhe/archive/2011/10/31/2230655.html 孤旅者

 

package com.zyc.demo;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;


public class writeTasklet implements Tasklet {

  /** Message */
    private String message;

    /**
     * @param message
     *            the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
            throws Exception {
        System.out.println(message);
        return RepeatStatus.FINISHED;
    }

 


 

}

 

package com.zyc.demo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
import org.springframework.batch.core.configuration.support.ApplicationContextJobFactory;
import org.springframework.batch.core.configuration.support.ClassPathXmlApplicationContextFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JobLaunch {
    /**
     * @param args
     */
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(
         "batch.xml");
 JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
 Job job = (Job) context.getBean("helloWorldJob");

 try {
     /* 运行Job */
     JobExecution result = launcher.run(job, new JobParameters());
     /* 处理结束,控制台打印处理结果 */
     System.out.println(result.toString());
 } catch (Exception e) {
     e.printStackTrace();
 }
}


}

batch.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/batchhttp://www.springframework.org/schema/batch/spring-batch-2.1.xsd
  http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
     >
  <beans:import resource="applicationContext.xml"/>
   
     <job id="helloWorldJob">
        <step id="step_hello" next="step_world">
            <tasklet ref="hello" transaction-manager="transactionManager"></tasklet>
        </step>
        <step id="step_world">
            <tasklet ref="world" transaction-manager="transactionManager"></tasklet>
        </step>
    </job>

    <beans:bean id="hello" class="com.zyc.demo.writeTasklet">
        <beans:property name="message" value="Hello "></beans:property>
    </beans:bean>

    <beans:bean id="world" class="com.zyc.demo.writeTasklet">
        <beans:property name="message" value=" World!"></beans:property>
    </beans:bean>
     </beans:beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/batchhttp://www.springframework.org/schema/batch/spring-batch-2.1.xsd
  http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"    
     default-autowire="byName">
<beans:bean id="jobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <beans:property name="jobRepository" ref="jobRepository" />
  </beans:bean>
  <beans:bean id="jobRepository"
  class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">  
 </beans:bean>
 <beans:bean id="transactionManager"
  class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
 </beans:bean>  
</beans:beans>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值