Java SNMP4J Trap应用程序正在冻结GUI(Java SNMP4J Trap application is freezing the GUI)
我在Java中有一个SNMP陷阱应用程序,旨在侦听SNMP代理并在JFrame窗口中的JTextArea上打印接收到的SNMP消息。
下面的第一部分是我的源代码,显示了TrapReceiver类的内容。 在这个类中,listen方法是充分利用工作的地方。 该类在JFrame类中进行实例化,我打算在上面提到的JTeaxtArea上显示消息。 我将JTextArea对象的引用,SNMP代理URL和端口发送到TrapReceiver类的构造函数中,然后调用TrapReceiver对象的run方法以在JFrame实例以外的单独线程中开始执行。 下面的第二部分展示了如何在JFrame实例中实例化TrapReeceiver类。
当我运行应用程序时,我注意到JFrame实例(即GUI)处于冻结状态,并且在JFrame实例中提到的JTeaxtArea上没有打印消息,该实例实例化了下面第一部分中所示的类TrapReeceiver。
我的问题是为什么JFrame实例(即GUI)是冻结的,尽管TRapReceiver本身是作为一个单独的线程执行的? 此外,我想知道这个冻结问题的可能解决方案是什么。 提前致谢。
PS:我已经验证了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() }
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() }
原文:https://stackoverflow.com/questions/9618096
最满意答案
看起来很破 这是逗号运算符的用法,它简单地评估为最终表达式的值,即
1
。因此,由于该代码相当于:
ss->elem = *i; res = 1;
随后的
res
测试看起来毫无意义,因此被打破。Looks broken. It's a use of the comma operator, which simply evaluates to the value of the final expression, i.e.
1
.Therefore, since that code is equivalent to:
ss->elem = *i; res = 1;
The subsequent testing of
res
seem pointless, and thus broken.
相关问答
更多-
我最近发现了对NULL值的精彩介绍: http://modern-sql.com/concept/three-valued-logic 一些报价: SQL空值基本上意味着“可能是任何东西”。 因此无法判断与null的比较是真是假。 ... 因此下面每个比较的结果是未知的: NULL = 1 NULL <> 1 NULL > 1 NULL = NULL ... 另一种看待它的方法是用一个随机()函数调用来精神上替换每个null。 I recently found this wonderful introdu ...
-
你不能有>10整数。 从你在这里看到你真的需要一个enum : public enum { Kg_1, Kg_3, Kg_5, Kg_10, Kg_11+ } 你将无法将这些直接放在任何列表视图中,并且需要一种将它们转换为用户友好的字符串的方法。 但是你将能够获得值(最有可能)作为这个枚举,并在不解析的情况下使用它。 详细信息当然取决于你有什么类型的列表视图和你在哪里使用它,但事实是,你不能在任何地方使用大于>10整数,因为它在C#中无效。 你当然可以使用普通字符串,但是你很可能需要一 ...
-
构建解析器: Condition ::= Term Condition' Condition' ::= epsilon | OR Term Condition' Term ::= Factor Term' Term' ::= epsilon | AND Factor Term' Factor ::= [ NOT ] Primary Primary ::= Literal | '(' Condition ')' Literal ::= Id Build a parser: Conditi ...
-
在C你用! 否定: #include
int main() { int a,b,c,f; a=0;b=1;c=1; f=((b||(!c))&&((!a)||(!c))&&(a||(!b)||c)); printf("%d\n",f); } ~运算符翻转一个数字中的所有位,这不是你想要的,以及为什么你得到-1。 你还需要|| 而不是| 和&&而不是& 。 &和| 运算符处理各个位,其中作为|| 和&&是逻辑运算符,并做你想要的。 In C you use ... -
循环中的条件不正确。 正确的条件是: while ( c != 'n' && c != 'N' && c != 'y' && c != 'Y' ) { // ... } 当你写: while (c != ('n' || 'N' || 'y' || 'Y')) 你问c是否与('n' || 'N' || 'y' || 'Y') 。 但是这个表达式本身将被评估为true因为非零值ORd一起被评估为true 。 所以你的情况相当于: while( c != true ) 这将导致无限循环...... ...
-
如果你有 String str = "a & b | (!c)"; 首先,摆脱空间: String str2 = str.replaceAll(" ", ""); 然后,获取你想要的数组: String[] array = str2.split(""); 更新 :根据OP改变的问题,另一个解决方案如下: String str = "a_1 & b_2 | (!c_3)"; // initial string StringCharacterIterator sci = new StringChara ...
-
如何将AND和OR中的布尔表达式转换为仅NAND(How to convert a boolean expression from AND and OR to only NAND)[2022-04-27]
这包括如何通过NAND构建其他逻辑门的细分。 应该是一个简单的应用程序: http://en.wikipedia.org/wiki/NAND_logic 例如C = A和B相当于 C = NOT (A NAND B) or C' = (A NAND B) C = C' NAND C' (effectively NOT'ing A NAND B) This has a breakdown of how to build other logic gates via NAND. Should be a ... -
布尔表达式在C中(boolean expression in C)[2023-10-24]
看起来很破 这是逗号运算符的用法,它简单地评估为最终表达式的值,即1 。 因此,由于该代码相当于: ss->elem = *i; res = 1; 随后的res测试看起来毫无意义,因此被打破。 Looks broken. It's a use of the comma operator, which simply evaluates to the value of the final expression, i.e. 1. Therefore, since that code is equivalent ... -
在C ++中,指针具有对bool隐式转换,其中如果指针等于nullptr则为“falsy”,否则为“truthy”。 所以 while(message) 相当于 while(message != nullptr) In C++ pointers have implicit conversions to bool, where a pointer is "falsy" if it is equal to nullptr and "truthy" otherwise. So while(message) ...
-
看看这个: (a + b)(b' + c)(a + c) = (ab' + 0 + ac + bc)(a + c) = (ab' + ab'c +ac + ac + abc + bc) = (ab' + ab'c + ac + abc + bc) = (ab'(1+c) + ac + bc(a + 1)) = (ab' + ac + bc) = (ab' + c(a+b)) = (ab' + bb' + c (a+b)) = (a+b)(b' + c) 关键步骤是实现bb' = 0,这样你就可以安全地 ...