我有一个可以运行作业的组件:
当前构建的job参数是固定的,但是不同的feed应该提供他们自己的将请求映射到JobParameters
的方式,因此我想出了一个通用的Mapper
接口和一个{{ 1}}接口,应将RequestMapper
映射到MyRequest
JobParameters
问题是我有@Component
public class MyJobRunner {
private JobLauncher jobLauncher;
private JobExplorer jobExplorer;
private JobLocator jobLocator;
private RequestMapper requestMapper;
@Autowired
public MyJobRunner(JobLocator jobLocator,
JobExplorer jobExplorer,
JobLauncher jobLauncher,
RequestMapper requestMapper) {
this.jobLocator = jobLocator;
this.jobExplorer = jobExplorer;
this.jobLauncher = jobLauncher;
this.requestMapper = requestMapper;
}
public MyResponse run(final MyRequest request, final String id) {
JobParameters parameters = getJobParameters(request, id);
}
private JobParameters getJobParameters(MyRequest request, String id) {
//current
JobParametersBuilder parameters = new JobParametersBuilder();
parameters.addString("name", request.getValue(NAME));
parameters.addString("date", request.getValue(DATE));
parameters.addString(ID.getKeyName(), id);
return parameters.toJobParameters();
//target is replace the above with requestMapper.map(...) to transform request to JobParameters through the interface
}
}
public interface Mapper<F, T> {
T map(F from);
}
public interface RequestMapper extends Mapper {
}
字段,该字段也应在JobParameters中,但不在id
对象中。在那种情况下,我应该如何设计MyRequest
接口,然后设计RequestMapper?我不确定是否可以将HashMap用作通用类型:
RequestMapper
其中键是id,值是请求对象。但是也许有一种更清洁的方法可以实现这一目标。