1

I'm using java scheduled in my project. After calling a rest api in my project, I start scheduled in my program. Likewise, I stop this scheduled with another rest api. In other words, I create, stop, run, etc. scheduled in runtime.

I want to convert this code from java scheduled to spring schedule.

When I look at the code samples, I can start scheduled by time. Or I can start it according to the parameter I gave in application.properties. However, none of them meet my will.My request is simply as follows.

if(condition) {
    startScheduled();
} 
else {
    stopScheduled();
}

but I could not find such an example.Can you help me?

omerstack
  • 535
  • 9
  • 23

3 Answers3

0

I cannot comment so I will post this here. Looks similar to your question: How to restart scheduled task on runtime with EnableScheduling annotation in spring?

Joe Huang
  • 3
  • 7
  • Thank you for your answer. But this example is not spring scheduled. I want to do this with spring scheduled(@Scheduled). – omerstack Aug 05 '19 at 17:30
  • any reason that you have to use @Scheduled? I never used the ScheduleFuture class in the answer but from the [documentation](https://www.logicbig.com/tutorials/core-java-tutorial/java-multi-threading/scheduled-future-interface.html) it seems to do the same as @Scheduled but with more flexibility. – Joe Huang Aug 05 '19 at 18:25
  • You can use @Async annotation if you have to do task only once else create a table and store status in database – Satish Kr Aug 05 '19 at 19:00
  • There's no reason I'm using spring scheduled. I wonder if we can do this with spring scheduled.Already I can do it with java schedule. My only purpose is to find out if I can do it using @schedules. – omerstack Aug 05 '19 at 20:54
0

First take a look at this Spring tutorial about @Scheduled annotations.

Then you need to establish how often your scheduled task is going to be executed. For this you will create a CRON expression that fits your needs. This website will help you achieve that.

Lastly inside the task, simply check your conditional and execute code accordingly.

Here is some example code:

@Component
public class ScheduledTask{

  private static final Logger log = LoggerFactory.getLogger(ScheduledTask.class);

  @Autowired
  SomeService someService;

  @Scheduled(cron = "0 5 0 * * ?", zone="America/Mexico_City")
  public void doSomething() {
      log.info("Starting scheduled task");
      if(condition) {
        startScheduled();
      } else {
        stopScheduled();
      }
  }
 }

Notice the @Component annotation, and also, this corn expression will execute the task every day at 00:05, on my time zone.

Hope this helps

José Ripoll
  • 504
  • 1
  • 7
  • 17
  • This code won't help me. I would like to start if scheduled according to if check .Example ; If userId = 0 from ui, scheduled will start. In your example, scheduled cannot start according to a changed value in runtime. Example : If the value in checkbox is "omer", let scheduled start. – omerstack Aug 05 '19 at 17:59
  • Ok ok...then what is the point of scheduled? Maybe i´m not undestanding correctly but isn´t what you are trying to do just a simple conditional check? you want If (userId == 0){executetask();} Or what does scheduled have to do with this? :B – José Ripoll Aug 05 '19 at 19:12
  • I gave example. My code is completely dynamic. Depending on the situation, I need to start and stop schedules.Actually, that's my question.I also want to do this using spring schedule. – omerstack Aug 05 '19 at 20:51
0

In my case, I use @Scheduled to schedule a short cycle. We are checking the cron at runtime using CronSequenceGenerator in that schedule.

WonChul Heo
  • 242
  • 1
  • 12