首页 > 解决方案 > 如何阻止 groupMonitor.run 方法使我的 ScheduledExecutorService 崩溃?

问题描述

groupMonitor.run("", "") 正在通过 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;
        }

    }

标签: java

解决方案


我不能试运行,但你可以试试下面 -

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

推荐阅读