1

I have a method that I would like to run a cron job, or something similar enough to a cron job (but not a Thread, TaskTimer, or something similar to those that will take up more resources in my case) for only a small part of that method.

  @Override
  public void notifyUsers(Data data) {
    // do some stuff with data
    
    // want a cron job here
    doImportantStuff(data);
  }

Normally, I could just set up my .xml file to have the entire method to independently run the task and figure everything out, however, this method is dependent on some other data is is being processed by another external service, so I am unfortunately dependent on that service to provide me with the information I need, and I can't fetch that data in my current service due to our way of working and organization of data and whatnot.

Additionally, the work that will be done in doImportantStuff() will basically iterate of a list of objects, each with a list of their own times to be executed (only once, however). So for example, data.list[0] will be executed in 30 and 45 minutes (once for each time, for the entire life span, so 2 executions total) where as data.list[1] will be executed in 15, 30, and 60 minutes (3 executions total). These will and be be variable times too for a variable amount of data. (To me it would make more sense to use a thread, but like I said, I can't).

I would like to know is there a way for me to execute a cron, or something similar or 'small' enough as a cron to run in the background, for a block of code inside a method?

I originally was going to create a new Thread and have that thread doImportantStuff() and then sleep for X amount of time, but then after further investigation I realized that it would take up too many resources since, in theory, there could be hundreds of other calls to this method, and then each of them creating their own thread.

I am not an expert on this particular matter, obviously, so apologies if my understanding on the topic is wrong.

mr nooby noob
  • 1,860
  • 5
  • 33
  • 56
  • Dont' create threads. You can use an executor with a fixed number of threads and you submit the task to it. use the executor to start a `CompleteableFuture` and wait for the result (if you really must wait for the result). But do you really need a background job to do stuff, especially if you are blocking after that anyway, you basically lose the benefit of a background thread. – M. Deinum Aug 11 '20 at 09:49
  • @M.Deinum Thanks for the input. Well the thing that I should have mentioned is that, the data I will be processing will essentially be a list of other data that will each have their own list of times to be executes. So for example, data1 would want to be executed in 30 min and 45 min, where as data2 would be executed in 30 min and an hour. Once they have been executed, they are done (which kinda defeats the purpose of a cron, a bit, but I need something somewhat 'light'). – mr nooby noob Aug 11 '20 at 10:01
  • 1
    Use a scheduling executor and a timer for that. – M. Deinum Aug 11 '20 at 10:03

1 Answers1

0

I suppose you can use Spring Schedule in java as well. Because in Spring has this kind of feature. It is easy and useful

@Scheduled(cron = "0 15 10 15 * ?")
public void scheduledMethod(){
    //code..
}

You can find some more info here

Abdusoli
  • 661
  • 1
  • 8
  • 24
  • I can't use this, as I mentioned above, since I am dependent on some data being processed and passed in from another service, which I can't process myself. – mr nooby noob Aug 11 '20 at 09:53