本文整理汇总了Scala中monix.reactive.observers.Subscriber类的典型用法代码示例。如果您正苦于以下问题:Scala Subscriber类的具体用法?Scala Subscriber怎么用?Scala Subscriber使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Subscriber类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MyConnectableObservable
//设置package包名称以及导入依赖的类
package my.samples.observables
import com.typesafe.scalalogging.LazyLogging
import monix.execution.{ Cancelable, Scheduler }
import monix.execution.cancelables.{ BooleanCancelable, SingleAssignmentCancelable }
import monix.reactive.Observable
import monix.reactive.observables.ConnectableObservable
import monix.reactive.observers.Subscriber
import my.samples.services.ZombieConnectorService
import scala.concurrent.duration._
class MyConnectableObservable(service: ZombieConnectorService)(implicit s: Scheduler) extends ConnectableObservable[Long] with LazyLogging {
private[this] val connection = SingleAssignmentCancelable()
private val serviceName = service.getClass.getName
override def connect(): Cancelable = {
logger.info(s"connecting to the service $serviceName")
// 1. we connect to the service first
service.connect()
// 2. we register a callback that says what to do when we disconnect
connection := BooleanCancelable { () =>
service.disconnect()
}
connection
}
def close() = {
logger.info(s"shutting down connection to service $serviceName")
connection.cancel()
}
override def unsafeSubscribeFn(subscriber: Subscriber[Long]): Cancelable =
Observable.interval(1.second).subscribe(subscriber)
}
object MyConnectableObservable {
def apply(service: ZombieConnectorService)(implicit s: Scheduler) =
new MyConnectableObservable(service)
}
示例2: MyObserver
//设置package包名称以及导入依赖的类
package my.samples.observers
import akka.actor.ActorRef
import monix.execution.Ack.Continue
import monix.execution.{ Ack, Scheduler }
import monix.reactive.observers.Subscriber
import my.samples.models.MyMessages
import my.samples.models.MyMessages.Tick
import org.slf4j.LoggerFactory
import scala.concurrent.Future
class MyObserver(actorRef: ActorRef, sourceName: String)(implicit s: Scheduler) extends Subscriber[Long] {
private[this] def logger = LoggerFactory.getLogger(this.getClass)
override implicit def scheduler: Scheduler = s
override def onError(ex: Throwable): Unit =
logger.error(s"error happened when processing the stream: error message << ${ex.getMessage} >>")
override def onComplete(): Unit =
logger.info("stream completed")
override def onNext(elem: Long): Future[Ack] = {
logger.info(s"message received from source $sourceName --> $elem")
actorRef ! Tick(sourceName, elem)
Continue
}
}
object MyObserver {
def apply(actorRef: ActorRef, sourceName: String)(implicit s: Scheduler) = {
new MyObserver(actorRef, sourceName)(s)
}
}
示例3: SubjectSubscription
//设置package包名称以及导入依赖的类
package com.hypertino.hyperbus.util
import com.hypertino.hyperbus.transport.api.matchers.RequestMatcher
import monix.eval.Task
import monix.execution.Ack.Stop
import monix.execution.{Ack, Cancelable, Scheduler}
import monix.reactive.{Observable, Observer}
import monix.reactive.observers.Subscriber
import monix.reactive.subjects.{ConcurrentSubject, Subject}
import scala.util.Success
abstract class SubjectSubscription[T](implicit val scheduler: Scheduler) extends FuzzyMatcher {
type eventType = T
// FyzzyIndex properties
def requestMatcher: RequestMatcher
override def indexProperties: Seq[FuzzyIndexItemMetaInfo] = requestMatcher.indexProperties
override def matches(other: Any): Boolean = requestMatcher.matches(other)
// Subject properties
protected val subject: Subject[eventType, eventType]
def cancel(): Unit = {
remove()
subject.onComplete()
}
def publish(t: eventType): Task[Ack] = {
Task.fromFuture(subject.onNext(t).andThen {
case Success(Stop) ? remove()
})
}
private def cancel_1() = cancel()
val observable: Observable[eventType] = new Observable[eventType] {
override def unsafeSubscribeFn(subscriber: Subscriber[eventType]): Cancelable = {
val original: Cancelable = subject.unsafeSubscribeFn(subscriber)
add()
new Cancelable {
override def cancel(): Unit = {
cancel_1()
original.cancel()
}
}
}
}
protected def remove(): Unit
protected def add(): Unit
}