KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > thread > impl > SimpleWorkerThread


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.excalibur.thread.impl;
18
19 import org.apache.avalon.excalibur.pool.Poolable;
20
21 import org.apache.avalon.framework.logger.LogEnabled;
22 import org.apache.avalon.framework.logger.Logger;
23
24 import org.apache.excalibur.thread.impl.AbstractThreadPool;
25 import org.apache.excalibur.thread.impl.WorkerThread;
26
27 /**
28  * This class extends the Thread class to add recyclable functionalities.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  */

32 class SimpleWorkerThread
33     extends WorkerThread
34     implements Poolable, LogEnabled
35 {
36     /** Log major events like uncaught exceptions and worker creation
37      * and deletion. Stuff that is useful to be able to see over long
38      * periods of time. */

39     private Logger m_logger;
40     
41     /**
42      * Log minor detail events like
43      */

44     private Logger m_detailLogger;
45
46     /**
47      * Allocates a new <code>Worker</code> object.
48      */

49     protected SimpleWorkerThread( final AbstractThreadPool pool,
50                                   final ThreadGroup JavaDoc group,
51                                   final String JavaDoc name )
52     {
53         super( pool, group, name );
54     }
55
56     public void enableLogging( final Logger logger )
57     {
58         m_logger = logger;
59         m_detailLogger = logger.getChildLogger( "detail" );
60         
61         // Log a created message here rather as we can't in the constructor
62
// due to the lack of a logger.
63
debug( "created." );
64     }
65
66     /**
67      * Used to log major events against the worker. Creation, deletion,
68      * uncaught exceptions etc.
69      *
70      * @param message Message to log.
71      */

72     protected void debug( final String JavaDoc message )
73     {
74         if ( m_logger.isDebugEnabled() )
75         {
76             // As we are dealing with threads where more than one thread is
77
// always involved, log both the name of the thread that triggered
78
// event along with the name of the worker involved. This
79
// increases the likely hood of walking away sane after a
80
// debugging session.
81
m_logger.debug( "\"" + getName() + "\" "
82                 + "(in " + Thread.currentThread().getName() + ") : " + message );
83         }
84     }
85
86     /**
87      * Used to log major events against the worker. Creation, deletion,
88      * uncaught exceptions etc.
89      *
90      * @param message Message to log.
91      * @param throwable Throwable to log with the message.
92      */

93     protected void debug( final String JavaDoc message, final Throwable JavaDoc throwable )
94     {
95         if ( m_logger.isDebugEnabled() )
96         {
97             m_logger.debug( "\"" + getName() + "\" "
98                 + "(in " + Thread.currentThread().getName() + ") : " + message, throwable );
99         }
100     }
101
102     /**
103      * Used to log minor events against the worker. Start and stop of
104      * individual pieces of work etc. Separated from the major events
105      * so that they are not lost in a sea of minor events.
106      *
107      * @param message Message to log.
108      */

109     protected void detailDebug( final String JavaDoc message )
110     {
111         if ( m_detailLogger.isDebugEnabled() )
112         {
113             m_detailLogger.debug( "\"" + getName() + "\" "
114                 + "(in " + Thread.currentThread().getName() + ") : " + message );
115         }
116     }
117
118     /**
119      * Used to log minor events against the worker. Start and stop of
120      * individual pieces of work etc. Separated from the major events
121      * so that they are not lost in a sea of minor events.
122      *
123      * @param message Message to log.
124      * @param throwable Throwable to log with the message.
125      */

126     protected void detailDebug( final String JavaDoc message, final Throwable JavaDoc throwable )
127     {
128         if ( m_detailLogger.isDebugEnabled() )
129         {
130             m_detailLogger.debug( "\"" + getName() + "\" "
131                 + "(in " + Thread.currentThread().getName() + ") : " + message, throwable );
132         }
133     }
134 }
135
136
Popular Tags