All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook Maven / Gradle / Ivy

There is a newer version: 1.5.18
Show newest version
/**
 * Copyright 2013 Netflix, Inc.
 * 
 * Licensed 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 com.netflix.hystrix.strategy.executionhook;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
import com.netflix.hystrix.HystrixInvokable;
import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.netflix.hystrix.exception.HystrixRuntimeException.FailureType;
import com.netflix.hystrix.strategy.HystrixPlugins;

/**
 * Abstract ExecutionHook with invocations at different lifecycle points of {@link HystrixCommand}
 * and {@link HystrixObservableCommand} execution with default no-op implementations.
 * 

* See {@link HystrixPlugins} or the Hystrix GitHub Wiki for information on configuring plugins: https://github.com/Netflix/Hystrix/wiki/Plugins. *

* Note on thread-safety and performance *

* A single implementation of this class will be used globally so methods on this class will be invoked concurrently from multiple threads so all functionality must be thread-safe. *

* Methods are also invoked synchronously and will add to execution time of the commands so all behavior should be fast. If anything time-consuming is to be done it should be spawned asynchronously * onto separate worker threads. * * @since 1.2 * */ public abstract class HystrixCommandExecutionHook { /** * Invoked before {@link HystrixInvokable} begins executing. * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.2 */ public void onStart(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked when {@link HystrixInvokable} emits a value. * * @param commandInstance The executing HystrixInvokable instance. * @param value value emitted * * @since 1.4 */ public T onEmit(HystrixInvokable commandInstance, T value) { return value; //by default, just pass through } /** * Invoked when {@link HystrixInvokable} fails with an Exception. * * @param commandInstance The executing HystrixInvokable instance. * @param failureType {@link FailureType} enum representing which type of error * @param e exception object * * @since 1.2 */ public Exception onError(HystrixInvokable commandInstance, FailureType failureType, Exception e) { return e; //by default, just pass through } /** * Invoked when {@link HystrixInvokable} finishes a successful execution. * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.4 */ public void onSuccess(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked at start of thread execution when {@link HystrixCommand} is executed using {@link ExecutionIsolationStrategy#THREAD}. * * @param commandInstance The executing HystrixCommand instance. * * @since 1.2 */ public void onThreadStart(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked at completion of thread execution when {@link HystrixCommand} is executed using {@link ExecutionIsolationStrategy#THREAD}. * This will get invoked whenever the Hystrix thread is done executing, regardless of whether the thread finished * naturally, or was unsubscribed externally * * @param commandInstance The executing HystrixCommand instance. * * @since 1.2 */ public void onThreadComplete(HystrixInvokable commandInstance) { // do nothing by default } /** * Invoked when the user-defined execution method in {@link HystrixInvokable} starts. * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.4 */ public void onExecutionStart(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked when the user-defined execution method in {@link HystrixInvokable} emits a value. * * @param commandInstance The executing HystrixInvokable instance. * @param value value emitted * * @since 1.4 */ public T onExecutionEmit(HystrixInvokable commandInstance, T value) { return value; //by default, just pass through } /** * Invoked when the user-defined execution method in {@link HystrixInvokable} fails with an Exception. * * @param commandInstance The executing HystrixInvokable instance. * @param e exception object * * @since 1.4 */ public Exception onExecutionError(HystrixInvokable commandInstance, Exception e) { return e; //by default, just pass through } /** * Invoked when the user-defined execution method in {@link HystrixInvokable} completes successfully. * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.4 */ public void onExecutionSuccess(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked when the fallback method in {@link HystrixInvokable} starts. * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.2 */ public void onFallbackStart(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked when the fallback method in {@link HystrixInvokable} emits a value. * * @param commandInstance The executing HystrixInvokable instance. * @param value value emitted * * @since 1.4 */ public T onFallbackEmit(HystrixInvokable commandInstance, T value) { return value; //by default, just pass through } /** * Invoked when the fallback method in {@link HystrixInvokable} fails with an Exception. * * @param commandInstance The executing HystrixInvokable instance. * @param e exception object * * @since 1.2 */ public Exception onFallbackError(HystrixInvokable commandInstance, Exception e) { //by default, just pass through return e; } /** * Invoked when the user-defined execution method in {@link HystrixInvokable} completes successfully. * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.4 */ public void onFallbackSuccess(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked when the command response is found in the {@link com.netflix.hystrix.HystrixRequestCache}. * * @param commandInstance The executing HystrixCommand * * @since 1.4 */ public void onCacheHit(HystrixInvokable commandInstance) { //do nothing by default } /** * Invoked with the command is unsubscribed before a terminal state * * @param commandInstance The executing HystrixInvokable instance. * * @since 1.5.9 */ public void onUnsubscribe(HystrixInvokable commandInstance) { //do nothing by default } /** * DEPRECATED: Change usages of this to {@link #onExecutionStart}. * * Invoked before {@link HystrixCommand#run()} is about to be executed. * * @param commandInstance * The executing HystrixCommand instance. * * @since 1.2 */ @Deprecated public void onRunStart(HystrixCommand commandInstance) { // do nothing by default } /** * DEPRECATED: Change usages of this to {@link #onExecutionStart}. * * Invoked before {@link HystrixCommand#run()} is about to be executed. * * @param commandInstance * The executing HystrixCommand instance. * * @since 1.2 */ @Deprecated public void onRunStart(HystrixInvokable commandInstance) { // do nothing by default } /** * DEPRECATED: Change usages of this to {@link #onExecutionEmit} if you want to add a hook for each value emitted by the command * or to {@link #onExecutionSuccess} if you want to add a hook when the command successfully executes * * Invoked after successful execution of {@link HystrixCommand#run()} with response value. * In a {@link HystrixCommand} using {@link ExecutionIsolationStrategy#THREAD}, this will get invoked if the Hystrix thread * successfully runs, regardless of whether the calling thread encountered a timeout. * * @param commandInstance * The executing HystrixCommand instance. * @param response * from {@link HystrixCommand#run()} * @return T response object that can be modified, decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public T onRunSuccess(HystrixCommand commandInstance, T response) { // pass-thru by default return response; } /** * DEPRECATED: Change usages of this to {@link #onExecutionEmit} if you want to add a hook for each value emitted by the command * or to {@link #onExecutionSuccess} if you want to add a hook when the command successfully executes * * Invoked after successful execution of {@link HystrixCommand#run()} with response value. * In a {@link HystrixCommand} using {@link ExecutionIsolationStrategy#THREAD}, this will get invoked if the Hystrix thread * successfully runs, regardless of whether the calling thread encountered a timeout. * * @param commandInstance * The executing HystrixCommand instance. * @param response * from {@link HystrixCommand#run()} * @return T response object that can be modified, decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public T onRunSuccess(HystrixInvokable commandInstance, T response) { // pass-thru by default return response; } /** * DEPRECATED: Change usages of this to {@link #onExecutionError} * * Invoked after failed execution of {@link HystrixCommand#run()} with thrown Exception. * * @param commandInstance * The executing HystrixCommand instance. * @param e * Exception thrown by {@link HystrixCommand#run()} * @return Exception that can be decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public Exception onRunError(HystrixCommand commandInstance, Exception e) { // pass-thru by default return e; } /** * DEPRECATED: Change usages of this to {@link #onExecutionError} * * Invoked after failed execution of {@link HystrixCommand#run()} with thrown Exception. * * @param commandInstance * The executing HystrixCommand instance. * @param e * Exception thrown by {@link HystrixCommand#run()} * @return Exception that can be decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public Exception onRunError(HystrixInvokable commandInstance, Exception e) { // pass-thru by default return e; } /** * DEPRECATED: Change usages of this to {@link #onFallbackStart} * * Invoked before {@link HystrixCommand#getFallback()} is about to be executed. * * @param commandInstance * The executing HystrixCommand instance. * * @since 1.2 */ @Deprecated public void onFallbackStart(HystrixCommand commandInstance) { // do nothing by default } /** * DEPRECATED: Change usages of this to {@link #onFallbackEmit} if you want to write a hook that handles each emitted fallback value * or to {@link #onFallbackSuccess} if you want to write a hook that handles success of the fallback method * * Invoked after successful execution of {@link HystrixCommand#getFallback()} with response value. * * @param commandInstance * The executing HystrixCommand instance. * @param fallbackResponse * from {@link HystrixCommand#getFallback()} * @return T response object that can be modified, decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public T onFallbackSuccess(HystrixCommand commandInstance, T fallbackResponse) { // pass-thru by default return fallbackResponse; } /** * DEPRECATED: Change usages of this to {@link #onFallbackEmit} if you want to write a hook that handles each emitted fallback value * or to {@link #onFallbackSuccess} if you want to write a hook that handles success of the fallback method * * Invoked after successful execution of {@link HystrixCommand#getFallback()} with response value. * * @param commandInstance * The executing HystrixCommand instance. * @param fallbackResponse * from {@link HystrixCommand#getFallback()} * @return T response object that can be modified, decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public T onFallbackSuccess(HystrixInvokable commandInstance, T fallbackResponse) { // pass-thru by default return fallbackResponse; } /** * DEPRECATED: Change usages of this to {@link #onFallbackError}. * * Invoked after failed execution of {@link HystrixCommand#getFallback()} with thrown exception. * * @param commandInstance * The executing HystrixCommand instance. * @param e * Exception thrown by {@link HystrixCommand#getFallback()} * @return Exception that can be decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public Exception onFallbackError(HystrixCommand commandInstance, Exception e) { // pass-thru by default return e; } /** * DEPRECATED: Change usages of this to {@link #onStart}. * * Invoked before {@link HystrixCommand} executes. * * @param commandInstance * The executing HystrixCommand instance. * * @since 1.2 */ @Deprecated public void onStart(HystrixCommand commandInstance) { // do nothing by default } /** * DEPRECATED: Change usages of this to {@link #onEmit} if you want to write a hook that handles each emitted command value * or to {@link #onSuccess} if you want to write a hook that handles success of the command * * Invoked after completion of {@link HystrixCommand} execution that results in a response. *

* The response can come either from {@link HystrixCommand#run()} or {@link HystrixCommand#getFallback()}. * * @param commandInstance * The executing HystrixCommand instance. * @param response * from {@link HystrixCommand#run()} or {@link HystrixCommand#getFallback()}. * @return T response object that can be modified, decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public T onComplete(HystrixCommand commandInstance, T response) { // pass-thru by default return response; } /** * DEPRECATED: Change usages of this to {@link #onEmit} if you want to write a hook that handles each emitted command value * or to {@link #onSuccess} if you want to write a hook that handles success of the command * * Invoked after completion of {@link HystrixCommand} execution that results in a response. *

* The response can come either from {@link HystrixCommand#run()} or {@link HystrixCommand#getFallback()}. * * @param commandInstance * The executing HystrixCommand instance. * @param response * from {@link HystrixCommand#run()} or {@link HystrixCommand#getFallback()}. * @return T response object that can be modified, decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public T onComplete(HystrixInvokable commandInstance, T response) { // pass-thru by default return response; } /** * DEPRECATED: Change usages of this to {@link #onError}. * * Invoked after failed completion of {@link HystrixCommand} execution. * * @param commandInstance * The executing HystrixCommand instance. * @param failureType * {@link FailureType} representing the type of failure that occurred. *

* See {@link HystrixRuntimeException} for more information. * @param e * Exception thrown by {@link HystrixCommand} * @return Exception that can be decorated, replaced or just returned as a pass-thru. * * @since 1.2 */ @Deprecated public Exception onError(HystrixCommand commandInstance, FailureType failureType, Exception e) { // pass-thru by default return e; } /** * DEPRECATED: Change usages of this to {@link #onThreadStart}. * * Invoked at start of thread execution when {@link HystrixCommand} is executed using {@link ExecutionIsolationStrategy#THREAD}. * * @param commandInstance * The executing HystrixCommand instance. * * @since 1.2 */ @Deprecated public void onThreadStart(HystrixCommand commandInstance) { // do nothing by default } /** * DEPRECATED: Change usages of this to {@link #onThreadComplete}. * * Invoked at completion of thread execution when {@link HystrixCommand} is executed using {@link ExecutionIsolationStrategy#THREAD}. * This will get invoked if the Hystrix thread successfully executes, regardless of whether the calling thread * encountered a timeout. * * @param commandInstance * The executing HystrixCommand instance. * * @since 1.2 */ @Deprecated public void onThreadComplete(HystrixCommand commandInstance) { // do nothing by default } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy