Spring Batch Example – Hello World Project

来源:互联网 发布:怎么给淘宝店做宣传 编辑:程序博客网 时间:2024/06/10 08:11

Spring Batch Example – Hello World Project

Create a Mavenproject by Eclipse

New Project- Maven– QuickStart – GroupID(SpringBatchHelloWorld),articficateID(com.ermdashboard.SpringBatchHelloWorld)

Src/main/java/com.ermdashboard.SpringBatchHelloWorld,create Java Code:

packagecom.ermdashboard.SpringBatchHelloWorld;

 

import org.springframework.batch.core.Job;

importorg.springframework.batch.core.JobExecution;

import org.springframework.batch.core.JobParameters;

importorg.springframework.batch.core.launch.JobLauncher;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

 

public class JobLaunch {

      /**

     * @param args

     */

    public static void main(String[] args) {

      String[]springConfig  =

                  {     "application.xml",

                  "batch.xml"

                  };

        ApplicationContext context = newClassPathXmlApplicationContext(springConfig);

        JobLauncher launcher = (JobLauncher)context.getBean("jobLauncher");

        Job job = (Job) context.getBean("helloWorldJob");

 

        try {

            /* 运行Job */

            JobExecution result =launcher.run(job, newJobParameters());

            /* 处理结束,控制台打印处理结果 */

            System.out.println(result.toString());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

 

packagecom.ermdashboard.SpringBatchHelloWorld;

 

importorg.springframework.batch.core.StepContribution;

importorg.springframework.batch.core.scope.context.ChunkContext;

importorg.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;

    }

 

    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)

            throws Exception {

        System.out.println(message);

        return RepeatStatus.FINISHED;

    }

 

}

 

Pom.xml

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>

 

 <groupId>com.ermdashboard</groupId>

  <artifactId>SpringBatchHelloWorld</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

 <name>SpringBatchHelloWorld</name>

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

 

  <properties>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <jdk.version>1.6</jdk.version>

    <spring.version>3.2.2.RELEASE</spring.version>

    <spring.batch.version>2.2.0.RELEASE</spring.batch.version>

  </properties>

 

 

 

  <dependencies>

        <!-- Spring Core -->

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-core</artifactId>

                  <version>${spring.version}</version>

            </dependency>

 

            <!-- Spring jdbc, for database -->

            <dependency>

                  <groupId>org.springframework</groupId>

                  <artifactId>spring-jdbc</artifactId>

                  <version>${spring.version}</version>

            </dependency>

 

            <!-- Spring Batch dependencies-->

            <dependency>

                  <groupId>org.springframework.batch</groupId>

                  <artifactId>spring-batch-core</artifactId>

                  <version>${spring.batch.version}</version>

            </dependency>

            <dependency>

                  <groupId>org.springframework.batch</groupId>

                  <artifactId>spring-batch-infrastructure</artifactId>

                  <version>${spring.batch.version}</version>

            </dependency>

            <dependency>

    <groupId>com.oracle</groupId>

   <artifactId>ojdbc14</artifactId>

    <version>10.2.0.3.0</version>

</dependency>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

  

  </dependencies>

 

   <build>

            <finalName>spring-batch</finalName>

            <plugins>

             <plugin>

                  <groupId>org.apache.maven.plugins</groupId>

                  <artifactId>maven-eclipse-plugin</artifactId>

                  <version>2.9</version>

                  <configuration>

                        <downloadSources>true</downloadSources>

                        <downloadJavadocs>false</downloadJavadocs>

                  </configuration>

             </plugin>

             <plugin>

                  <groupId>org.apache.maven.plugins</groupId>

                  <artifactId>maven-compiler-plugin</artifactId>

                  <version>2.3.2</version>

                  <configuration>

                        <source>${jdk.version}</source>

                        <target>${jdk.version}</target>

                  </configuration>

             </plugin>

            </plugins>

      </build>

 

</project>

 

Src/main/java目录下

Application.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:p="http://www.springframework.org/schema/p"

   xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

