春季批处理向现有作业参数中添加新参数

huangapple go评论80阅读模式
英文:

spring batch add new parameter to existing job parameters

问题

以下是您要翻译的内容:

我有一个接口实现,将我的请求映射到 JobParameters

@Component("myMapper")
public class MyMapper implements RequestMapper {

    @Override
    public JobParameters map(Request request) {
        JobParametersBuilder parameters = new JobParametersBuilder();
        parameters.addString("name", request.getValue(NAME));
        parameters.addString("test", request.getValue(TEST));
        return parameters.toJobParameters();
    }
}

在我的 JobRunner 类中,在执行作业之前,我必须添加一个不在 Request 对象中的附加参数:

public Response runJob(final Request request, final String id) {
    Job job = getJob(request);
    JobParameters parameters = myMapper.map(request);

    // 我想将 id 添加到参数中
    JobExecution execution;

        execution = jobLauncher.run(job, parameters);

}

如何添加这个新参数?

英文:

I have an interface implementation that maps my request to JobParameters

@Component("myMapper")
public class MyMapper implements RequestMapper {

    @Override
    public JobParameters map(Request request) {
        JobParametersBuilder parameters = new JobParametersBuilder();
        parameters.addString("name", request.getValue(NAME));
        parameters.addString("test", request.getValue(TEST));
        return parameters.toJobParameters();
    }
}

In my JobRunner class, I have to add an additional param that is not in Request Object before executing the job:

public Response runJob(final Request request, final String id) {
    Job job = getJob(request);
    JobParameters parameters = myMapper.map(request);

    //i want to add id to parameters
    JobExecution execution;

        execution = jobLauncher.run(job, parameters);

}

How can I add this new param?

答案1

得分: 3

JobParameters中没有直接添加参数的方法,您需要使用JobParametersBuilder

您需要从现有的JobParameters构建JobParametersBuilder,然后在构建器中添加参数,再从构建器中获取JobParameters

JobParameters parameters = myMapper.map(request);
JobParametersBuilder parametersBuilder = new JobParametersBuilder(parameters);
parametersBuilder.addString("id", id);
JobParameters newParameters = parametersBuilder.toJobParameters();
英文:

There is no method to add a parameter in JobParameters directly, you need to use JobParametersBuilder.

You need to construct JobParametersBuilder from existing JobParameters and add the parameter in builder and again get JobParameters from builder.

JobParameters parameters = myMapper.map(request);
JobParametersBuilder parametersBuilder = new JobParametersBuilder(parameters);
parametersBuilder.addString("id", id);
JobParameters newParameters = parametersBuilder.toJobParameters();

huangapple
  • 本文由 发表于 2020年9月5日 20:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63753677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定