I'm trying to schedule a task that should run every 10 seconds. However this task should have a dynamical allowed period execution time. In other words, if the max allowed time is 5 seconds and if the task runs for more than 5 secs it should be killed/shutdown.

I have tried to use `@Schedule` with a cron time but no matter what I try I can't kill once it is running. However, an advice was given to me to not use the `@Schedule` and create a normal task with a `ScheduledExecutorService` but I have no idea how to do it.

My original method looks like that:

```
    @Scheduled(cron = "0/10 * * * * ?")
    @Transactional
    public void testMethod(Integer period) {
        ThreadPoolTaskScheduler scheduler;

        scheduler.setAwaitTerminationSeconds(period);
        scheduler.setWaitForTasksToCompleteOnShutdown(false);

        importantMethod();

    }

```

I have tried to rewrite it like so:

```
public void testMethod(){
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);            
  scheduler.scheduleAtFixedRate(importantMethod(), delay,
                                  period, TimeUnit.SECONDS);
}

```

However, I'm not sure how to set it up to have no delay to run every 10 seconds or every 5 mins and to only shut it down after the maximum allowed time is exceeded.

Any help will be greatly appreciated.