2

securesocial in memoryサービスでplay-reactivemongoを使用しようとすると、エラーが発生します。

import play.api.Play.current

/**
 * A Sample In Memory user service in Scala
 *
 * IMPORTANT: This is just a sample and not suitable for a production environment since
 * it stores everything in memory.
 */
class InMemoryUserService(application: Application) extends UserServicePlugin(application) {

  private var users = Map[String, Identity]()
  private var tokens = Map[String, Token]()
[...]

しかし、コントローラー内では問題なく動作するようです。

import play.api.Play.current

object Application extends Controller with securesocial.core.SecureSocial {


  val db = ReactiveMongoPlugin.db
  lazy val collections = db("persons")
[...]

コントローラの外部で再生コンテキストを使用することは可能ですか?

私が得るエラーは、上記のクラススローエラーを使用するリソースにアクセスしようとしたときです:

! @6dl5aj9h3 - Internal server error, for (GET) [/login] ->

play.api.PlayException: Cannot load plugin[An exception occurred during Plugin [service.InMemoryUserService] initialization]
    at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:149) ~[play_2.10.jar:2.1.0]
    at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:130) ~[play_2.10.jar:2.1.0]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.immutable.List.foreach(List.scala:309) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
Caused by: play.api.PlayException: Cannot load plugin[An exception occurred during Plugin [service.InMemoryUserService] initialization]
    at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:149) ~[play_2.10.jar:2.1.0]
    at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:130) ~[play_2.10.jar:2.1.0]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.immutable.List.foreach(List.scala:309) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
         [repeats a lot]
Caused by: play.api.PlayException: Cannot load plugin[An exception occurred during Plugin [service.InMemoryUserService] initialization]
    at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:149) ~[play_2.10.jar:2.1.0]
    at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:130) ~[play_2.10.jar:2.1.0]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.immutable.List.foreach(List.scala:309) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
java.lang.StackOverflowError: null
    at java.io.UnixFileSystem.getBooleanAttributes0(Native Method) ~[na:1.6.0_43]
    at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:228) ~[na:1.6.0_43]
    at java.io.File.exists(File.java:733) ~[na:1.6.0_43]
    at sun.misc.URLClassPath$FileLoader.getResource(URLClassPath.java:999) ~[na:1.6.0_43]
    at sun.misc.URLClassPath$FileLoader.findResource(URLClassPath.java:966) ~[na:1.6.0_43]
    at sun.misc.URLClassPath$1.next(URLClassPath.java:196) ~[na:1.6.0_43]
4

2 に答える 2

2

ReactiveMongo で securesocial UserService を実装しようとしたときに同じエラーが発生しました。変更することで解決しました

  val db = ReactiveMongoPlugin.db(application)

  def db = ReactiveMongoPlugin.db(application)
于 2013-11-21T00:31:20.883 に答える
2

はい、可能です。フォルダー内のファイルをチェックするプラグインを作成しました。見つかった場合は、ファイルを読み取り、mongo インスタンスに保存します。

package plugins

import play.api.{Logger, Application, Plugin}
import akka.actor.Cancellable
import play.api.libs.concurrent.Akka
import scala.io.Source
import scalax.file.Path
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import play.modules.reactivemongo.ReactiveMongoPlugin
import java.io.{IOException, FileNotFoundException}
import models.LeadCategory
import reactivemongo.api.DefaultDB
import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global

/**
 * User: jakobdobrzynski
 * Date: 4/24/13
 * Time: 10:34 AM
 */
class WordImporter(application: Application) extends Plugin {

  val defaultInterval = 2
  var cancellable: Option[Cancellable] = None    

  val leadCategoryPathKey = "lead.categories.path"
  val newFile = ".new"
  val doneFiles = ".done"


  override def onStop() {
    cancellable.map(_.cancel())
  }

  override def onStart() {
    import play.api.Play.current
    import scala.concurrent.duration._
    import play.api.libs.concurrent.Execution.Implicits._

    val i = defaultInterval
    val newPath = current.configuration.getString(leadCategoryPathKey + newFile).getOrElse("")
    val donePath = current.configuration.getString(leadCategoryPathKey + doneFiles).getOrElse("")
    val db = ReactiveMongoPlugin.db

    cancellable = if (true) {
      Some(
        Akka.system.scheduler.schedule(0 seconds, i minutes) {
          Logger.info("Looking for files")
          val files = new java.io.File(newPath).listFiles
          for(f <- files) {
            if(f.getName.endsWith(".csv")) {
              handleCsvFile(f.getName, newPath, donePath, db)
            }
          }
          Logger.info("Done looking for files")
        })

    } else None
  }

  /*
  "caregoryId" -> BSONLong(leadCategory.categoryId),
  "categoryName" -> BSONString(leadCategory.categoryName),
  "word" -> BSONString(leadCategory.categoryName),
  "creationDate" -> leadCategory.creationDate.map(date => BSONDateTime(date.getMillis)),
  "updateDate" -> leadCategory.updateDate.map(date => BSONDateTime(date.getMillis)))
  */

  def handleCsvFile(fileName: String, newPath: String, donePath: String, db : DefaultDB) =    {

    Logger.info("Handling csv file: "+fileName)    
    lazy val collection = db("leadCategories")    
    try {
      val file = Source.fromFile(newPath + fileName)
      val iter = file.getLines().map(_.split(","))
      iter foreach (a => {
        collection.insert(LeadCategory(None, a(2).toLong, (""+a(1)).toLowerCase(), (""+a(0)).toLowerCase(), Some(DateTime.now()), Some(DateTime.now())))
      })
      Logger.info("Reading done! Time to move file")
      val path: Path = Path.fromString(newPath+fileName)

      val now = DateTime.now()
      val pattern = DateTimeFormat.forPattern("dd-MM-yyyy-hh-mm-ss")
      val nameParts = fileName.split("\\.")
      val dest: Path = Path.fromString(donePath+nameParts(0)+"-" + pattern.print(now) + "."+nameParts(1))
      path.moveTo(target = dest)
    } catch {
      case e: FileNotFoundException => {
        Logger.error("No file found")
      }
      case e: IOException => {
        Logger.error("Something went wrong when reading the file")
      }
    }
  }
}

私にとっての魔法は、次の 2 行にありました。

import scala.concurrent.ExecutionContext
import ExecutionContext.Implicits.global

それが役立つことを願っています=)

于 2013-04-26T13:21:58.263 に答える