真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)

一.Job的創(chuàng)建及其應(yīng)用
1.Job flow的介紹:
(1)狀態(tài)機(jī):例完成step1,是否繼續(xù)完成step2,step3,我們就需要通過(guò)Job flow來(lái)控制
(2)進(jìn)行演示:使用next()方法來(lái)達(dá)到順序執(zhí)行step1,step2...的目的,再使用on(),to(),from()方法達(dá)到與next()方法同樣的目的,再展示fail()方法和stopAndRestart()方法;
例1:創(chuàng)建JobFlowDemoOne,以及三個(gè)Step使用next()讓其順序執(zhí)行
JobFlowDemOne:

我們擁有十余年網(wǎng)頁(yè)設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁(yè)設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序制作、手機(jī)網(wǎng)站制作、H5開(kāi)發(fā)、等業(yè)務(wù)。無(wú)論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。

package com.dhcc.batch.batchDemo.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JobFlowDemOne {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job JobFlowDemo1() {
        return jobBuilderFactory.get("JobFlowDemo1").start(step1()).next(step2()).next(step3()).build();

    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step1-->Hello Spring Batch....");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2").tasklet((contribution, context) -> {
            System.out.println("step 2-->Hello Spring Batch..");
            return RepeatStatus.FINISHED;
        }).build();

    }

    @Bean
    public Step step3() {
        return stepBuilderFactory.get("step3").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("step3-->Hello Spring Batch....");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }
}

在step2()中,我們使用了java8的新特性,使用了Lambda表達(dá)式

package com.dhcc.batch.batchDemo;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class BatchDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchDemoApplication.class, args);
    }
}

開(kāi)始執(zhí)行,執(zhí)行結(jié)果為:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
觀察控制臺(tái)執(zhí)行結(jié)果,我們可以發(fā)現(xiàn)Step1,step2,step3,按照next()的順序順序執(zhí)行完畢,此時(shí)我們?cè)谶M(jìn)入數(shù)據(jù)庫(kù)中進(jìn)行觀察,我們主要觀察JobExecution表以及StepExecution表,查看其表數(shù)據(jù):
JobExecution:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
StepExecution:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
在查看一個(gè)JobInstance,觀察Job實(shí)例:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
例2:我們使用on(),to(),from()方法順序執(zhí)行我們的Step,達(dá)到與例1相同的效果
原代碼不變,我們將JobFlowDemo1略作修改(注:我在這里為例方便演示新建了一個(gè)數(shù)據(jù)庫(kù)):

@Bean
    public Job JobFlowDemo2() {
        return jobBuilderFactory.get("JobFlowDemo2")
//              .start(step1())
//              .next(step2())
//              .next(step3())
                .start(step1()).on("COMPLETED").to(step2())
                .from(step2()).on("COMPLETED").to(step3())
                .from(step3()).end()
                .build();

觀察控制臺(tái):
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
代碼執(zhí)行成功;
fail()方法和stopAndRestart()方法后面用到的時(shí)候我們?cè)谧鲈敿?xì)了解

二.Flow的創(chuàng)建及使用
1.flow的介紹:
(1)flow是一個(gè)Step的集合,他規(guī)定了Step與Step之間的轉(zhuǎn)換關(guān)系;
(2)創(chuàng)建Flow可以達(dá)到復(fù)用的效果,讓其在不同的Job之間進(jìn)行復(fù)用;
(3)使用FlowBuilder去創(chuàng)建一個(gè)Flow,他和Job類似,使用start(),next()以及end()來(lái)運(yùn)行flow;
例:
創(chuàng)建JobFlowDemoTwoApplication

package com.dhcc.batch.batchDemo.jobFlowDemoTwo;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class JobFlowDemoTwoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JobFlowDemoTwoApplication.class, args);
    }
}

創(chuàng)建step,flow以及Job——》JobFlowDemoTwoConfiguration:

