How to limit the execution of a runnable JAR that runs independently and can be executed through a web application.?

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. ]

+4
source share
5 answers

, . . 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();
    }
    }

}
+1

, , jar/code ( ). , , . , , : "" (ID, , , ), , , , / , throw-not-execute-after-locked . , , , finally (: ). / , DB optimistic locking Lock, : Version. , . , ( )

+1

, , jvms. , , jvms, . , , . Terracotta - . apache zookeeper, curator, apache, .

Zookeeper : , :

lock = new InterProcessSemaphoreMutex(client, lockPath);
lock.acquire(5, TimeUnit.MINUTES);
// do something
lock.release();

:

+1

- . ( ) in_use - . , in_use . , -. , .

+1

, , , .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();
}

}

+1

Source: https://habr.com/ru/post/1661279/


All Articles