从在akka之外的线程上运行的回调函数调用akka调度程序

时间:2016-03-22 15:45:45

标签: multithreading scala akka

如何从Akka系统之外的线程调用akka调度程序?

我有一个actor会实例化一些java类,这些类具有在自己的线程上运行的回调。我想从这些回调中安排执行:

伪代码:

class GPIOActor extends Actor with PubSubActor {

  class DebouncerListener(pin: Pin) extends GpioPinListenerDigital {

    override def stateChangeEvent(event: GpioPinDigitalStateChangeEvent) {
      if (notAlreadyWaitingForBounceToFinish)
        context.system.scheduler.scheduleOnce(debouncePollPeriod, new Runnable() {
          def run(): Unit = {
            if (stillBouncing) {
              context.system.scheduler.scheduleOnce(debouncePollPeriod, this)(context.dispatcher)
            } else self ! PinStateChangeEvent(pin.getValue)
          }
        }      
    })(context.dispatcher)
  }

  val myPin = GPIOPinListener(RapiPin)

  myPin.registerCallback(new DebouncerListener(myPin))

  (...)

  def receive: Receive = {
    case PinStateChangeEvent(newValue) => notifySubscribers(newValue)
  }

}

0 个答案:

没有答案
相关问题