spring-batch参数传递

曹兴贤
2023-12-01

1.JobParameters

在调用JOB的时候设置JOB的全局参数,可在JOB执行过程的各个地方获取。

如下代码所示,在调用JOB是设置JobParameters

package com.flight.neon.batch.demo.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoJobTest {

    @Autowired
    SimpleJobLauncher jobLauncher;

    @Autowired
    Job userJob;

    @Test
    public void demoTest() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        String batchId = "test-model";
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("batchId", batchId)
                .toJobParameters();
        jobLauncher.run(userJob, jobParameters);
    }

}

在process中可以通过注解的方式获取参数值(JOB中的其他步骤也可通过这种方法获取参数值)

package com.flight.neon.batch.demo.job.processor;

import com.flight.neon.batch.demo.job.entity.User;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author 这个码农不太萌
 */
@Component
public class DemoProcess {

    @StepScope
    @Bean("userProcess")
    public ItemProcessor<User, User> userProcess(@Value("#{jobParameters['batchId']}") String batchId) {
        ItemProcessor<User, User> itemProcessor = ehrOrg -> {
            //数据处理
            return ehrOrg;
        };
        return itemProcessor;
    }

}

2.jobExecutionContext

jobExecutionContext是全局的job上下文数据,可以通过jobExecution或者value注解的方式获取

如下给JOB定义一个监听,并在监听中塞值取值

package com.flight.neon.batch.demo.job.listener;

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

/**
 * @author 这个码农不太萌
 */
@Component
public class DemoJobListener implements JobExecutionListener {
    @Override
    public void beforeJob(JobExecution jobExecution) {
        jobExecution.getExecutionContext().put("test-job-context","aaaaa");
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        System.out.println(jobExecution.getExecutionContext().get("test-job-context"));
    }
}

如下在process中通过value注解取值

package com.flight.neon.batch.demo.job.processor;

import com.flight.neon.batch.demo.job.entity.User;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author 这个码农不太萌
 */
@Component
public class DemoProcess {

    @StepScope
    @Bean("userProcess")
    public ItemProcessor<User, User> userProcess(@Value("#{jobParameters['batchId']}") String batchId,
                                                 @Value("#{jobExecutionContext['test-job-context']}") String jobContext) {
        System.out.println("jobParameters is" + batchId);
        System.out.println("jobExecution is" + jobContext);
        ItemProcessor<User, User> itemProcessor = ehrOrg -> {
            //数据处理
            return ehrOrg;
        };
        return itemProcessor;
    }

}

3.stepExecutionContext

stepExecutionContext与jobExecutionContext基本类似,只不过stepExecutionContext是在单个step中可见的,jobExecutionContext是job全局可见的

总结:

spirng-batch传值基本就这三种,按业务场景需要使用。需要注意的是,每个值获取的代码链路必须加上@StepScope注解才能进行注解形式的值获取

 类似资料: