alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Akka.scala)

This example Play Framework source code file (Akka.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

actorsystem, akka, akkaplugin, api, application, concurrent, core, could, giving, library, none, play, play framework, shutdown, some, starting, time

The Akka.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.libs.concurrent

import java.util.concurrent.{ TimeUnit, TimeoutException }
import play.api._
import play.core.ClosableLazy
import scala.concurrent.Future
import akka.actor.ActorSystem
import scala.concurrent.duration._

import com.typesafe.config._

/**
 * Helper to access the application defined Akka Actor system.
 */
object Akka {

  /**
   * Retrieve the application Akka Actor system.
   *
   * Example:
   * {{{
   * val newActor = Akka.system.actorOf[Props[MyActor]]
   * }}}
   */
  def system(implicit app: Application) = {
    app.plugin[AkkaPlugin].map(_.applicationSystem).getOrElse {
      sys.error("Akka plugin is not registered.")
    }
  }

}

/**
 * Plugin managing the application Akka Actor System.
 */
class AkkaPlugin(app: Application) extends Plugin {

  private val lazySystem = new ClosableLazy[ActorSystem] {

    protected def create() = {
      val config = app.configuration.underlying
      val name = app.configuration.getString("play.plugins.akka.actor-system").getOrElse("application")
      val system = ActorSystem(name, app.configuration.underlying, app.classloader)
      Play.logger.info(s"Starting application default Akka system: $name")

      val close: CloseFunction = { () =>
        Play.logger.info(s"Shutdown application default Akka system: $name")
        system.shutdown()

        app.configuration.getMilliseconds("play.akka.shutdown-timeout") match {
          case Some(timeout) =>
            try {
              system.awaitTermination(Duration(timeout, TimeUnit.MILLISECONDS))
            } catch {
              case te: TimeoutException =>
                // oh well.  We tried to be nice.
                Play.logger.info(s"Could not shutdown the Akka system in $timeout milliseconds.  Giving up.")
            }
          case None =>
            // wait until it is shutdown
            system.awaitTermination()
        }
      }

      (system, close)
    }

  }

  def applicationSystem: ActorSystem = lazySystem.get()

  override def onStop() {
    lazySystem.close()
  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Akka.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.