2

I need to schedule a task to run after 2 minutes. Then when the time is up I need to check if we are still ONLINE. If we are still online I simple don't do anything. If OFFLINE then I will do some work.

private synchronized void schedule(ConnectionObj connectionObj)
{
    if(connectionObj.getState() == ONLINE)
    {
        // schedule timer
    }
    else
    {
       // cancel task.
    }
}

This is the code I am considering:

@Async
private synchronized void task(ConnectionObj connectionObj)
{
    try
    {
        Thread.sleep(2000); // short time for test
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }

    if(connectionObj.getState() == ONLINE)
    {
       // don't do anything
    }

    else
    {
       doWork();
    }
}

For scheduling this task should I use @Async? I may still get many more calls to schedule while I am waiting inside the task() method.

Does SpringBoot have something like a thread that I create each time schedule() gets called so that this becomes easy?

I am looking for something similar to a postDelay() from Android: how to use postDelayed() correctly in android studio?

J_Strauton
  • 2,270
  • 3
  • 28
  • 70

2 Answers2

3

I'm not sure about an exclusively spring-boot solution, since it isn't something that I work with.

However, you can use ScheduledExecutorService, which is in the base Java environment. For your usage, it would look something like this:

@Async
private synchronized void task(ConnectionObj connectionObj)
{
    Executors.newScheduledThreadPool(1).schedule(() -> {
        if(connectionObj.getState() == ONLINE) 
        {
            // don't do anything
        } 

        else 
        {
           doWork();
        }
    }, 2, TimeUnit.MINUTES);
}

I used lambda expressions, which are explained here.

Leftist Tachyon
  • 304
  • 1
  • 15
3

Update

Seeing as how you need to schedule them "on-demand", @Scheduling won't help as you mentioned. I think the simplest solution is to go for something like @Leftist proposed.

Otherwise, as I mentioned in the comments, you can look at Spring Boot Quartz integration to create a job and schedule it with Quartz. It will then take care of running it after the two minute mark. It's just more code for almost the same result.

Original

For Spring Boot, you can use the built in Scheduling support. It will take care of running your code on time on a separate thread.

As the article states, you must enable scheduling with @EnableScheduling.

Then you annotate your method you want to run with @Scheduled(..) and you can either setup a fixedDelay or cron expression, or any of the other timing options to suit your time execution requirements.

Hermann Steidel
  • 1,000
  • 10
  • 18
  • I need to control when to start it though. I believe `@Scheduled` cannot be told to start or stop. – J_Strauton Feb 11 '19 at 03:14
  • @J_Strauton. Ah, so you mean you need to schedule it "on-demand" and then have the thread kick off after two minutes? – Hermann Steidel Feb 11 '19 at 03:17
  • Got it. My bad, didn't catch that requirement. You can look into Spring Boot Quartz https://www.baeldung.com/spring-quartz-schedule but it's a bit heavier weight than what you might be looking for. A simple solution would be to use the solution proposed by @Leftist. – Hermann Steidel Feb 11 '19 at 03:24