I have one executable JAR file that I plan to run independently using the Timer .
The same JAR is run through a web application (developed in spring MVC).
I need to limit the execution of the JAR in a situation where the jar is executed using a timer, then at that time it cannot be executed through the web application.
Note : [ I used processbuilder to execute the JAR. ]
, . . Db . - .. - WatchDog ,
public class Watchdog implements Runnable { private Vector observers = new Vector(1); private long timeout = -1; private volatile boolean stopped = false; public static final String ERROR_INVALID_TIMEOUT = "timeout less than 1."; /** * Constructor for Watchdog. * @param timeout the timeout to use in milliseconds (must be >= 1). */ public Watchdog(long timeout) { if (timeout < 1) { throw new IllegalArgumentException(ERROR_INVALID_TIMEOUT); } this.timeout = timeout; } public void addTimeoutObserver(TimeoutObserver to) { observers.addElement(to); } public void removeTimeoutObserver(TimeoutObserver to) { //no need to synchronize, as Vector is always synchronized observers.removeElement(to); } protected final void fireTimeoutOccured() { Enumeration e = observers.elements(); while (e.hasMoreElements()) { ((TimeoutObserver) e.nextElement()).timeoutOccured(this); } } /** * Start the watch dog. */ public synchronized void start() { stopped = false; Thread t = new Thread(this, "WATCHDOG"); t.setDaemon(true); t.start(); } /** * Stop the watch dog. */ public synchronized void stop() { stopped = true; notifyAll(); } public synchronized void run() { final long until = System.currentTimeMillis() + timeout; long now; while (!stopped && until > (now = System.currentTimeMillis())) { try { wait(until - now); Boolean SafeToContinue=CheckDbCountDay(); if(SafeToContinue) ExecJarUsingStrategyPattern(new StrategyDoIt()); else ExecJarUsingStrategyPattern(new showMessage()); } catch (InterruptedException e) { callBack("plz call support"); } } if (!stopped) { fireTimeoutOccured(); } } }
, , jar/code ( ). , , . , , : "" (ID, , , ), , , , / , throw-not-execute-after-locked . , , , finally (: ). / , DB optimistic locking Lock, : Version. , . , ( )
finally
, , jvms. , , jvms, . , , . Terracotta - . apache zookeeper, curator, apache, .
Zookeeper : , :
lock = new InterProcessSemaphoreMutex(client, lockPath); lock.acquire(5, TimeUnit.MINUTES); // do something lock.release();
:
- . ( ) in_use - . , in_use . , -. , .
, , , .jar, , :
import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) { if (FileLock.exists()){ System.out.println("The file exists so the jar is running"); System.exit(0); } FileLock.createLockFile(); //start Running the .Jar File /* * * * CODE * * * */ //Delete the file when the program terminates FileLock.deleteLockFile(); System.out.println("Program exiting normally"); }
}
class FileLock { private static File lock = new File("jar-running.txt"); public static boolean exists() { return lock.exists(); } public static void createLockFile() { try { lock.createNewFile(); } catch (IOException e) { // the file already exists System.exit(0);} } public static void deleteLockFile() { if(lock.exists()) lock.delete(); }
Source: https://habr.com/ru/post/1661279/More articles:Removing duplicates in Python list by id - pythonhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1661275/problems-to-install-php-intl-with-xampp-macos&usg=ALkJrhhbv-pveMIcdwOJgle-_3ZeUfEBMQDoctrine2 - proxy object among regular objects in getResult - symfonyAngularJS: Error: [$ injector: unpr] Unknown provider: $ scopeProvider <- $ scope <- productService - javascriptThrowing exceptions from the constructor and what happens to the instance and memory allocation? - garbage-collectionHow to get the path to the subfolder in the main set? - iosAngular2 Template Selection - angularjsTensorflow, try and except does not handle exception - pythonHow parallax scroll layout over another in Fragment without toolbar in Android - android(Android Studio) Suggest variable names on top of outer classes in quick completion? - androidAll Articles