如何将JobParameters
传递给MongoItemReader
查询?
我的ItemReader看起来像: -
@Bean
public ItemReader<Person> PersonTenantBasedItemReader() {
MongoItemReader<Person> reader = new MongoItemReader<Person>();
reader.setTemplate(mongoTemplate);
reader.setTargetType((Class<? extends Person>) Person.class);
reader.setQuery("{status:'XYZ',nextCheckpointDate:{$gte:?fromDate,$lte:?toDate}"); // !!!!I want to pass fromDate and toDate as job parameters. !!!!
Map<String, Direction> sorts = new HashMap<String, Sort.Direction>(1);
sorts.put("status", Sort.Direction.ASC);
reader.setSort(sorts);
return reader;
}
这就是我发布工作的方式: -
@Test
public void test() throws Exception {
Map<String, JobParameter> map = new HashMap<String, JobParameter>(2);
map.put("fromDate", new JobParameter(new Date()));
map.put("toDate", new JobParameter(//some future date comes here);
JobParameters jobParameters = new JobParameters(map);
JobExecution run = launcher.run(job, jobParameters);
Assert.assertNotNull(run);
}
我得到了这个例外: -
com.mongodb.util.JSONParseException:
{status:'DELINQUENT',nextCheckpointDate:{$gte:?fromDate,$lte:?toDate}
^
at com.mongodb.util.JSONParser.parse(JSON.java:216)
at com.mongodb.util.JSONParser.parseObject(JSON.java:249)
at com.mongodb.util.JSONParser.parse(JSON.java:213)
at com.mongodb.util.JSONParser.parseObject(JSON.java:249)
at com.mongodb.util.JSONParser.parse(JSON.java:213)
at com.mongodb.util.JSONParser.parse(JSON.java:163)
at com.mongodb.util.JSON.parse(JSON.java:99)
at com.mongodb.util.JSON.parse(JSON.java:79)
at org.springframework.data.mongodb.core.query.BasicQuery.<init>(BasicQuery.java:39)
at org.springframework.batch.item.data.MongoItemReader.doPageRead(MongoItemReader.java:176)
at org.springframework.batch.item.data.AbstractPaginatedDataItemReader.doRead(AbstractPaginatedDataItemReader.java:59)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:155)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:114)
答案 0 :(得分:3)
我在配置中看到两个问题:
JobParameter
值,您需要使用步骤范围。您的bean当前使用单例范围配置(默认)。尝试以下内容:
@Bean
@StepScope
public MongoItemReader<Person> PersonTenantBasedItemReader(@Value("#{jobParameters[fromDate]}") String fromDate, @Value("#{jobParameters[toDate]}") String toDate) {
MongoItemReader<Person> reader = new MongoItemReader<Person>();
reader.setTemplate(mongoTemplate);
reader.setTargetType((Class<? extends Person>) Person.class);
reader.setQuery(String.format("{status:'XYZ',nextCheckpointDate:{ %s ? %s }", fromDate, toDate));
Map<String, Direction> sorts = new HashMap<String, Sort.Direction>(1);
sorts.put("status", Sort.Direction.ASC);
reader.setSort(sorts);
return reader;
}
答案 1 :(得分:0)
List<Object> param = new ArrayList<Object>();
param.add(fromDate);
param.add(toDate)
reader.setQuery("{status:'XYZ',nextCheckpointDate:{$gte:?0,$lte:?1}");