加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Scala Play Framework中对服务器启动执行操作?

发布时间:2020-12-16 19:05:19 所属栏目:安全 来源:网络整理
导读:我的conf /目录中有一个配置文件servers.conf,每当路由/服务器被命中时,我的ServerController读取.这不是性能,因为当文件不会改变时,它需要在每个连续命中重新读取配置文件.此外,如果配置文件有问题,我可以告诉用户ASAP而不是在页面命中引发异常. 目前我在我
我的conf /目录中有一个配置文件servers.conf,每当路由/服务器被命中时,我的ServerController读取.这不是性能,因为当文件不会改变时,它需要在每个连续命中重新读取配置文件.此外,如果配置文件有问题,我可以告诉用户ASAP而不是在页面命中引发异常.

目前我在我的ServerController.scala中有

case class Server(ip: String,port: String)

/**
  * This controller creates an `Action` to handle HTTP requests to the
  * application's server page.
  */
@Singleton
class ServerController @Inject() extends Controller {

  /**
    * Create an Action to render an HTML page with a the list of servers.
    * The configuration in the `routes` file means that this method
    * will be called when the application receives a `GET` request with
    * a path of `/servers`.
    */
  def index = Action {

    val serverList = ConfigFactory.load().getConfigList("servers")
    val servers: List[Server] = serverList match {
      case null => List[Server]()
      case _ => val servers = serverList map { s =>
        Server(s.getString("ip"),s.getString("port"))
      } filter { s =>
        s.ip != null && s.port != null
      }
      servers.toList
    }

    Ok(views.html.servers(servers))
  }
}

我的目标是让服务器在启动时读取配置文件,如果配置文件中没有读入问题,路由被命中,将服务器列表传递给ServerController.如果有问题,我想要立即抛出异常.

我似乎找不到我的应用程序的入口点,所以我不知道如何在启动时执行操作.

有谁知道如何做到这一点?我正在使用Play 2.5.x.

解决方法

如果您使用的是最新版本的Play,那么在启动时可以看到任何一个名为Module的类,这个类是根包(即文件顶部没有包定义).以下是从最新的Flash 2.5.x版Activator模板中获取的示例,该模板已修改,用于在应用程序启动和关闭时运行代码:

在服务/ Say.scala中,这将是一个简单的服务,说“你好!”在启动和“再见!”当应用程序关闭时:

package services

import javax.inject._
import play.api.inject.ApplicationLifecycle
import scala.concurrent.Future

trait Say {
  def hello(): Unit
  def goodbye(): Unit
}

@Singleton
class SayImpl @Inject() (appLifecycle: ApplicationLifecycle) extends Say {  
    override def hello(): Unit = println("Hello!")
    override def goodbye(): Unit = println("Goodbye!")

    // You can do this,or just explicitly call `hello()` at the end
    def start(): Unit = hello()

    // When the application starts,register a stop hook with the
    // ApplicationLifecycle object. The code inside the stop hook will
    // be run when the application stops.
    appLifecycle.addStopHook { () =>
        goodbye()
        Future.successful(())
    }

    // Called when this singleton is constructed (could be replaced by `hello()`)
    start()
}

在Module.scala中,

import com.google.inject.AbstractModule
import services._

/**
 * This class is a Guice module that tells Guice how to bind several
 * different types. This Guice module is created when the Play
 * application starts.

 * Play will automatically use any class called `Module` that is in
 * the root package. You can create modules in other locations by
 * adding `play.modules.enabled` settings to the `application.conf`
 * configuration file.
 */
class Module extends AbstractModule {

  override def configure() = {
    // We bind the implementation to the interface (trait) as an eager singleton,// which means it is bound immediately when the application starts.
    bind(classOf[Say]).to(classOf[SayImpl]).asEagerSingleton()
  }
}

您可能会发现有用的其他资源是the Scala dependency injection (DI) documentation和the Guice documentation.Gay是Play使用的默认DI框架.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读