2424import java .util .Map ;
2525import java .util .Properties ;
2626
27- import javax .annotation .PostConstruct ;
28-
2927import org .apache .commons .logging .Log ;
3028import org .apache .commons .logging .LogFactory ;
3129
4341import org .springframework .batch .core .explore .JobExplorer ;
4442import org .springframework .batch .core .launch .JobLauncher ;
4543import org .springframework .batch .core .launch .JobParametersNotFoundException ;
46- import org .springframework .batch .core .launch .NoSuchJobException ;
4744import org .springframework .batch .core .repository .JobExecutionAlreadyRunningException ;
4845import org .springframework .batch .core .repository .JobInstanceAlreadyCompleteException ;
4946import org .springframework .batch .core .repository .JobRepository ;
5047import org .springframework .batch .core .repository .JobRestartException ;
48+ import org .springframework .beans .factory .InitializingBean ;
5149import org .springframework .beans .factory .annotation .Autowired ;
5250import org .springframework .boot .ApplicationArguments ;
5351import org .springframework .boot .ApplicationRunner ;
7169 * @author Akshay Dubey
7270 * @since 2.3.0
7371 */
74- public class JobLauncherApplicationRunner implements ApplicationRunner , Ordered , ApplicationEventPublisherAware {
72+ public class JobLauncherApplicationRunner
73+ implements ApplicationRunner , InitializingBean , Ordered , ApplicationEventPublisherAware {
7574
7675 /**
7776 * The default order for the command line runner.
@@ -114,15 +113,14 @@ public JobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExpl
114113 this .jobRepository = jobRepository ;
115114 }
116115
117- @ PostConstruct
118- public void validate () {
116+ @ Override
117+ public void afterPropertiesSet () {
119118 if (StringUtils .hasText (this .jobNames )) {
120- String [] jobsToRun = this .jobNames .split ("," );
121- for (String jobName : jobsToRun ) {
119+ for (String jobName : jobsToRun ()) {
122120 if (!isLocalJob (jobName ) && !isRegisteredJob (jobName )) {
123- throw new IllegalArgumentException ("No job instances were found for job name [ " + jobName + "] " );
121+ throw new IllegalArgumentException ("No job found with name ' " + jobName + "' " );
124122 }
125- }
123+ }
126124 }
127125 }
128126
@@ -187,7 +185,7 @@ private boolean isRegisteredJob(String jobName) {
187185 private void executeLocalJobs (JobParameters jobParameters ) throws JobExecutionException {
188186 for (Job job : this .jobs ) {
189187 if (StringUtils .hasText (this .jobNames )) {
190- String [] jobsToRun = this . jobNames . split ( "," );
188+ String [] jobsToRun = jobsToRun ( );
191189 if (!PatternMatchUtils .simpleMatch (jobsToRun , job .getName ())) {
192190 logger .debug (LogMessage .format ("Skipped job: %s" , job .getName ()));
193191 continue ;
@@ -199,9 +197,9 @@ private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionEx
199197
200198 private void executeRegisteredJobs (JobParameters jobParameters ) throws JobExecutionException {
201199 if (this .jobRegistry != null && StringUtils .hasText (this .jobNames )) {
202- String [] jobsToRun = this . jobNames . split ( "," );
200+ String [] jobsToRun = jobsToRun ( );
203201 for (String jobName : jobsToRun ) {
204- if ( isRegisteredJob ( jobName ) && !isLocalJob (jobName )) {
202+ if ( !isLocalJob (jobName )) {
205203 Job job = this .jobRegistry .getJob (jobName );
206204 execute (job , jobParameters );
207205 }
@@ -263,4 +261,8 @@ private JobParameters merge(JobParameters parameters, JobParameters additionals)
263261 return new JobParameters (merged );
264262 }
265263
264+ private String [] jobsToRun () {
265+ return this .jobNames .split ("," );
266+ }
267+
266268}
0 commit comments