Skip to content

Instantly share code, notes, and snippets.

@takezoe
Last active November 12, 2018 07:28
Show Gist options
  • Save takezoe/5492f1b70415c9e3f30572367c8836a5 to your computer and use it in GitHub Desktop.
Save takezoe/5492f1b70415c9e3f30572367c8836a5 to your computer and use it in GitHub Desktop.
Hystrix adapter for scala.concurrent.Future
package utils
import com.netflix.hystrix.{HystrixCommandGroupKey, HystrixObservableCommand}
import rx.Observable
import rx.lang.scala.subjects.ReplaySubject
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
abstract class HystrixFutureCommand[T](groupKey: HystrixCommandGroupKey)(implicit ec: ExecutionContext)
extends HystrixObservableCommand[T](groupKey) {
override def construct(): Observable[T] = {
val channel = ReplaySubject[T]()
run().onComplete {
case Success(v) => {
channel.onNext(v)
channel.onCompleted()
}
case Failure(t) => {
channel.onError(t)
channel.onCompleted()
}
}
channel.asJavaSubject
}
def run(): Future[T]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment