KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > AsyncLogTarget


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output;
9
10 import org.jivesoftware.util.log.ErrorAware;
11 import org.jivesoftware.util.log.ErrorHandler;
12 import org.jivesoftware.util.log.LogEvent;
13 import org.jivesoftware.util.log.LogTarget;
14 import java.util.LinkedList JavaDoc;
15
16 /**
17  * An asynchronous LogTarget that sends entries on in another thread.
18  * It is the responsibility of the user of this class to start
19  * the thread etc.
20  * <p/>
21  * <pre>
22  * LogTarget mySlowTarget = ...;
23  * AsyncLogTarget asyncTarget = new AsyncLogTarget( mySlowTarget );
24  * Thread thread = new Thread( asyncTarget );
25  * thread.setPriority( Thread.MIN_PRIORITY );
26  * thread.start();
27  * <p/>
28  * logger.setLogTargets( new LogTarget[] { asyncTarget } );
29  * </pre>
30  *
31  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
32  */

33 public class AsyncLogTarget extends AbstractTarget implements Runnable JavaDoc {
34
35     private final LinkedList JavaDoc m_list;
36     private final int m_queueSize;
37     private final LogTarget m_logTarget;
38
39     public AsyncLogTarget(final LogTarget logTarget) {
40         this(logTarget, 15);
41     }
42
43     public AsyncLogTarget(final LogTarget logTarget, final int queueSize) {
44         m_logTarget = logTarget;
45         m_list = new LinkedList JavaDoc();
46         m_queueSize = queueSize;
47         open();
48     }
49
50     /**
51      * Provide component with ErrorHandler.
52      *
53      * @param errorHandler the errorHandler
54      */

55     public synchronized void setErrorHandler(final ErrorHandler errorHandler) {
56         super.setErrorHandler(errorHandler);
57
58         if (m_logTarget instanceof ErrorAware) {
59             ((ErrorAware)m_logTarget).setErrorHandler(errorHandler);
60         }
61     }
62
63     /**
64      * Process a log event by adding it to queue.
65      *
66      * @param event the log event
67      */

68     public void doProcessEvent(final LogEvent event) {
69         synchronized (m_list) {
70             final int size = m_list.size();
71             while (m_queueSize <= size) {
72                 try {
73                     m_list.wait();
74                 }
75                 catch (final InterruptedException JavaDoc ie) {
76                     //This really should not occur ...
77
//Maybe we should log it though for
78
//now lets ignore it
79
}
80             }
81
82             m_list.addFirst(event);
83
84             if (size == 0) {
85                 //tell the "server" thread to wake up
86
//if it is waiting for a queue to contain some items
87
m_list.notify();
88             }
89         }
90     }
91
92     public void run() {
93         //set this variable when thread is interupted
94
//so we know we can shutdown thread soon.
95
boolean interupted = false;
96
97         while (true) {
98             LogEvent event = null;
99
100             synchronized (m_list) {
101                 while (null == event) {
102                     final int size = m_list.size();
103
104                     if (size > 0) {
105                         event = (LogEvent)m_list.removeLast();
106
107                         if (size == m_queueSize) {
108                             //tell the "client" thread to wake up
109
//if it is waiting for a queue position to open up
110
m_list.notify();
111                         }
112
113                     }
114                     else if (interupted || Thread.interrupted()) {
115                         //ie there is nothing in queue and thread is interrupted
116
//thus we stop thread
117
return;
118                     }
119                     else {
120                         try {
121                             m_list.wait();
122                         }
123                         catch (final InterruptedException JavaDoc ie) {
124                             //Ignore this and let it be dealt in next loop
125
//Need to set variable as the exception throw cleared status
126
interupted = true;
127                         }
128                     }
129                 }
130             }
131
132
133             try {
134                 //actually process an event
135
m_logTarget.processEvent(event);
136             }
137             catch (final Throwable JavaDoc throwable) {
138                 getErrorHandler().error("Unknown error writing event.", throwable, event);
139             }
140         }
141     }
142 }
143
Popular Tags