package com.dhcc.batch.batchDemo.jobFlowDemoTwo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JobFlowDemoTwoConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step jobFlowDemoTwo1() {
        return stepBuilderFactory.get("jobFlowDemoTwo1").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello-->jobFlowDemoTwo1");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step jobFlowDemoTwo2() {
        return stepBuilderFactory.get("jobFlowDemoTwo2").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello-->jobFlowDemoTwo2");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step jobFlowDemoTwo3() {
        return stepBuilderFactory.get("jobFlowDemoTwo3").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello-->jobFlowDemoTwo3");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    // 創(chuàng)建Flow,它是一個(gè)Step集合
    @Bean
    public Flow jobFlowDemoFlow() {
        return new FlowBuilder("jobFlowDemoFlow")
                .start(jobFlowDemoTwo1())
                .next(jobFlowDemoTwo2())
                .build();

    }

    //創(chuàng)建一個(gè)Job執(zhí)行Flow以及Step
    @Bean
    public Job jobFlowDemoTwoJob() {
        return  jobBuilderFactory.get("jobFlowDemoTwoJob")
                .start(jobFlowDemoFlow())
                .next(jobFlowDemoTwo3())
                .end()
                .build();

    }

}

開(kāi)始運(yùn)行,我們觀察控制臺(tái):
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
運(yùn)行結(jié)果我們可以看見(jiàn)Job先運(yùn)行了Flow中的兩個(gè)Step,然后再運(yùn)行jobFlowDemoStep3(),與我們?cè)O(shè)定的順序一樣
在數(shù)據(jù)庫(kù)中,我們以stepExection表為例觀察:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
看見(jiàn)了3個(gè)Step順序執(zhí)行完成;
三.Spilt多線程并發(fā)任務(wù)定義及使用
1.Spilt異步執(zhí)行Flow
2.舉例說(shuō)明:
(1)首先定義兩個(gè)Flow,在每個(gè)Flow中定義一些Step,每一個(gè)Step將自身的名字以及當(dāng)前運(yùn)行的線程打印出來(lái);
(2)創(chuàng)建一個(gè)Job使用Spilt異步的啟動(dòng)兩個(gè)Flow;
(3)運(yùn)行Job,查看結(jié)果;
例:
JobSpiltDemoApplication:

package com.dhcc.batch.batchDemo.jobSpiltDemo;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class JobSpiltDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JobSpiltDemoApplication.class, args);
    }
}

JobSpiltDemoConfiguration:

package com.dhcc.batch.batchDemo.jobSpiltDemo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

@Configuration
public class JobSpiltDemoConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 創(chuàng)建job運(yùn)行Flow,我們利用split(new
     * SimpleAsyncTaskExecutor()).add()讓flow異步執(zhí)行,add()中可以添加多個(gè)Flow
     * 
     * @return
     */
    @Bean
    public Job SpiltJob() {
        return jobBuilderFactory.get("SpiltJob").start(jobSpiltFlow1()).split(new SimpleAsyncTaskExecutor())
                .add(jobSpiltFlow2()).end().build();

    }

    // 創(chuàng)建Flow1
    @Bean
    public Flow jobSpiltFlow1() {
        return new FlowBuilder("jobSpiltFlow1")
                .start(stepBuilderFactory.get("jobSpiltStep1").tasklet(tasklet()).build())
                .next(stepBuilderFactory.get("jobSpiltStep2").tasklet(tasklet()).build()).build();

    }

    // 創(chuàng)建Flow1
    @Bean
    public Flow jobSpiltFlow2() {
        return new FlowBuilder("jobSpiltFlow2")
                .start(stepBuilderFactory.get("jobSpiltStep3").tasklet(tasklet()).build())
                .next(stepBuilderFactory.get("jobSpiltStep4").tasklet(tasklet()).build()).build();

    }

    private Tasklet tasklet() {
        return new PrintTasklet();
    }

    // step執(zhí)行的任務(wù)類(可以寫(xiě)為外部類,此處為了方便,我們寫(xiě)為內(nèi)部類)
    private class PrintTasklet implements Tasklet {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            System.out.println("has been execute on stepName:" + chunkContext.getStepContext().getStepName()
                    + ",has been execute on thread:" + Thread.currentThread().getName());
            return RepeatStatus.FINISHED;
        }

    }

}