http://www.springframework.org/schema/aop 

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-2.5.xsd"

    default-autowire="byName">

 

    <bean id="jobLauncher"class="org.springframework.batch.core.launch.support.SimpleJobLauncher">

        <propertyname="jobRepository" ref="jobRepository"/>

    </bean>

 

    <bean id="jobRepository"class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>

 

    <bean id="transactionManager"class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

</beans>

 

Batch.xml

<?xml version="1.0"encoding="UTF-8"?>

<bean:beans xmlns="http://www.springframework.org/schema/batch"

   xmlns:bean="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"

   xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/batch

http://www.springframework.org/schema/batch/spring-batch.xsd">

 

    <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>

 

    <bean:bean id="hello"class="com.ermdashboard.SpringBatchHelloWorld.writeTasklet">

        <bean:propertyname="message" value="Hello "></bean:property>

    </bean:bean>

 

    <bean:bean id="world"class="com.ermdashboard.SpringBatchHelloWorld.writeTasklet">

        <bean:propertyname="message" value=" World!"></bean:property>

    </bean:bean>

</bean:beans>

 

Run JobLaunch将逐个调用配置在job helloWorldJob中的step:step_hello和step_world,

Aug 12,2013 5:14:48 PM org.springframework.context.support.AbstractApplicationContextprepareRefresh

INFO:Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@89fbe3:startup date [Mon Aug 12 17:14:48 CST 2013]; root of context hierarchy

Aug 12,2013 5:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions

INFO:Loading XML bean definitions from class path resource [application.xml]

Aug 12, 20135:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions

INFO:Loading XML bean definitions from class path resource [batch.xml]

Aug 12,2013 5:14:48 PM org.springframework.beans.factory.support.DefaultListableBeanFactoryregisterBeanDefinition

INFO:Overriding bean definition for bean 'helloWorldJob': replacing [Generic bean:class [org.springframework.batch.core.configuration.xml.SimpleFlowFactoryBean];scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0;autowireCandidate=true; primary=false; factoryBeanName=null;factoryMethodName=null; initMethodName=null; destroyMethodName=null] with[Generic bean: class[org.springframework.batch.core.configuration.xml.JobParserJobFactoryBean];scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0;autowireCandidate=true; primary=false; factoryBeanName=null;factoryMethodName=null; initMethodName=null; destroyMethodName=null]

Aug 12,2013 5:14:48 PM org.springframework.beans.factory.support.DefaultListableBeanFactorypreInstantiateSingletons

INFO:Pre-instantiating singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@aa37a6:defining beans[jobLauncher,jobRepository,transactionManager,org.springframework.batch.core.scope.internalStepScope,org.springframework.beans.factory.config.CustomEditorConfigurer,org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor,step_hello,step_world,helloWorldJob,hello,world];root of factory hierarchy

Aug 12,2013 5:14:48 PM org.springframework.batch.core.launch.support.SimpleJobLauncherafterPropertiesSet

INFO: NoTaskExecutor has been set, defaulting to synchronous executor.

Aug 12,2013 5:14:49 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1run

INFO:Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters:[{}]

Aug 12,2013 5:14:49 PM org.springframework.batch.core.job.SimpleStepHandler handleStep

INFO:Executing step: [step_hello]

Hello

Aug 12,2013 5:14:49 PM org.springframework.batch.core.job.SimpleStepHandler handleStep

INFO:Executing step: [step_world]

 World!

Aug 12,2013 5:14:49 PMorg.springframework.batch.core.launch.support.SimpleJobLauncher$1 run

INFO:Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters:[{}] and the following status: [COMPLETED]

JobExecution:id=0, version=2, startTime=Mon Aug 12 17:14:49 CST 2013, endTime=Mon Aug 1217:14:49 CST 2013, lastUpdated=Mon Aug 12 17:14:49 CST 2013, status=COMPLETED,exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=0,version=0, Job=[helloWorldJob]], jobParameters=[{}]

 

原创粉丝点击