This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.runtime.common.launch;
020
021import org.apache.reef.tang.Configuration;
022import org.apache.reef.tang.Tang;
023import org.apache.reef.tang.exceptions.InjectionException;
024
025import javax.inject.Inject;
026import java.util.logging.Level;
027import java.util.logging.Logger;
028
029/**
030 * This is used as the Exception handler for REEF client processes (Driver, Evaluator).
031 * <p>
032 * It catches all exceptions and sends them to the controlling process.
033 * For Evaluators, that is the Driver. For the Driver, that is the Client.
034 * <p>
035 * After sending the exception, this shuts down the JVM, as this JVM is then officially dead.
036 */
037public final class REEFUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
038  private static final Logger LOG = Logger.getLogger(REEFUncaughtExceptionHandler.class.getName());
039  private final Configuration errorHandlerConfig;
040
041  private REEFErrorHandler errorHandler;
042
043  /**
044   * @param errorHandlerConfig
045   */
046  @Inject
047  public REEFUncaughtExceptionHandler(final Configuration errorHandlerConfig) {
048    this.errorHandlerConfig = errorHandlerConfig;
049    this.errorHandler = null;
050  }
051
052  @Override
053  public synchronized void uncaughtException(final Thread thread, final Throwable throwable) {
054    if (this.errorHandler == null) {
055      try {
056        this.errorHandler = Tang.Factory.getTang().newInjector(this.errorHandlerConfig)
057            .getInstance(REEFErrorHandler.class);
058      } catch (InjectionException ie) {
059        LOG.log(Level.WARNING, "Unable to inject error handler.");
060      }
061    }
062
063    final String msg = "Thread " + thread.getName() + " threw an uncaught exception.";
064
065    if (this.errorHandler != null) {
066      LOG.log(Level.SEVERE, msg, throwable);
067      this.errorHandler.onNext(new Exception(msg, throwable));
068      try {
069        this.wait(100);
070      } catch (final InterruptedException expected) {
071        // try-catch block used to wait and give process a chance to setup communication with its parent
072      }
073      this.errorHandler.close();
074    }
075
076    LOG.log(Level.SEVERE, msg + " System.exit(1)");
077    System.exit(1);
078  }
079
080  @Override
081  public String toString() {
082    return "REEFUncaughtExceptionHandler{" +
083        "errorHandler=" + String.valueOf(this.errorHandler) +
084        '}';
085  }
086}