運(yùn)行結(jié)果:

Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
觀察控制臺(tái),我們可以看出我們達(dá)到了讓Step異步執(zhí)行的目的
觀察數(shù)據(jù)庫(kù)表:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)

四.決策器的定義及應(yīng)用
1.Decision:為我們提供下一步執(zhí)行哪一個(gè)Step提供條件決策
2.JobExecutionDecider:接口,提供決策條件
例:

package com.dhcc.batch.batchDemo.flowDecisionDemo;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class FlowDecisionDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(FlowDecisionDemoApplication.class, args);
    }
}


FlowDecisionDemoConfiguration:

package com.dhcc.batch.batchDemo.flowDecisionDemo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlowDecisionDemoConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    //編寫(xiě)Job
    @Bean
    public Job FlowDecisionJob() {
        return jobBuilderFactory.get("FlowDecisionJob")
                .start(firstStep()).next(myDecider())
                .from(myDecider()).on("EVEN").to(evenStep())
                .from(myDecider()).on("ODD").to(oddStep())
                .from(oddStep()).on("*").to(myDecider())
                .end()
                .build();

    }
    @Bean
    public Step firstStep() {
        return stepBuilderFactory.get("firstStep").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello firstStep..");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step evenStep() {
        return stepBuilderFactory.get("evenStep").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello evenStep..");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step oddStep() {
        return stepBuilderFactory.get("oddStep").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello oddStep..");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    //編寫(xiě)決策器
    @Bean
    public JobExecutionDecider myDecider() {
        return new myDecider();

    }

}

myDecider:

package com.dhcc.batch.batchDemo.flowDecisionDemo;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;

public class myDecider implements JobExecutionDecider {
    private int count=0;

    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        count++;
        if(count%2==0) {
            return new FlowExecutionStatus("EVEN");
        }else {
            return new FlowExecutionStatus("ODD");
        }
    }

}

運(yùn)行結(jié)果:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
我們觀察控制臺(tái),分析結(jié)果:首先job中先運(yùn)行firstStep(),然后進(jìn)入到myDecider中Count++,此時(shí)count=1,返回”O(jiān)DD”,job中執(zhí)行oddStep(),然后無(wú)論什么狀態(tài)再次進(jìn)入myDecider中,此時(shí)count=2,故返回”EVEN”,下一步執(zhí)行evenStep();
五.Job的嵌套定義及應(yīng)用
1.job可以嵌套使用,嵌套的Job我們將其稱為子job,被嵌套的Job我們將其稱為父job;
2.一個(gè)父Job可以有多個(gè)子Job;
3.子job不能單獨(dú)運(yùn)行,需要其父Job去啟動(dòng);
例:
NestedJobApplication:

package com.dhcc.batch.batchDemo.nestedJob;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class NestedJobApplication {

    public static void main(String[] args) {
        SpringApplication.run(NestedJobApplication.class, args);
    }
}

接下來(lái)建立兩個(gè)子Job,每個(gè)子job中有兩個(gè)Step,一個(gè)父job
讓父job控制運(yùn)行子job

ChildJobOneConfiguration:

package com.dhcc.batch.batchDemo.nestedJob;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChildJobOneConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job childJob1() {
        return jobBuilderFactory.get("childJob1")
                .start(childJob1Step1())
                .next(childJob1Step2())
                .build();

    }

    @Bean
    public Step childJob1Step1() {
        return stepBuilderFactory.get("childJob1Step1").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello---->childJob1Step1");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step childJob1Step2() {
        return stepBuilderFactory.get("childJob1Step2").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello---->childJob1Step2");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

}

