Skip to content

Instantly share code, notes, and snippets.

@joseronierison
Last active September 10, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseronierison/8a25da4eae672cc5a24773b7017cae9b to your computer and use it in GitHub Desktop.
Save joseronierison/8a25da4eae672cc5a24773b7017cae9b to your computer and use it in GitHub Desktop.
Hystrix Command Sample
package commands
import com.netflix.hystrix.HystrixCommand
import com.netflix.hystrix.HystrixCommand.Setter
import com.netflix.hystrix.{ HystrixCommandGroupKey, HystrixCommandKey, HystrixCommandProperties }
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy
object GetDependencyCommand {
final def settings: Setter = Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("get-dependency-group"))
.andCommandKey(HystrixCommandKey.Factory.asKey("GetDependencyCommand"))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD) //Strategy could be Thread Pool or Semaphore
.withCircuitBreakerSleepWindowInMilliseconds(1000) //Time before checking if circuit is healthy again
.withCircuitBreakerErrorThresholdPercentage(70) //The threshold percentege in last 10s(default) to open circuit
.withExecutionTimeoutInMilliseconds(2000) //Time waiting the response
)
def apply(dependencyCode: String): GetDependencyCommand = {
new GetDependencyCommand(dependencyCode)
}
}
class GetDependencyCommand(
dependencyCode: String
) extends HystrixCommand[Unit](GetDependencyCommand.settings) {
//EXECUTION: here is executed the function which makes a external call
override def run(): Unit = {
// Do a call
}
//FALLBACK: here is handled the fallback actions
override def getFallback: Unit = {
getFailedExecutionException match {
case ex: DependencyFailureException => //failure fallback return throwed by your call in run
case ex: HystrixRuntimeException => //decide what do when, eg.: circuit is open, timeout happens, thread rejects
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment