我有一个Java SNMP陷阱应用程序,旨在监听SNMP代理并在JFrame窗口的JTextArea上打印接收到的SNMP消息。

下面的第一部分是我的源代码,显示了TrapReceiver类的内容。在此类中,listen方法是充分利用工作的地方。该类是在JFrame类中实例化的,我打算在该类上显示有关上述JTeaxtArea的消息。我将JTextArea对象的引用,SNMP代理URL和端口发送到类TrapReceiver的构造函数中,然后调用TrapReceiver对象的run方法以在除JFrame实例之外的单独线程中开始执行。下面的第二部分显示了如何在JFrame实例中实例化TrapReeceiver类。

当我运行该应用程序时,我注意到JFrame实例(即GUI)处于冻结状态,并且在JFrame实例中提到的JTeaxtArea上未打印任何消息,该实例实例化了下面第I部分中所示的TrapReeceiver类。

我的问题是尽管TRapReceiver本身是作为单独的线程执行的,为什么JFrame实例(即GUI)却冻结了?另外,我想知道解决此冻结问题的可能解决方案是什么。提前致谢。

附注:我已经验证TrapReceiver可以正常工作,并且可以在没有GUI的情况下作为独立应用程序运行时将消息打印到标准输出,但是由于某些线程同步问题,此GUI冻结了。我试图在不加线程的情况下运行TrapReceiver,即使在这种情况下,GUI仍然冻结。

第一部分

 package com.[Intenionally removed].snmp;

 import java.io.IOException;
 import javax.swing.JTextArea;
 import org.snmp4j.*;
 import org.snmp4j.mp.MPv1;
 import org.snmp4j.mp.MPv2c;
 import org.snmp4j.security.Priv3DES;
 import org.snmp4j.security.SecurityProtocols;
 import org.snmp4j.smi.OctetString;
 import org.snmp4j.smi.TcpAddress;
 import org.snmp4j.smi.TransportIpAddress;
 import org.snmp4j.smi.UdpAddress;
 import org.snmp4j.transport.AbstractTransportMapping;
 import org.snmp4j.transport.DefaultTcpTransportMapping;
 import org.snmp4j.transport.DefaultUdpTransportMapping;
 import org.snmp4j.util.MultiThreadedMessageDispatcher;
 import org.snmp4j.util.ThreadPool;

 public class TrapReceiver implements CommandResponder, Runnable {

   private String targetSnmpAgentURL;
   private int targetSnmpAgentPort;
   private JTextArea outConsole;

   public TrapReceiver() {
   }

   public TrapReceiver(JTextArea outConsole) {
       this.outConsole = outConsole;
   }


   public TrapReceiver(JTextArea outConsole, String targetSnmpAgentURL, int  targetSnmpAgentPort) {
       this.targetSnmpAgentURL = targetSnmpAgentURL;
       this.targetSnmpAgentPort = targetSnmpAgentPort;
       this.outConsole = outConsole;

       try {
           listen(new UdpAddress(targetSnmpAgentURL + "/" + targetSnmpAgentPort));
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    public final synchronized void listen(TransportIpAddress address) throws IOException {


        AbstractTransportMapping transport;
        if (address instanceof TcpAddress) {
            transport = new DefaultTcpTransportMapping((TcpAddress) address);
        } else {
            transport = new DefaultUdpTransportMapping((UdpAddress) address);
        }

        ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
        MessageDispatcher mDispathcher = new MultiThreadedMessageDispatcher(
            threadPool, new MessageDispatcherImpl());

        // add message processing models
        mDispathcher.addMessageProcessingModel(new MPv1());
       mDispathcher.addMessageProcessingModel(new MPv2c());

        // add all security protocols
        SecurityProtocols.getInstance().addDefaultProtocols();
        SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());

        // Create Target
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));

        Snmp snmp = new Snmp(mDispathcher, transport);
        snmp.addCommandResponder(this);

        transport.listen();
        System.out.println("Listening on " + address);

        try {
           this.wait();
        } catch (InterruptedException ex) {
           Thread.currentThread().interrupt();
        }
        }

    /**
    * This method will be called whenever a pdu is received on the given port
    * specified in the listen() method
    */
    @Override
    public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
        //System.out.println("Received PDU...");
        outConsole.append("Received PDU...\n");
        PDU pdu = cmdRespEvent.getPDU();

        if (pdu != null) {
        outConsole.append("Trap Type = " + pdu.getType() + "\n");
        outConsole.append("Alarm Type: " + pdu.getVariableBindings().get(4) + "\n");
        outConsole.append("Alarm Message:  " + pdu.getVariableBindings().get(9) +  "\n\n");

        }
     }


     @Override
     public void run() {
        try {
            listen(new UdpAddress(targetSnmpAgentURL + "/" + targetSnmpAgentPort));
        } catch (IOException e) {
            outConsole.append("\nError occured while listening to SNMP messages: \n" + e.getMessage() + "\n\n");
        }
     }

} //end of class TrapReceiver

第二部分

在下面,我在线程中运行类TrapReceiver的实例。
   private void jButtonStartListeningSNMPActionPerformed(java.awt.event.ActionEvent evt)   {

    Thread snmpThread =
           new Thread(new TrapReceiver(jTextAreaSNMPAlarmOutput, jTextFieldSnmpAgentUrl.getText().trim(), Integer.parseInt(jTextFieldSnmpAgentPort.getText().trim())));
    snmpThread.start()

   }

最佳答案

问题是您正在listen()构造函数中调用TrapReceiver,这在gui线程上发生。您只想在listen()方法中调用run(),因为这是新线程中发生的部分。

09-25 15:25