java - How to run Spring Batch Jobs in certain order (Spring Boot)? -
i'm developing spring batch using spring boot.
i'm minimal configuration provided spring boot , defined jobs (no xml configuration @ all). when run application,
springapplication.run(app.class, args);
the jobs sequentially executed in arbitrary order.
i'm defining jobs way in @configuration
annotated classes, spring rest:
@bean public job requesttickets() { return jobbuilderfactory.get(config.job_request_tickets) .start(steprequesttickets()) .build(); }
how can instruct framework run jobs in order?
edit: warning give hint? (maybe has nothing be)
2016-12-29 17:45:33.320 warn 3528 --- [main] o.s.b.c.c.a.defaultbatchconfigurer: no datasource provided...using map based jobrepository
1.you first disable automatic job start specifying spring.batch.job.enabled=false
in application.properties
2.in main class, - applicationcontext ctx = springapplication.run(springbatchmain.class, args);
assuming main class named - springbatchmain.java.
this initialize context without starting jobs.
3.once context initialized, either can - joblauncher joblauncher = (joblauncher) ctx.getbean("joblauncher");
or autowired
joblauncher bean in main class , launch specific jobs sequentially in specific sequential order invoking , joblauncher.run(job, jobparameters)
.
you can specific job
instances context initialized @ step # 2.
you can use ordered collection put jobs there , launch jobs iterating on collection.
4.this above technique works long joblauncher configured synchronous i.e. main thread waits joblauncher.run()
call complete , default behavior of joblauncher.
if have defined joblauncher use asynctaskexecutor jobs started in parallel , sequential ordering not maintained.
hope helps !!
edit:
i experimenting @order
annotation pointed stephane nicoll , seems in creating ordered collection of jobs , can iterate , launch jobs in order.
this below component gives me jobs in order specified ,
@component public class myjobs { @autowired private list<job> jobs; public list<job> getjobs() { return jobs; } }
and can , myjobs myjobs = (myjobs) ctx.getbean("myjobs");
in main class provided bean defined,
@bean public myjobs myjobs() { return new myjobs(); }
i can iterate on myjobs
, launch jobs in order specified @order annotation.
Comments
Post a Comment