-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from benjchristensen/execution-hooks
HystrixCommand Execution Hooks #10
- Loading branch information
Showing
11 changed files
with
1,264 additions
and
117 deletions.
There are no files selected for viewing
968 changes: 935 additions & 33 deletions
968
hystrix-core/src/main/java/com/netflix/hystrix/HystrixCommand.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
...src/main/java/com/netflix/hystrix/strategy/executionhook/HystrixCommandExecutionHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/** | ||
* 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.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} execution with default no-op implementations. | ||
* <p> | ||
* See {@link HystrixPlugins} or the Hystrix GitHub Wiki for information on configuring plugins: <a | ||
* href="https://github.com/Netflix/Hystrix/wiki/Plugins">https://github.com/Netflix/Hystrix/wiki/Plugins</a>. | ||
* <p> | ||
* <b>Note on thread-safety and performance</b> | ||
* <p> | ||
* 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. | ||
* <p> | ||
* 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 HystrixCommand#run()} is about to be executed. | ||
* | ||
* @param commandInstance | ||
* The executing HystrixCommand instance. | ||
* | ||
* @since 1.2 | ||
*/ | ||
public <T> void onRunStart(HystrixCommand<T> commandInstance) { | ||
// do nothing by default | ||
} | ||
|
||
/** | ||
* Invoked after successful execution of {@link HystrixCommand#run()} with response value. | ||
* | ||
* @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 | ||
*/ | ||
public <T> T onRunSuccess(HystrixCommand<T> commandInstance, T response) { | ||
// pass-thru by default | ||
return response; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public <T> Exception onRunError(HystrixCommand<T> commandInstance, Exception e) { | ||
// pass-thru by default | ||
return e; | ||
} | ||
|
||
/** | ||
* Invoked before {@link HystrixCommand#getFallback()} is about to be executed. | ||
* | ||
* @param commandInstance | ||
* The executing HystrixCommand instance. | ||
* | ||
* @since 1.2 | ||
*/ | ||
public <T> void onFallbackStart(HystrixCommand<T> commandInstance) { | ||
// do nothing by default | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public <T> T onFallbackSuccess(HystrixCommand<T> commandInstance, T fallbackResponse) { | ||
// pass-thru by default | ||
return fallbackResponse; | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public <T> Exception onFallbackError(HystrixCommand<T> commandInstance, Exception e) { | ||
// pass-thru by default | ||
return e; | ||
} | ||
|
||
/** | ||
* Invoked before {@link HystrixCommand} executes. | ||
* | ||
* @param commandInstance | ||
* The executing HystrixCommand instance. | ||
* | ||
* @since 1.2 | ||
*/ | ||
public <T> void onStart(HystrixCommand<T> commandInstance) { | ||
// do nothing by default | ||
} | ||
|
||
/** | ||
* Invoked after completion of {@link HystrixCommand} execution that results in a response. | ||
* <p> | ||
* 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 | ||
*/ | ||
public <T> T onComplete(HystrixCommand<T> commandInstance, T response) { | ||
// pass-thru by default | ||
return response; | ||
} | ||
|
||
/** | ||
* 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. | ||
* <p> | ||
* 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 | ||
*/ | ||
public <T> Exception onError(HystrixCommand<T> commandInstance, FailureType failureType, Exception e) { | ||
// pass-thru by default | ||
return e; | ||
} | ||
|
||
/** | ||
* 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 <T> void onThreadStart(HystrixCommand<T> commandInstance) { | ||
// do nothing by default | ||
} | ||
|
||
/** | ||
* Invoked at completion of thread execution when {@link HystrixCommand} is executed using {@link ExecutionIsolationStrategy#THREAD}. | ||
* | ||
* @param commandInstance | ||
* The executing HystrixCommand instance. | ||
* | ||
* @since 1.2 | ||
*/ | ||
public <T> void onThreadComplete(HystrixCommand<T> commandInstance) { | ||
// do nothing by default | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...n/java/com/netflix/hystrix/strategy/executionhook/HystrixCommandExecutionHookDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* 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; | ||
|
||
/** | ||
* Default implementations of {@link HystrixCommandExecutionHook} that does nothing. | ||
* | ||
* @ExcludeFromJavadoc | ||
*/ | ||
public class HystrixCommandExecutionHookDefault extends HystrixCommandExecutionHook { | ||
|
||
private static HystrixCommandExecutionHookDefault INSTANCE = new HystrixCommandExecutionHookDefault(); | ||
|
||
private HystrixCommandExecutionHookDefault() { | ||
|
||
} | ||
|
||
public static HystrixCommandExecutionHook getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
} |
Oops, something went wrong.