4

I have an SNMP trap application in Java that aims at listening to an SNMP agent and printing the received SNMP messages on a JTextArea in a JFrame window.

Part I below is my source code showing the content of class TrapReceiver. In this class, the listen method is the place that makes the most of the job. The class is intantiated within a JFrame class on which I intend to show the messages on so mentioned JTeaxtArea. I send the reference of the JTextArea object, the SNMP agent URL and the port into the constructor of the class TrapReceiver and then call the run method of TrapReceiver object to start execution in a seperate thread other than the JFrame instance. Part II below shows how I instantiate the class TrapReeceiver within the JFrame instance.

When I run the application I noticed that the JFrame instance (i.e. GUI) is freezing and no message is being printed on so mentioned JTeaxtArea within the JFrame instance which instantiates the class TrapReeceiver shown in Part I below.

My question is why the JFrame instance (i.e. GUI) is freezing although the TRapReceiver itself is executed as a separate thread? Also, I wonder what could be the possible solution to this freezing issue. Thanks in advance.

P.S.: I have verified that TrapReceiver works fine and can print messages to standard output when running as a stand alone application without GUI but it is this GUI that is freezing somehow due to some possible thread synch issue. I tried to run the TrapReceiver without putting onto a thread and even in this case the GUI was still freezing.

PART I

 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

PART II

In the following, I run an instance of class TrapReceiver within a thread.

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

   }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60

1 Answers1

5

the problem is that you are calling listen() in the TrapReceiver constructor, which happens on the gui thread. you only want to call listen() in the run() method, as that's the part that happens in the new thread.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Seems I forgot to remove it. I have done it just now but the GUI still freezes. According to the java tutorial (at http://docs.oracle.com/javase/tutorial/uiswing/events/generalrules.html), "Because all drawing and event-listening methods are executed in the same thread, a slow event-listener method can make the program seem unresponsive and slow to repaint itself. If you need to perform some lengthy operation as the result of an event, do it by starting up another thread ". This, I am already doing in my applcation but I dont get why it does not work. – F. Aydemir Mar 08 '12 at 13:33
  • 3
    Hey, I solved the problem. I was sending thread object to java.awt.inVokeLater method but it looks there can be only one event dispatching thread (according to what I read from Java tutorial). So, you suggestion helped it was my mistake to forget to remove the call in constrcutor. In conclusion, the invocation in PART II above is the correct way of doing it. Thanks. – F. Aydemir Mar 08 '12 at 13:39
  • 1
    Sorry too much copy paste put my mind over the clouds appearantly but if someone needs an snmp trap above is the working code :) – F. Aydemir Mar 08 '12 at 13:40