Created
April 15, 2015 10:11
-
-
Save hamnis/0a2a03c159f21dfc658b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import com.netflix.hystrix.{HystrixCommandGroupKey, HystrixCommand} | |
import rx.Subscriber | |
import scala.concurrent.{Future, Promise} | |
package object hystrix { | |
implicit class HystrixCommandToFuture[A](val cmd: HystrixCommand[A]) extends AnyVal { | |
def futureNoRepeat: Future[A] = { | |
val p = Promise[A]() | |
cmd.observe().subscribe(new Subscriber[A]() { | |
override def onError(e: Throwable): Unit = p.failure(e) | |
override def onCompleted(): Unit = () | |
override def onNext(t: A): Unit = p.success(t) | |
}) | |
p.future | |
} | |
} | |
implicit class StringHystrixOps(val s: String) extends AnyVal { | |
def asKey = HystrixCommandGroupKey.Factory.asKey(s) | |
} | |
implicit class ByNameOps[B](f: => B) { | |
def command(key: String = "byName"): HystrixCommand[B] = new HystrixCommand[B](key.asKey) { | |
override def run(): B = f | |
} | |
def commandWithFallback(key: String = "byNameFallback")(f2: => B) = new HystrixCommand[B](key.asKey) { | |
override def run(): B = f | |
override def getFallback: B = f2 | |
} | |
} | |
implicit class Function1Ops[A,B](val f: A => B) extends AnyVal { | |
def command(a: A, key: String = "function1"): HystrixCommand[B] = new HystrixCommand[B](key.asKey) { | |
override def run(): B = f(a) | |
} | |
def commandWithFallback(a: A, key: String = "function1Fallback")(f2: => B) = new HystrixCommand[B](key.asKey) { | |
override def run(): B = f(a) | |
override def getFallback: B = f2 | |
} | |
def commandWithFallback1(a: A, key: String = "function1Fallback")(f2: A => B) = new HystrixCommand[B](key.asKey) { | |
override def run(): B = f(a) | |
override def getFallback: B = f2(a) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment