java - 如何限制独立执行且可通过Web应用程序执行的可运行JAR的执行。?

标签 java spring-mvc jar processbuilder

我有一个可运行的 JAR 文件,计划使用 Timer 独立运行。

同样的JAR通过web应用程序执行(使用spring MVC开发)。

如果 jar 通过计时器执行,那么我必须限制 JAR 的执行,然后不允许通过 web 应用程序执行。

注意:[我使用了 processbuilder 来执行 JAR。]

最佳答案

我认为您正在开发可许可的 jar 。 以我的拙见,对于这种 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();
    }
    }

}

关于java - 如何限制独立执行且可通过Web应用程序执行的可运行JAR的执行。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40692723/

相关文章:

Java,比较单个字符串与字符

Spring:Lookup方法注入(inject)、Provider<T>、ObjectFactory和factoryBean之间的区别

java - 无法让 JUnit 使用我的 @Autowired 存储库和 MongoTemplate

java - 如何报告 JVM 中的所有异常,无论是自己的代码还是第三方代码?

java - 我无法在 ADT BUNDLE 中找到自定义 html 文件的正确路径

java - 在 IntelliJ 中附加 .java 文件作为 .jar 的源?

java - Maven - 生成 JARs GPG 签名

java - 在java中,如何从jar文件中检索图像?

Java字符串到日期更改时区

java - Spring 和 Jquery