ChildJobTwoConfiguration:

package com.dhcc.batch.batchDemo.nestedJob;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChildJobTwoConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job childJob2() {
        return jobBuilderFactory.get("childJob2")
                .start(childJob2Step1())
                .next(childJob2Step2())
                .build();

    }

    @Bean
    public Step childJob2Step1() {
        return stepBuilderFactory.get("childJob2Step1").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello---->childJob2Step1");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

    @Bean
    public Step childJob2Step2() {
        return stepBuilderFactory.get("childJob2Step2").tasklet(new Tasklet() {

            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println("Hello---->childJob2Step2");
                return RepeatStatus.FINISHED;
            }
        }).build();

    }

}

ParentJobConfiguration:

package com.dhcc.batch.batchDemo.nestedJob;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class ParentJobConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private Job childJob1;
    @Autowired
    private Job childJob2;
    @Autowired
    private JobLauncher jobLauncher;

    @Bean
    public Job parentJob(JobRepository repository,PlatformTransactionManager transactionManager) {
        return jobBuilderFactory.get("parentJob")
                .start(parentJobStep())
                .next(childJob1(repository, transactionManager))
                .next(childJob2(repository, transactionManager))
                .build();
    }

    private Step childJob1(JobRepository repository,PlatformTransactionManager transactionManager) {
        return new JobStepBuilder(new StepBuilder("childJob1"))
                .job(childJob1)
                .launcher(jobLauncher)
                .repository(repository)
                .transactionManager(transactionManager)
                .build();
    }

    private Step childJob2(JobRepository repository,PlatformTransactionManager transactionManager) {
        return new JobStepBuilder(new StepBuilder("childJob2"))
                .job(childJob2)
                .launcher(jobLauncher)
                .repository(repository)
                .transactionManager(transactionManager)
                .build();
    }

    @Bean
    public Step parentJobStep() {
        return stepBuilderFactory.get("parentJobStep")
                .tasklet(new Tasklet() {

                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("Helllo------>parentJobStep..");
                        return RepeatStatus.FINISHED;
                    }
                }).build();

    }

}

在配置文件中添加

spring.datasource.url=jdbc:MySQL://localhost:3306/springbatch?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=qitao1996
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql

spring.batch.initialize-schema=always

spring.batch.job.names=parentJob

運(yùn)行結(jié)果:

Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)

觀察控制臺(tái),成功運(yùn)行,我們達(dá)到了job嵌套的效果
六.監(jiān)聽(tīng)器的定義及應(yīng)用
1.Listener:控制Job執(zhí)行的一種方式
2.可以通過(guò)接口或者注解實(shí)現(xiàn)監(jiān)聽(tīng)器
3.在spring-batch中提供各個(gè)級(jí)別的監(jiān)聽(tīng)器接口,從job級(jí)別到item級(jí)別都有
(1)JobExecutionListener(before..,after..);
(2)StepExecutionListener(before..,after..);
(3)ChunkListener(before..,after..);
(4)ItemReaderListener;ItemWriterListener;ItemProcessListener(before..,after..,error..);
例:
ListenerJobApplication :

package com.dhcc.batch.batchDemo.listenerJob;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class ListenerJobApplication {

    public static void main(String[] args) {
        SpringApplication.run(ListenerJobApplication.class, args);
    }
}

創(chuàng)建監(jiān)聽(tīng)器(通過(guò)實(shí)現(xiàn)接口實(shí)現(xiàn))
MyJobListener:

package com.dhcc.batch.batchDemo.listenerJob;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

/**
 * 實(shí)現(xiàn)接口構(gòu)建監(jiān)聽(tīng)器
 * @author Administrator
 *
 */
public class MyJobListener implements JobExecutionListener{

    @Override
    public void beforeJob(JobExecution jobExecution) {
        System.out.println(jobExecution.getJobInstance().getJobName()+"before running......");
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        System.out.println(jobExecution.getJobInstance().getJobName()+"before running......");
    }

}

