0

I am trying to run the cron jobs based on specific conditions using spring boot. Ex: if(bankName=="Sbi") then run the sbi job scheduler, if(bankName =="City") then run the city job scheduler and so on.

I am getting bank name through the database (service which fetch the bank details by customer account number) and I am not able to run the job based on the specified condition.

I am expecting output as bank name is 'sbi' then run task1 job and bank name is 'city' then run task2 job. But it run task3 job(bank name is 'hdfc') also even the if condition not met.

Please find below code snippet.

Note: I have put the break-point in SchedulingTask.java file for debugging purpose but it seems not hitting this break point. Please guide me how I can fix this problem.

   @SpringBootApplication
            @EnableScheduling
            public class SpringBootApp {
                public static void main(String[] args) {
                     SpringApplication.run(SpringBootApp.class, args);
                    // new SchedulingTask();
                }
            }

            @Configuration
            @EnableScheduling
            public class SchedulingTask {
            /* I have to get the Bankname from database  for testing i am hard-coding the name*/
                    String [] bankName = {"Sbi","City"};        
                     void  foo(String[] bankName) {
                        for (String name : bankName) {
                            setBankName(name);
                        }       
                    }

                public  void setBankName(String name) {
                    if(name.equalsIgnoreCase("sbi")) {  
                        new Task1();
                    } 
                    else if(name.equalsIgnoreCase("City")) {
                                new Task2();
                    } 
                    else if(name.equalsIgnoreCase("Hdfc")) {
                                 new Task3();
                    }
                }   
            }

@Configuration
@EnableScheduling
public class Task1 {

    @Bean
    @ConditionalOnProperty(value = "Sbi", matchIfMissing = true, havingValue = "true")
    public SchedulingTask1 scheduledSbiJob() {
        return new SchedulingTask1();
    }

}

@Configuration
@EnableScheduling
public class Task2 {

    @Bean
    @ConditionalOnProperty(value = "City", matchIfMissing = true, havingValue = "true")
    public SchedulingTask2 scheduledCityJob() {
        return new SchedulingTask2();
    }

}

@Configuration
@EnableScheduling
public class Task3 {

    @Bean
    @ConditionalOnProperty(value = "Hdfc", matchIfMissing = true, havingValue = "true")
    public SchedulingTask3 scheduledHdfcJob() {
        return new SchedulingTask3();
    }

}

@Component
public class SchedulingTask1 {
    @Scheduled(cron="*/2 * * * * ?")
    public void sbiTask() {
        System.out.println("sbiTask method excuted at every 2 seconds.  Current time is :: " +new Date());
    }
}

@Component
public class SchedulingTask2 {
    @Scheduled(cron="*/4 * * * * ?")
    public void cityTask() {
        System.out.println("cityTask method excuted at every 4 seconds.  Current time is :: " +new Date());
    }
}

@Component
public class SchedulingTask3 {
    @Scheduled(cron="*/6 * * * * ?")
    public void hdfcTask() {
        System.out.println("hdfcTask method excuted at every 6 seconds.  Current time is :: " +new Date());
    }
}
rama
  • 1
  • 1
  • 1
    Possible duplicate of [What is purpose of @ConditionalOnProperty annotation?](https://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation) – Gimby Oct 09 '19 at 08:03
  • no it is deferent question – rama Oct 09 '19 at 08:33
  • I think you should use @ConditionalOnExpression, however your condition parameter will come from database, which makes your solution complex as you need to fetch all required parameter before bean creation. in my opinion run all scheduler together and enable/disable them with a flag – Shailesh Chandra Oct 09 '19 at 08:49
  • provide any example using @ConditionalOnExpression and enable/disable it is very appreciate – rama Oct 09 '19 at 09:42

0 Answers0