I have a snmpTrapListener class which once started keep listening to port 162 for snmp traps, now i want it to terminate or stop listening but this should be done from another class(Actually i am using shutdown hook of other class to stop this). How to do that?
Asked
Active
Viewed 1,047 times
0
-
1What do you mean exactly? Your question is unclear. Do you want to terminate a thread that is currently waiting on a given port? Maybe this will helps you http://stackoverflow.com/questions/2983835/how-can-i-interrupt-a-serversocket-accept-method – Jean-Baptiste Yunès Feb 02 '15 at 14:46
-
Yes but that should be done from another java class. My listener class keeps on listening once started now i have another class which when executed should terminate or stops thread that is listening to the port. – Swapnil1988 Feb 02 '15 at 14:54
-
Expose a "stop" method to others threads. This method just have to do the close... – Jean-Baptiste Yunès Feb 02 '15 at 17:37
2 Answers
0
You need to expose a public "stop" method, so you can call it from whichever other class you want to terminate the listening... the exact "termination" code can be hidden inside a private method, but the "stop" method needs to be public.
As for "how to make the class itself visible to the other class", consider registering your port-listener as a listener to to terminating class as well, so that the terminating class can invoke the abovementioned public class.

Ofer Lando
- 814
- 7
- 12
-
Will it work over same thread that is listening or new created thread of object creation will be stopped. – Swapnil1988 Feb 02 '15 at 14:59
-
As long as you know how to "link" between the classes, yes - but the question is how are you going to tell the "terminating" class which class it needs to "stop" from listening? – Ofer Lando Feb 02 '15 at 15:14
0
You need to expose function that can be called via thread interrupt. Some example can be found @ http://10kloc.wordpress.com/2013/12/24/cancelling-tasks-in-executors/

Ashkrit Sharma
- 627
- 5
- 7