1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.hadoop.chukwa.datacollection.adaptor.filetailer;
1920import org.apache.log4j.Logger;
21import org.apache.hadoop.chukwa.util.ExceptionUtil;
2223publicclassTerminatorThreadextends Thread {
24privatestatic Logger log = Logger.getLogger(TerminatorThread.class);
2526privateFileTailingAdaptor adaptor = null;
2728publicTerminatorThread(FileTailingAdaptor adaptor) {
29this.adaptor = adaptor;
30 }
3132publicvoid run() {
3334long endTime = System.currentTimeMillis() + (10 * 60 * 1000); // now + 1035// mins36int count = 0;
37 log.info("Terminator thread started." + adaptor.toWatch.getPath());
38try {
39while (adaptor.tailFile()) {
40if (log.isDebugEnabled()) {
41 log.debug("Terminator thread:" + adaptor.toWatch.getPath()
42 + " still working");
43 }
44long now = System.currentTimeMillis();
45if (now > endTime) {
46 log.warn("TerminatorThread should have been finished by now! count="47 + count);
48 count++;
49 endTime = System.currentTimeMillis() + (10 * 60 * 1000); // now + 1050// mins51if (count > 3) {
52 log.warn("TerminatorThread should have been finished by now, stopping it now! count="53 + count);
54break;
55 }
56 }
57 }
58 } catch (InterruptedException e) {
59 log.info("InterruptedException on Terminator thread:"60 + adaptor.toWatch.getPath(), e);
61 } catch (Throwable e) {
62 log.warn("Exception on Terminator thread:" + adaptor.toWatch.getPath(),
63 e);
64 }
6566 log.info("Terminator thread finished." + adaptor.toWatch.getPath());
67try {
68 adaptor.reader.close();
69 } catch (Throwable ex) {
70 log.debug(ExceptionUtil.getStackTrace(ex));
71 }
72 }
73 }