0

I have a Java application that needs to run several times. Every time it runs, it checks if there's data to process and if so, it processes the data.

I'm trying to figure out what's the best approach (performance, resource consumption, etc.) to do this:

1.- Launch it once, and if there's nothing to process make it sleep (All Java).

2.- Using a bash script to launch the Java app, and when it finishes, sleep (the script) and then relaunch the java app.

I was wondering if it is best to keep the Java app alive (sleeping) or relaunching every time.

antorqs
  • 619
  • 3
  • 18
  • It depends. How long, how many resource required is algorithm etc... do you have ready to croning, scheduling infrastructure etc... what is wide ecosystem, resources should be locked (must rivalise with other) etc – Jacek Cz Feb 14 '17 at 09:25
  • There are pros and cons for each possibility... Startup time vs. complexity for example. – Erich Kitzmueller Feb 14 '17 at 09:29
  • @JacekCz Hello. At the moment it is deployed as the second approach. There are several applications (~30) following the same structure (Bash script for launching and relaunching) and using cron to start them all. Time taken for every program depends on the amount of data to process but it can go from several seconds to hours. – antorqs Feb 14 '17 at 09:29
  • 1
    Consider putting your thread to wait() instead of sleep() releasing the lock of your thread to not waste resources due to blocking – Nikolas Feb 14 '17 at 09:30
  • 1
    You should be aware that cron doesn't care if the previous run has finished or not, so you better make sure that there are not two instances of the program trying to process the same input data - see here: http://stackoverflow.com/questions/14409651/will-cron-start-a-new-job-if-the-current-job-is-not-complete – Erich Kitzmueller Feb 14 '17 at 09:35
  • @ammoQ I had a plan write the same warning :) Yes, important for long programs – Jacek Cz Feb 14 '17 at 09:38
  • @ammoQ You are right. I just checked here, and no they're not with cron, they are on launch on startup only. We're using cron to other tasks. – antorqs Feb 14 '17 at 09:39

3 Answers3

1

It's hard to answer your question without the specific context. On the face of it, your questions sounds like it could be a premature optimization.

Generally, I suggest you do what's easier for you to do (and to maintain), unless you have good reasons not to. Here are some possible good reasons, pick the ones appropriate to your situation:

For sleeping in Java:

  • The check of whether there's new data is easier in Java
  • Starting the Java program takes time or other resources, for example if on startup, your program needs to load a bunch of data
  • Starting the Java process from bash is complex for some reason - maybe it requires you to fiddle with a bunch of environment variables, files or something else.

For re-launching the Java program from bash:

  • The check of whether there's new data is easier in bash
  • Getting the Java process to sleep is complex - maybe your Java process is a complex multi-threaded beast, and stopping, and then re-starting the various threads is complicated.
  • You need the memory in between Java jobs - killing the Java process entirely would free all of its memory.
Malt
  • 28,965
  • 9
  • 65
  • 105
  • Also a reason for the second approach: Checking if everything is still ok after waking up being to complex. For example, a database connection might have become invalid, etc. – Erich Kitzmueller Feb 14 '17 at 09:41
0

I would not keep it alive. Instead of it you can use some Job which runs at defined intervals you can use jenkins or you can use Windows scheduler and configure it to run every 5 minutes (as you wish).

Run a batch file with Windows task scheduler

And from your batch file you can do following:

javac JavaFileName.java // To Compile

java JavaFileName // to execute file

See here how to execute java file from cmd :

How do I run a Java program from the command line on Windows?

Community
  • 1
  • 1
lesnar
  • 2,400
  • 7
  • 41
  • 72
0

I personally would determine it, by the place where the application is working.

  • if it would be my personal computer, I would use second option with bash script (as resources on my local machine might change a lot, due to extensive use of some other programs and it can happen that at some point I might be running out of memory for example)
  • if it goes to cloud (amazon, google, whatever) I know exactly what kind of processes are running there (it should not change so dynamically comparing to my local PC) and long running java with some scheduler would be fine for me
pezetem
  • 2,503
  • 2
  • 20
  • 38