java - 如何阻止 groupMonitor.run 方法使我的 ScheduledExecutorService 崩溃?
问题描述
groupMonitor.run("", "") 正在通过 IP 网络监听一些电子元件。
问题是:所有 5-6 小时 groupMonitor.run("", "") 都会崩溃,需要再次调用。赶紧说:groupMonitor.run("", "") 必须一直通过IP网络监听。
另一个问题是:当 groupMonitor.run("", "") 崩溃时,它会打印所有 100 毫秒的“redo groupMonitor ...”并且向我的网络发送大量垃圾邮件,以至于我的 IP 网关(接受连接)卡住并拒绝所有未来传入请求
这是最小的主要可以这么说
GroupMonitorOrig groupMonitor = new GroupMonitorOrig();
//when new groupMonitor is created the boolean MonitorExited is initialized with true
while (true) {
if (groupMonitor.monitorExited) { //this part gets executed when groupMonitor.run("", "") method has "crashed"
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println("redo groupMonitor ... ");
groupMonitor.run("192.168.1.7", 2169);
} catch (Exception ex) {
}
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 100, TimeUnit.MILLISECONDS);
}
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是 groupmonitor 的运行功能
public void run(String remoteHost, int port) {
final InetSocketAddress remote = new InetSocketAddress(remoteHost, port);
try (KNXNetworkLink knxLink = KNXNetworkLinkIP.newTunnelingLink(null, remote, false, TPSettings.TP1);
ProcessCommunicator pc = new ProcessCommunicatorImpl(knxLink)) {
// start listening to group notifications using a process listener
pc.addProcessListener(this);
System.out.println("Monitoring network using net/IP server " + remoteHost + " ...");
while (knxLink.isOpen()) {
Thread.sleep(1000);
}
monitorExited = false;
pc.detach();
} catch (final KNXException | InterruptedException | RuntimeException e) {
System.err.println(e);
monitorExited = true;
}
}
解决方案
我不能试运行,但你可以试试下面 -
while(true)
可以避免的
GroupMonitorOrig groupMonitor = new GroupMonitorOrig();
//when new groupMonitor is created the boolean MonitorExited is initialized with true
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
if (groupMonitor.monitorExited) {
System.out.println("starting new groupMonitor ... ");
groupMonitor.run("192.168.1.7", 2169);
}
} catch (Exception ex) {
System.err.println("Error in Runnable - " + ex.getMessage());
}
}
};
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 100, TimeUnit.MILLISECONDS);
public void run(String remoteHost, int port) {
monitorExited = false;
final InetSocketAddress remote = new InetSocketAddress(remoteHost, port);
KNXNetworkLink knxLink = null;
ProcessCommunicator pc = null;
try {
knxLink = KNXNetworkLinkIP.newTunnelingLink(null, remote, false, TPSettings.TP1);
pc = new ProcessCommunicatorImpl(knxLink);
pc.addProcessListener(this);
System.out.println("Monitoring network using net/IP server " + remoteHost + " ...");
while (knxLink.isOpen()) {
Thread.sleep(1000);
}
safeDetach(pc);
safeClose(knxLink);
} catch (final KNXException | InterruptedException | RuntimeException e) {
System.err.println("Error in checking liveliness - " + e.getMessage());
safeDetach(pc);
safeClose(knxLink);
}
monitorExited = true;
}
private void safeDetach(ProcessCommunicator pc) {
try {
if (pc != null) pc.detach();
} catch (Exception e) {
System.err.println("Could not safely detach pc - " + e.getMessage());
}
}
private void safeClose(KNXNetworkLink knxLink) {
try {
if (knxLink != null) knxLink.close();
} catch (Exception e) {
System.err.println("Could not safely close link - " + e.getMessage());
}
}
推荐阅读
- google-chrome - 如何从控制台复制所有 404 错误 URL?
- knockout.js - Knockout.js 在数据绑定中找不到函数
- python - TypeError:参数应该是整数或无,而不是在 tkinter python 中进行登录时出现“str”错误
- amazon-web-services - Amazon EC2 PostFix 无法发送电子邮件
- wpf - 缩放中心到鼠标位置图像
- ip-camera - 在监控模式下的数据包捕获期间,无法看到从 Android 应用程序发送到 Ipcamera 的凭据
- dynamics-crm - Microsoft USD - 通过单击 x 按钮阻止用户关闭会话选项卡
- api - application/vnd.google-apps.file mime 代表什么?
- html - 如何在 Angular/Typescript 的 DOM 中折叠时获取元素的高度
- wordpress - WP Rest API 不允许通过 Laravel 请求发布匿名评论