Spring Batch Not reading from DB nor writing to file -
i want use multiple datasources, 1 spring batch metadata , other business data. batch job runs , not try connect secondarydatasource. can point out wrong configuration?
@configuration @enablebatchprocessing public class batchconfiguration extends defaultbatchconfigurer { @override @autowired public void setdatasource( @qualifier("batchdatasource") datasource batchdatasource) { super.setdatasource(batchdatasource); } } public class springbatchconfig { @autowired private jobbuilderfactory jobs; @autowired private stepbuilderfactory steps; private static final string query_find_students = "select * ..."; @bean itemreader<dotdetailsdto> reader( @qualifier("secondarydatasource") datasource datasource) throws sqlexception { jdbccursoritemreader<dotdetailsdto> databasereader = new jdbccursoritemreader<>(); databasereader.setdatasource(datasource); databasereader.setsql(query_find_students); databasereader.setrowmapper(new dotrowmapper()); return databasereader; } @bean public itemprocessor<dotdetailsdto, dotdetailsdto> itemprocessor() { return new customitemprocessor(); } @bean public itemwriter<dotdetailsdto> writer() throws exception { flatfileitemwriter<dotdetailsdto> writer = new flatfileitemwriter<dotdetailsdto>(); writer.setresource(new classpathresource("file:test.csv")); delimitedlineaggregator<dotdetailsdto> dellineagg = new delimitedlineaggregator<dotdetailsdto>(); dellineagg.setdelimiter(","); beanwrapperfieldextractor<dotdetailsdto> fieldextractor = new beanwrapperfieldextractor<dotdetailsdto>(); fieldextractor.setnames(new string[] { "airwaybillnumber", "outbounddate", "orig", "dest", "lotnumber", "lotflightnumber", "lotorig", "lotdest", "lotpcs", "lotwt", "lotflightdepartdate", "iatacode" }); dellineagg.setfieldextractor(fieldextractor); writer.setlineaggregator(dellineagg); writer.afterpropertiesset(); return writer; } @bean protected step step1(itemreader<dotdetailsdto> reader, itemprocessor<dotdetailsdto, dotdetailsdto> processor, itemwriter<dotdetailsdto> writer) throws sqlexception { return steps.get("step1").<dotdetailsdto, dotdetailsdto> chunk(10) .reader(reader).processor(processor).writer(writer).build(); } @bean(name = "firstbatchjob") public job job(@qualifier("step1") step step1) { return jobs.get("firstbatchjob").start(step1).build(); } } public class datasourceconfiguration { @bean(name="batchdatasource") public datasource datasource() throws sqlexception { basicdatasource datasource = new basicdatasource(); ... return datasource; } @bean public jdbctemplate jdbctemplate( @qualifier("batchdatasource") final datasource datasource) { return new jdbctemplate(datasource); } @primary @bean(name="secondarydatasource") public datasource secondarydatasource() throws sqlexception { oracledatasource secondarydatasource = new oracledatasource(); ... return secondarydatasource; } @bean public jdbctemplate secondaryjdbctemplate( @qualifier("secondarydatasource") final datasource secondarydatasource) { return new jdbctemplate(secondarydatasource); } } public static void main(string[] args) { // spring java config annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(); context.register(datasourceconfiguration.class); context.register(batchconfiguration.class); context.register(springbatchconfig.class); context.refresh(); joblauncher joblauncher = (joblauncher) context.getbean("joblauncher"); job job = (job) context.getbean("firstbatchjob"); system.out.println("starting batch job"); try { jobexecution execution = joblauncher.run(job, new jobparameters()); system.out.println("job status : " + execution.getstatus()); system.out.println("job completed"); } catch (exception e) { e.printstacktrace(); system.out.println("job failed"); } }
wow after 2 days figured out issue was. not providing new jobparameters because of running same old broken job on , over.
below fix in main method.
public static void main(string[] args) { annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(); context.register(datasourceconfiguration.class); context.register(batchconfiguration.class); context.register(springbatchconfig.class); context.refresh(); joblauncher joblauncher = (joblauncher) context.getbean("joblauncher"); job job = (job) context.getbean("firstbatchjob"); system.out.println("starting batch job"); try { dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss"); date date = new date(); jobparameters jobparam = new jobparametersbuilder().addstring("jobdate",dateformat.format(date)).tojobparameters(); jobexecution execution = joblauncher.run(job, jobparam); system.out.println("job status : " + execution.getstatus()); system.out.println("job completed : " + execution.getjobid()); } catch (exception e) { e.printstacktrace(); system.out.println("job failed"); } }
Comments
Post a Comment