I have a spring batch application as follows where I need to read two files daily and process:
MarketAFile_yyyy_mm_dd.csv
MarketBFile_yyyy_mm_dd.csv
I have configured Job
which first needs to fetch these files from the fileshare dynamically based on the date:
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.listener(listener())
.start(getFiles())
.next(step1())
.build();
}
@Bean
public Step getFiles() {
return stepBuilderFactory.get("getFiles")
.tasklet(fileMovingTasklet)
.build();
}
My FileMovingTasklet
execute()
method needs to access jobParameters
which should be the name of the file (derived from enum filename
) and the corresponding previousWorkingDate
for that market. I am iterating over the list of markets as you can see below and want to dynamically set the filename and corresponding date as to build final filename for example:
MarketAFile_2018_02_15.csv
MarketBFile_2018_02_15.csv
How can I pass this final file name so I have it avaialble in execute()
to perform a copy from fileshare to my local path?
@Component
public class FileMovingTasklet implements Tasklet, InitializingBean {
@Value("${file.suffix}")
private String suffix;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
try {
//get files to look for, for all markets
//copy from file share to local
} catch (IOException e) {
}
return RepeatStatus.FINISHED;
}
private void copyFiles(...) {
}
}
}
Here is my Main entry point:
@SpringBootApplication
@EnableBatchProcessing
public class App implements CommandLineRunner {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@Autowired
private PropertyHolder propertyHolder;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
public void run(String... strings) throws Exception {
for (Market market : Market.values()) {
List<MonthDay> listOfHolidays = propertyHolder.getHolidayMap(market);
if (todayIsHoliday(listOfHolidays)) {
String previousWorkingDay = getPreviousWorkingDay(listOfHolidays); //2018_02_15
}
}
// JobParameters jobParameters = buildJobParameters();
// jobLauncher.run(job, jobParameters);
}
private JobParameters buildJobParameters() {
Map<String, JobParameter> confMap = new HashMap<>();
confMap.put(AS_OF_DATE, new JobParameter(new Date()));
return new JobParameters(confMap);
}
}
Enum class:
public enum Market {
MARKETA("MarketA", "MarketAFile"),
MARKETB("MarketA", "MarketBFile");
private final String market;
private final String fileName;
Market(String market, String filename) {
this.market = market;
this.fileName = filename;
}
}