HADOOP-14615. Add ServiceOperations.stopQuietly that accept slf4j logger API.

Contributed by Wenxin He.
This commit is contained in:
Steve Loughran 2017-07-04 10:48:02 +01:00
parent 186650d21d
commit 1aaa7f1eac
No known key found for this signature in database
GPG Key ID: 950CC3E032B79CA2
2 changed files with 85 additions and 0 deletions

View File

@ -25,6 +25,7 @@
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Evolving;
import org.slf4j.Logger;
/**
* This class contains a set of methods to work with services, especially
@ -87,6 +88,25 @@ public static Exception stopQuietly(Log log, Service service) {
return null;
}
/**
* Stop a service; if it is null do nothing. Exceptions are caught and
* logged at warn level. (but not Throwables). This operation is intended to
* be used in cleanup operations
*
* @param log the log to warn at
* @param service a service; may be null
* @return any exception that was caught; null if none was.
* @see ServiceOperations#stopQuietly(Service)
*/
public static Exception stopQuietly(Logger log, Service service) {
try {
stop(service);
} catch (Exception e) {
log.warn("When stopping the service {} : {}", service.getName(), e, e);
return e;
}
return null;
}
/**
* Class to manage a list of {@link ServiceStateChangeListener} instances,

View File

@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.service;
import org.apache.hadoop.test.GenericTestUtils.LogCapturer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.PrintWriter;
import static org.apache.hadoop.test.GenericTestUtils.LogCapturer.captureLogs;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test miscellaneous service operations through mocked failures.
*/
@RunWith(MockitoJUnitRunner.class)
public class TestServiceOperations {
@Mock
private Service service;
@Mock
private RuntimeException e;
@Test
public void testStopQuietlyWhenServiceStopThrowsException() throws Exception {
Logger logger = LoggerFactory.getLogger(TestServiceOperations.class);
LogCapturer logCapturer = captureLogs(logger);
doThrow(e).when(service).stop();
ServiceOperations.stopQuietly(logger, service);
assertThat(logCapturer.getOutput(),
containsString("When stopping the service " + service.getName()
+ " : " + e));
verify(e, times(1)).printStackTrace(Mockito.any(PrintWriter.class));
}
}