gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:21:28 26 4
gpt4 key购买 nike

我有一个可运行的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com