英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论