how can pass jobparameters
mongoitemreader
query ?
my itemreader looks :-
@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 pass fromdate , todate job parameters. !!!! map<string, direction> sorts = new hashmap<string, sort.direction>(1); sorts.put("status", sort.direction.asc); reader.setsort(sorts); return reader; }
this how launching job:-
@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); }
i getting exception:-
com.mongodb.util.jsonparseexception: {status:'delinquent',nextcheckpointdate:{$gte:?fromdate,$lte:?todate} ^ @ com.mongodb.util.jsonparser.parse(json.java:216) @ com.mongodb.util.jsonparser.parseobject(json.java:249) @ com.mongodb.util.jsonparser.parse(json.java:213) @ com.mongodb.util.jsonparser.parseobject(json.java:249) @ com.mongodb.util.jsonparser.parse(json.java:213) @ com.mongodb.util.jsonparser.parse(json.java:163) @ com.mongodb.util.json.parse(json.java:99) @ com.mongodb.util.json.parse(json.java:79) @ org.springframework.data.mongodb.core.query.basicquery.<init>(basicquery.java:39) @ org.springframework.batch.item.data.mongoitemreader.dopageread(mongoitemreader.java:176) @ org.springframework.batch.item.data.abstractpaginateddataitemreader.doread(abstractpaginateddataitemreader.java:59) @ org.springframework.batch.item.support.abstractitemcountingitemstreamitemreader.read(abstractitemcountingitemstreamitemreader.java:88) @ org.springframework.batch.core.step.item.simplechunkprovider.doread(simplechunkprovider.java:91) @ org.springframework.batch.core.step.item.simplechunkprovider.read(simplechunkprovider.java:155) @ org.springframework.batch.core.step.item.simplechunkprovider$1.doiniteration(simplechunkprovider.java:114)
two issues see configuration:
- in order inject
jobparameter
values, need use step scope. bean configured using singleton scope (default). - you aren't injecting parameters method.
give following try:
@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; }
Comments
Post a Comment