Last active
November 12, 2018 07:28
-
-
Save takezoe/5492f1b70415c9e3f30572367c8836a5 to your computer and use it in GitHub Desktop.
Hystrix adapter for scala.concurrent.Future
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
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