創(chuàng)建監(jiān)聽(tīng)器(通過(guò)注解實(shí)現(xiàn))
MyChunkListener:

package com.dhcc.batch.batchDemo.listenerJob;

import org.springframework.batch.core.annotation.AfterChunk;
import org.springframework.batch.core.annotation.BeforeChunk;
import org.springframework.batch.core.scope.context.ChunkContext;
/**
 * 使用注解構(gòu)建監(jiān)聽(tīng)器
 * @author Administrator
 *
 */
public class MyChunkListener {

    @BeforeChunk
    public void beforeChunk(ChunkContext context) {
        System.out.println(context.getStepContext().getStepName()+"chunk before running.....");
    }

    @AfterChunk
    public void afterChunk(ChunkContext context) {
        System.out.println(context.getStepContext().getStepName()+"chunk after running.....");
    }

}

MyListenerJobConfiguration:

package com.dhcc.batch.batchDemo.listenerJob;

import java.util.Arrays;
import java.util.List;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyListenerJobConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    //監(jiān)聽(tīng)Job執(zhí)行
    @Bean
    public Job myListenerJob() {
        return jobBuilderFactory.get("myListenerJob")
                .start(myListenerStep())
                .listener(new MyJobListener())  
                .build();

    }

    private Step myListenerStep() {
        return stepBuilderFactory.get("myListenerStep")
                .chunk(2)
                .faultTolerant()
                .listener(new MyChunkListener())
                .reader(reader())
                .writer(writer())
                .build();
    }
    private ItemReader reader() {
        return new ListItemReader<>(Arrays.asList("maozedong","zhude","pendehuai","zhouenlai","liushaoqi"));
    }

    private ItemWriter writer() {
        return new ItemWriter() {

            @Override
            public void write(List items) throws Exception {
                for(String item:items) {
                    System.out.println("Writing item: "+item);
                }

            }
        };
    }

}

運(yùn)行結(jié)果:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
觀察控制臺(tái),我們成功的完成了監(jiān)聽(tīng)任務(wù)..
七.Job參數(shù)初探
1.JobParameters作用:在Job運(yùn)行過(guò)程中,可以用來(lái)傳遞信息
2.通過(guò)”key---->value”鍵值對(duì)的形式傳入,在代碼中我們通過(guò)get(“key”)來(lái)獲取value值
例:
JobParametersDemoApplication:

package com.dhcc.batch.batchDemo.jobParametersDemo;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableBatchProcessing
public class JobParametersDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JobParametersDemoApplication.class, args);
    }
}

創(chuàng)建Job,Step:
JobParametersConfiguretion:

package com.dhcc.batch.batchDemo.jobParametersDemo;

import java.util.Map;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JobParametersConfiguretion implements StepExecutionListener{

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    private Map parames;

    @Bean
    public Job MyParametersJobThree() {
        return jobBuilderFactory.get("MyParametersJobThree")
                .start(MyParametersJobStep3())
                .build();   
    }

    private Step MyParametersJobStep3() {

        return stepBuilderFactory.get("MyParametersJobStep3")
                .listener(this)
                .tasklet(new Tasklet() {

                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("parame is: "+parames.get("info"));
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println(stepExecution.getStepName()+"運(yùn)行之前...........");
        parames = stepExecution.getJobParameters().getParameters();
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        System.out.println(stepExecution.getStepName()+"運(yùn)行之完畢...........");
        return null;
    }
}

運(yùn)行結(jié)果:
Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
觀察控制臺(tái),此處我們沒(méi)有進(jìn)行傳參;


網(wǎng)站欄目:Spring-batch學(xué)習(xí)總結(jié)(2)—Job,Flow創(chuàng)建及應(yīng)用,多線程并發(fā),決策器,監(jiān)聽(tīng)器,參數(shù)
分享網(wǎng)址:http://weahome.cn/article/gojcji.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部