KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > recovery > LogRestarter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.recovery;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.jboss.logging.Logger;
29
30 /**
31  * This class supports asynchronous restarting of log files. Restarting a log
32  * file means overwriting its entire content with null bytes and returning the
33  * file to the pool of clean log files maintained by a <code>BatchWriter</code>.
34  * A <code>LogRestarter</code> encapsulates a queue of log files to be
35  * restarted and implements a background thread that restarts the log files
36  * in the queue. Its sole purpose is to avoid the delay of cleaning up a log
37  * file.
38  *
39  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
40  * @version $Revision: 37459 $
41  */

42 class LogRestarter
43       implements Runnable JavaDoc
44 {
45    /**
46     * Class <code>Logger</code>, for trace messages.
47     */

48    private static Logger errorLog = Logger.getLogger(LogRestarter.class);
49
50    /** This flag can be set to false to stop the log restarter thread. */
51    private boolean running = true;
52    
53    /** Queue of <code>BatchLog</code> instances to be restarted. */
54    private List JavaDoc logsToRestart = new ArrayList JavaDoc();
55
56    /**
57     * Takes a log file to be asynchronously restarted.
58     *
59     * @param log a <code>BatchLog</code> instance to be restarted.
60     */

61    synchronized void add(BatchLog log)
62    {
63       logsToRestart.add(log);
64       notify();
65    }
66
67    /**
68     * Stops the log restarter thread.
69     */

70    synchronized void stop()
71    {
72       running = false;
73       notify();
74    }
75
76    /**
77     * The log restarter thread body.
78     */

79    public void run()
80    {
81       BatchLog log;
82       
83       while (running)
84       {
85          synchronized (this)
86          {
87             if (logsToRestart.size() > 0)
88             {
89                log = (BatchLog) logsToRestart.remove(0);
90             }
91             else
92             {
93                try
94                {
95                   wait();
96                }
97                catch (InterruptedException JavaDoc e)
98                {
99                   if (!running)
100                      break;
101                }
102                continue;
103             }
104          }
105
106          try
107          {
108             log.restart();
109          }
110          catch (IOException JavaDoc e)
111          {
112             errorLog.error("Error cleaning up transaction log "
113                            + log.getFilename(), e);
114          }
115       }
116
117    }
118
119 }
120
Popular Tags