4141import org .springframework .batch .core .explore .JobExplorer ;
4242import org .springframework .batch .core .launch .JobLauncher ;
4343import org .springframework .batch .core .launch .JobParametersNotFoundException ;
44- import org .springframework .batch .core .launch .NoSuchJobException ;
4544import org .springframework .batch .core .repository .JobExecutionAlreadyRunningException ;
4645import org .springframework .batch .core .repository .JobInstanceAlreadyCompleteException ;
4746import org .springframework .batch .core .repository .JobRepository ;
4847import org .springframework .batch .core .repository .JobRestartException ;
48+ import org .springframework .beans .factory .InitializingBean ;
4949import org .springframework .beans .factory .annotation .Autowired ;
5050import org .springframework .boot .ApplicationArguments ;
5151import org .springframework .boot .ApplicationRunner ;
6666 * @author Jean-Pierre Bergamin
6767 * @author Mahmoud Ben Hassine
6868 * @author Stephane Nicoll
69+ * @author Akshay Dubey
6970 * @since 2.3.0
7071 */
71- public class JobLauncherApplicationRunner implements ApplicationRunner , Ordered , ApplicationEventPublisherAware {
72+ public class JobLauncherApplicationRunner
73+ implements ApplicationRunner , InitializingBean , Ordered , ApplicationEventPublisherAware {
7274
7375 /**
7476 * The default order for the command line runner.
@@ -111,6 +113,17 @@ public JobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExpl
111113 this .jobRepository = jobRepository ;
112114 }
113115
116+ @ Override
117+ public void afterPropertiesSet () {
118+ if (StringUtils .hasText (this .jobNames )) {
119+ for (String jobName : jobsToRun ()) {
120+ if (!isLocalJob (jobName ) && !isRegisteredJob (jobName )) {
121+ throw new IllegalArgumentException ("No job found with name '" + jobName + "'" );
122+ }
123+ }
124+ }
125+ }
126+
114127 public void setOrder (int order ) {
115128 this .order = order ;
116129 }
@@ -161,10 +174,18 @@ protected void launchJobFromProperties(Properties properties) throws JobExecutio
161174 executeRegisteredJobs (jobParameters );
162175 }
163176
177+ private boolean isLocalJob (String jobName ) {
178+ return this .jobs .stream ().anyMatch ((job ) -> job .getName ().equals (jobName ));
179+ }
180+
181+ private boolean isRegisteredJob (String jobName ) {
182+ return this .jobRegistry != null && this .jobRegistry .getJobNames ().contains (jobName );
183+ }
184+
164185 private void executeLocalJobs (JobParameters jobParameters ) throws JobExecutionException {
165186 for (Job job : this .jobs ) {
166187 if (StringUtils .hasText (this .jobNames )) {
167- String [] jobsToRun = this . jobNames . split ( "," );
188+ String [] jobsToRun = jobsToRun ( );
168189 if (!PatternMatchUtils .simpleMatch (jobsToRun , job .getName ())) {
169190 logger .debug (LogMessage .format ("Skipped job: %s" , job .getName ()));
170191 continue ;
@@ -176,18 +197,12 @@ private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionEx
176197
177198 private void executeRegisteredJobs (JobParameters jobParameters ) throws JobExecutionException {
178199 if (this .jobRegistry != null && StringUtils .hasText (this .jobNames )) {
179- String [] jobsToRun = this . jobNames . split ( "," );
200+ String [] jobsToRun = jobsToRun ( );
180201 for (String jobName : jobsToRun ) {
181- try {
202+ if (! isLocalJob ( jobName )) {
182203 Job job = this .jobRegistry .getJob (jobName );
183- if (this .jobs .contains (job )) {
184- continue ;
185- }
186204 execute (job , jobParameters );
187205 }
188- catch (NoSuchJobException ex ) {
189- logger .debug (LogMessage .format ("No job found in registry for job name: %s" , jobName ));
190- }
191206 }
192207 }
193208 }
@@ -246,4 +261,8 @@ private JobParameters merge(JobParameters parameters, JobParameters additionals)
246261 return new JobParameters (merged );
247262 }
248263
264+ private String [] jobsToRun () {
265+ return this .jobNames .split ("," );
266+ }
267+
249268}
0 commit comments