001: package examples.nistgoodies.threadaudit;
002:
003: import gov.nist.javax.sip.SipStackImpl;
004:
005: import javax.sip.*;
006: import java.util.Properties;
007: import java.util.Timer;
008: import java.util.TimerTask;
009:
010: /**
011: * This example demonstrates how an application can monitor
012: * the health of the internal threads of the SIP Stack.
013: *
014: * This code is in the public domain.
015: *
016: * @author R. Borba (Natural Convergence)
017: *
018: */
019: public class ThreadAudit {
020:
021: /// SIP Stack objects
022: private static SipStack sipStack;
023: private static ListeningPoint listeningPoint;
024:
025: /// Test timer
026: private static Timer timer = new Timer();
027:
028: /// Interval between thread audits
029: private static long auditIntervalInMillis;
030:
031: /// Initializes the stack and starts the periodic audit
032: public static void init(boolean enableThreadAudit,
033: long auditInterval) {
034: // Save the audit interval for future use
035: auditIntervalInMillis = auditInterval;
036:
037: /// Initialize the stack properties
038: Properties properties = new Properties();
039: properties.setProperty("javax.sip.STACK_NAME",
040: "Thread Audit Sample");
041: if (enableThreadAudit) {
042: // That's all we need to do in order to enable the thread auditor
043: properties
044: .setProperty(
045: "gov.nist.javax.sip.THREAD_AUDIT_INTERVAL_IN_MILLISECS",
046: String.valueOf(auditInterval));
047: }
048: System.out.println("Thread Audit is "
049: + (enableThreadAudit ? "enabled" : "disabled"));
050:
051: // Create and initialize the SIP Stack
052: initSipStack(properties);
053:
054: // Start monitoring the health of the internal threads
055: startThreadAudit();
056:
057: // Sleep for a while so we can see some good audit reports being generated
058: sleep(4 * auditIntervalInMillis);
059:
060: // Kill one of the internal threads of the SIP stack so we can detect it in the next audit
061: System.out
062: .println("Killing one of the internal threads on purpose to see if the thread auditor detects it");
063: try {
064: sipStack.deleteListeningPoint(listeningPoint);
065: } catch (ObjectInUseException e) {
066: System.err.println("Failed to delete UDP listening point");
067: e.printStackTrace();
068: System.exit(0);
069: }
070:
071: // Sleep again to see if we're able to detect the listening point thread going away
072: sleep(4 * auditIntervalInMillis);
073:
074: System.out.println("Done!");
075: System.exit(0);
076: }
077:
078: /// Creates and initializes the SIP Stack
079: private static void initSipStack(Properties properties) {
080: // Create the SIP Stack
081: SipFactory l_oSipFactory = SipFactory.getInstance();
082: l_oSipFactory.setPathName("gov.nist");
083: try {
084: sipStack = l_oSipFactory.createSipStack(properties);
085: } catch (PeerUnavailableException e) {
086: System.err
087: .println("could not find \"gov.nist.jain.protocol.ip.sip.SipStackImpl\" in the classpath");
088: e.printStackTrace();
089: System.err.println(e.getMessage());
090: System.exit(0);
091: }
092:
093: // Create a UDP listening point
094: try {
095: listeningPoint = sipStack.createListeningPoint("127.0.0.1",
096: 5060, "UDP");
097: } catch (Exception e) {
098: System.err.println("Failed to create UDP listening point");
099: e.printStackTrace();
100: System.exit(0);
101: }
102: }
103:
104: /// Sleeps for a while
105: private static void sleep(long millis) {
106: try {
107: Thread.sleep(millis);
108: } catch (InterruptedException e) {
109: System.err.println("Can't sleep");
110: e.printStackTrace();
111: System.exit(0);
112: }
113: }
114:
115: // Kicks off the periodic audit
116: private static void startThreadAudit() {
117: /// Timer class used to periodically audit the stack
118: class AuditTimer extends TimerTask {
119: /// Action to be performed by this timer task
120: public final void run() {
121: // That's all we need to do in order to check if the internal threads of the stack are healthy
122: String auditReport = ((SipStackImpl) sipStack)
123: .getThreadAuditor().auditThreads();
124: if (auditReport != null) {
125: System.out.println("--> RED ALERT!!! "
126: + auditReport);
127: } else {
128: System.out
129: .println("--> Internal threads of the stack appear to be healthy...");
130: }
131:
132: // Schedule the next audit
133: timer.schedule(new AuditTimer(), auditIntervalInMillis);
134: }
135: }
136:
137: // Kick off the audit timer
138: timer.schedule(new AuditTimer(), auditIntervalInMillis);
139: }
140:
141: /// Entry point
142: public static void main(String[] args) throws Exception {
143: ThreadAudit.init(true, 5000);
144: }
145: }
|