Bridging the Communication between Lift Comet Actor and Akka Actor in Scala


In this blog, I would explain how to create bridge between Lift Comet Actor and Akka Actor.
Since Lift has its own actor model, so a bridge is needed to fill communication gap between LiftComet and Akka Actor, so that Akka actor would be able send message to comet, once comet has been set.

This bridging concept would be more useful for those web applications, which are built in Lift and using Akka Actor for message passing and running in distributed environment.

I have created a small Lift Login application with Things to Do and Chat functionality. you can find full source code here on GitHub:

You will find bridge concept in Chat Module.

class BridgeActor extends Actor {
  private var target: Option[CometActor] = None
  def receive = {
    case comet: CometActor => target = Some(comet)
    case msg => target.foreach(_ ! msg)
  }
}

/**
 * Controls the life-cycle of Actor Bridges
 */
object BridgeController extends Loggable {

  def getBridgeActor: ActorRef = {
    GlobalActorSystem.getActorSystem.actorOf(akka.actor.Props[BridgeActor])
  }
}

Here is one BridgeController, which would provide a BridgeActor on the request of Chat comet. Comet would get registered in forwarding target.
Akka actor would send message to this forwarding target.

Concept would be more clear from below lines:-

In Chat.scala:-

/*Requesting Bridge Controller to provide BridgeActor*/
private lazy val bridge: ActorRef = BridgeController.getBridgeActor

  override def localSetup {
    bridge ! this
    val future = akka.pattern.ask(alertManager, Subscribe(bridge, User.currentUser.open_!.id.is))
    val result = Await.result(future, timeout.duration).asInstanceOf[List[(ObjectId, String)]]
    chats = result
    super.localSetup
}


 override def localShutdown {
    alertManager ! Unsubscribe(bridge)
    bridge ! akka.actor.PoisonPill
    super.localShutdown
  }

We need to instantiate bridge at the time of setting up comet. This would register comet in forwarding target of BridgeActor.
We also need to kill bridge actor at the time of shutting down Comet. We can do this by giving PoisonPill.

This is a working example and would be more useful for remote application.

Please provide your valuable feedback.

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Java, Agile, Scala, Web, LiftWeb, Akka. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s