Source Code Cross Referenced for ThreadAudit.java in  » 6.0-JDK-Modules » Java-Advanced-Imaging » examples » nistgoodies » threadaudit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » Java Advanced Imaging » examples.nistgoodies.threadaudit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.