Skip to content

Commit

Permalink
Change CachelessHystrixPropertiesStrategy to a normal class
Browse files Browse the repository at this point in the history
Apparently you can only register a strategy once inside a single Java process and
subsequent attempts will cause exceptions to be thrown.

Instead of registering in Global, which, depending on libs/other singletons/w/e,
may already be too late, use the hystrix.plugin.HystrixPropertiesStrategy.implementation
system property and name our class.
  • Loading branch information
lloydmeta committed Oct 1, 2014
1 parent b7aed25 commit d77bbde
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
4 changes: 1 addition & 3 deletions app/com/m3/octoparts/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import _root_.controllers.ControllersModule
import com.kenshoo.play.metrics.MetricsFilter
import com.m3.octoparts.cache.CacheModule
import com.m3.octoparts.http.HttpModule
import com.m3.octoparts.hystrix.{ CachelessHystrixPropertiesStrategy, HystrixMetricsLogger, HystrixModule }
import com.m3.octoparts.hystrix.{ HystrixMetricsLogger, HystrixModule }
import com.m3.octoparts.logging.PartRequestLogger
import com.beachape.logging.LTSVLogger
import com.m3.octoparts.repository.RepositoriesModule
import com.netflix.hystrix.strategy.HystrixPlugins
import com.typesafe.config.ConfigFactory
import com.wordnik.swagger.config.{ ConfigFactory => SwaggerConfigFactory }
import com.wordnik.swagger.model.ApiInfo
Expand Down Expand Up @@ -82,7 +81,6 @@ object Global extends WithFilters(MetricsFilter) with ScaldiSupport {

override def onStart(app: Application) = {
super.onStart(app)
HystrixPlugins.getInstance().registerPropertiesStrategy(CachelessHystrixPropertiesStrategy)
startPeriodicTasks(app)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy
* to return null so that [[com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory]] will always
* generate a new [[HystrixCommandProperties]] for each new [[com.netflix.hystrix.HystrixCommand]] instantiation
*/
object CachelessHystrixPropertiesStrategy extends HystrixPropertiesStrategy {
class CachelessHystrixPropertiesStrategy extends HystrixPropertiesStrategy {
override def getCommandPropertiesCacheKey(commandKey: HystrixCommandKey, builder: HystrixCommandProperties.Setter): String = null
}
1 change: 1 addition & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ object OctopartsBuild extends Build {
sonatypeSettings ++
coverallsSettings ++
Seq(
javaOptions += "-Dhystrix.plugin.HystrixPropertiesStrategy.implementation=com.m3.octoparts.hystrix.CachelessHystrixPropertiesStrategy",
publishArtifact := false,
libraryDependencies ++= Seq(
// Logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import org.scalatest.{ Matchers, FunSpec }

class CachelessHystrixPropertiesStrategySpec extends FunSpec with Matchers {

val subject = new CachelessHystrixPropertiesStrategy
val commandKey = HystrixCommandKey.Factory.asKey("hello")
val commandProps = HystrixCommandProperties.Setter()
describe("getCommandPropertiesCacheKey") {
it("should return null") {
val r = CachelessHystrixPropertiesStrategy.getCommandPropertiesCacheKey(commandKey, commandProps)
val r = subject.getCommandPropertiesCacheKey(commandKey, commandProps)
r should be(null)
}
}

// The following works if this test is run by itself, but
describe("after registering with HystrixPlugins") {

it("should allow HystrixPropertiesFactory.getCommandProperties to instantiate different HystrixCommandProperties for the same command key") {
HystrixPlugins.getInstance().registerPropertiesStrategy(CachelessHystrixPropertiesStrategy)
if (HystrixPlugins.getInstance().getPropertiesStrategy.getClass != subject.getClass) {
fail("HystrixPlugins.getPropertiesStrategy did not return CachelessHystrixPropertiesStrategy")
}
val properties1 = HystrixPropertiesFactory.getCommandProperties(
commandKey,
HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(300))
Expand Down

0 comments on commit d77bbde

Please sign in to comment.