Source Code Cross Referenced for ThreadWatchDog.java in  » Web-Server » Rimfaxe-Web-Server » com » rimfaxe » thread » 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 » Web Server » Rimfaxe Web Server » com.rimfaxe.thread 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.rimfaxe.thread;
002:
003:        import java.util.*;
004:
005:        /** The ThreadWatchDog class is the watchdog used to monitor threads
006:         * in an application. There are two dogs used to monitor, the ALPHA_DOG
007:         * and the BETA_DOG. The BETA_DOG's sole responsibility is to monitor
008:         * the ALPHA_DOG. The ALPHA_DOG monitors the BETA_DOG and all assigned
009:         * threads for monitoring. If a thread dies, the dogs will report an
010:         * exception. If the dead thread is a MutableThread, the dog will try to
011:         * restart it.
012:         */
013:        public class ThreadWatchDog extends MutableThread {
014:
015:            private static ThreadWatchDog mAlphaThread = null; // alphadog instance
016:            private static ThreadWatchDog mBetaThread = null; // betadog instance
017:            private static HashMap mThreadHashMap = null; // vector of all threads
018:            private static int mAlphaPriority = Thread.NORM_PRIORITY;
019:            private static int mBetaPriority = Thread.NORM_PRIORITY;
020:            private static long POLL_TIME = 3000; //  3 second check for TEST Change this to 60000 for one min. etc
021:            /** The attribute mWatchDogPollCount is added for test interest. It shows
022:             * how many times the polling has occurred within the dog.
023:             */
024:            public int mWatchDogPollCount = 0; // test count
025:            private int mDogIdentity = 0;
026:
027:            private ThreadWatchDog() {
028:                super ();
029:                mThreadHashMap = new HashMap();
030:            }
031:
032:            public void endWatching() {
033:                mAlphaThread.clear();
034:                mBetaThread.clear();
035:            }
036:
037:            public void clear() {
038:                mThreadHashMap = new HashMap();
039:            }
040:
041:            /** The getInstance method insures that the ALPHA_DOG (and therefore also
042:             * the BETA_DOG) is a singleton. It is static so that it can be referenced
043:             * throughout the application within the JVM.
044:             */
045:            public synchronized static ThreadWatchDog getInstance() {
046:                if (mAlphaThread == null) {
047:                    mAlphaThread = new ThreadWatchDog();
048:                    mAlphaThread.setPriority(mAlphaPriority);
049:                    mAlphaThread.setName("ALPHA DOG");
050:                    mBetaThread = new ThreadWatchDog();
051:                    mBetaThread.setPriority(mBetaPriority);
052:                    mBetaThread.setName("BETA DOG");
053:                    // these dogs watch each other
054:                    mAlphaThread.put(mBetaThread);
055:                    mBetaThread.put(mAlphaThread);
056:                }
057:                return mAlphaThread;
058:            }
059:
060:            /** This is the run method for the thread.
061:             */
062:            public void run() {
063:                //System.out.println("run()");
064:                while (true) {
065:
066:                    try {
067:                        //System.out.println("checkAllThreads()");
068:                        checkAllThreads();
069:                        poll();
070:
071:                    } catch (ReportingException eReportingException) {
072:                        ReportingExceptionHandler
073:                                .processException(eReportingException);
074:                    }
075:                }
076:            }
077:
078:            /** The checkAllThreads method for a dog checks each thread which has been
079:             * put into the HashMap.
080:             */
081:
082:            public synchronized void checkAllThreads() {
083:
084:                Set lThreadSet = mThreadHashMap.keySet();
085:                Iterator lThreadIterator = lThreadSet.iterator();
086:
087:                while (lThreadIterator.hasNext()) {
088:                    String lThreadKey = (String) lThreadIterator.next();
089:                    Object lTestThread = mThreadHashMap.get(lThreadKey);
090:
091:                    if (lTestThread instanceof  MutableThread) {
092:
093:                        if (!((MutableThread) lTestThread).isAlive()) {
094:                            ReportingExceptionHandler
095:                                    .processException(new ReportingException(
096:                                            "Mutable Thread " + lThreadKey
097:                                                    + " is dead"));
098:                            try {
099:                                ReportingLogger
100:                                        .logDebug("Attempting to restart thread : "
101:                                                + lThreadKey);
102:                                // attempt to restart the thread by clearing and restarting
103:                                ReportingLogger.logDebug("Creating Thread for "
104:                                        + lThreadKey);
105:                                ((MutableThread) lTestThread).createThread();
106:                                ReportingLogger.logDebug("Starting "
107:                                        + lThreadKey);
108:                                ((MutableThread) lTestThread).start();
109:                                ReportingLogger.logDebug("Restart Successful");
110:                            } catch (Exception eException) {
111:                                ReportingException lReportingException = new ReportingException(
112:                                        "Thread " + lThreadKey
113:                                                + " restart failure");
114:                                lReportingException.put("eException",
115:                                        eException);
116:                                ReportingExceptionHandler
117:                                        .processException(lReportingException);
118:                            }
119:                        } else {
120:                            ReportingLogger.logDebug("Mutable Thread "
121:                                    + ((MutableThread) lTestThread).getName()
122:                                    + "  is Alive");
123:                        }
124:
125:                        ReportingLogger.logDebug("Thread "
126:                                + ((MutableThread) lTestThread).getName()
127:                                + " is alive ");
128:                    } else { // not a Mutable thread
129:                        if (!((Thread) lTestThread).isAlive()) {
130:
131:                            // LOG this except for "BETA DOG" and "ALPHA DOG"
132:
133:                            if ((!lThreadKey.equalsIgnoreCase("ALPHA DOG"))
134:                                    && (!lThreadKey
135:                                            .equalsIgnoreCase("BETA DOG"))) {
136:                                com.rimfaxe.util.Log.log("Thread " + lThreadKey
137:                                        + " is being restarted");
138:                            }
139:
140:                            ReportingExceptionHandler
141:                                    .processException(new ReportingException(
142:                                            "Thread " + lThreadKey + " is dead"));
143:                        } else {
144:                            // thread is alive
145:                            ReportingLogger.logDebug("Thread "
146:                                    + ((Thread) lTestThread).getName()
147:                                    + "  is Alive");
148:                        }
149:                    }
150:                }
151:
152:            }
153:
154:            /** The put method puts a mutable thread into the HashMap for monitoring by a dog.
155:             * Note that this is an overloaded method which also allows for non-mutable Threads to
156:             * be monitored as well.
157:             */
158:            public synchronized void put(MutableThread aMutableThread) {
159:                ReportingLogger
160:                        .logDebug("ThreadWatchDog.adding mutable thread : "
161:                                + aMutableThread.getName());
162:                mThreadHashMap.put(aMutableThread.getName(), aMutableThread);
163:            }
164:
165:            public synchronized void remove(MutableThread aMutableThread) {
166:                ReportingLogger
167:                        .logDebug("ThreadWatchDog.adding mutable thread : "
168:                                + aMutableThread.getName());
169:                mThreadHashMap.remove(aMutableThread.getName());
170:            }
171:
172:            /** The put method causes the dog to monitor the passed Thread.
173:             */
174:            public synchronized void put(Thread aThread) {
175:                ReportingLogger.logDebug("ThreadWatchDog.adding thread : "
176:                        + aThread.getName());
177:                mThreadHashMap.put(aThread.getName(), aThread);
178:            }
179:
180:            private synchronized void poll() throws ReportingException {
181:                try {
182:                    mWatchDogPollCount++; // for test and monitoring, shows the number of times polling has occurred
183:                    ReportingLogger.logDebug("WatchDogPollCount is "
184:                            + mWatchDogPollCount + " for " + this .getName());
185:                    this .wait(POLL_TIME);
186:                } catch (InterruptedException eInterruptedException) {
187:                    ReportingException lReportingException = new ReportingException(
188:                            "Interrupted Exception Thread WatchDog poll");
189:                    lReportingException.put("eInterruptedException",
190:                            eInterruptedException);
191:                    throw lReportingException;
192:                }
193:
194:            }
195:
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.