【问题标题】:Play 2.2 tests failed with exception 'The play Cache is not alive (STATUS_SHUTDOWN)'播放 2.2 测试失败,出现异常“播放缓存不存在 (STATUS_SHUTDOWN)”
【发布时间】:2013-11-19 03:40:37
【问题描述】:

当我运行我的测试时,这个错误会在第一次测试后立即抛出。我猜这是因为 Play 使用的是 CacheManager.create(ehcacheXml),它只为每个应用程序创建一个实例。

[error]     IllegalStateException: The play Cache is not alive (STATUS_SHUTDOWN) (Cache.java:4267)

如何配置play以使用多实例ehcache?

这是我的测试:

abstract class WithCleanTestData extends WithApplication(FakeApplication(
  additionalConfiguration = TestConf.getConf.toMap
  )) {
  override def around[T: AsResult](t: => T): Result = super.around {
    prepareDbWithData()
    t
  }
  def prepareDbWithData() = {
  }
}

object MyTest extends PlaySpecification {

  "test api" should {
    class MyCtrl() extends Controller with MyControler

    "post data 1" in new WithCleanTestData {
      val myControler = new MyCtrl()
      val ret: Future[SimpleResult] = myControler.method().apply(FakeRequest())
      .....
    }

    "post data 2" in new WithCleanTestData {
      val myControler = new MyCtrl()
      val ret: Future[SimpleResult] = myControler.method().apply(FakeRequest())
      .....
    }
  }
}

【问题讨论】:

    标签: scala playframework ehcache playframework-2.1 playframework-2.2


    【解决方案1】:

    在播放邮件列表上查看我的回复!

    https://groups.google.com/d/topic/play-framework/PBIfeiwl5rU/discussion


    /**
     * Custom in-memory cache plugin working around the shortcomings of the play bundled one.
     *
     * See more:
     * https://groups.google.com/d/msg/play-framework/PBIfeiwl5rU/-IWifSWhBlAJ
     *
     */
    class FixedEhCachePlugin(app: Application) extends CachePlugin {
    
      lazy val cache = {
        val manager = CacheManager.getInstance()
        manager.addCacheIfAbsent("play")
        manager.getCache("play")
      }
    
      override def onStart() {
        cache
      }
    
      override def onStop() {
        cache.flush()
      }
    
      lazy val api = new CacheAPI {
    
        def set(key: String, value: Any, expiration: Int) {
          val element = new Element(key, value)
          if (expiration == 0) element.setEternal(true)
          element.setTimeToLive(expiration)
          cache.put(element)
        }
    
        def get(key: String): Option[Any] = {
          Option(cache.get(key)).map(_.getObjectValue)
        }
    
        def remove(key: String) {
          cache.remove(key)
        }
      }
    
    }
    

    【讨论】:

    • 我也遇到了同样的异常。我是 scala 和 scala 播放框架的新手。您能否告诉我如何将这个新的 FixedEhCachePlugin 挂接到我的 TestSpec 类以及 CachePlugin 的 sbt 依赖项是什么?
    • 能否请您添加示例测试类的示例,作为此 FixedEhCachePlugin 已插入其中的方式
    【解决方案2】:

    根据 johanandren 的帖子,我为 Java 解决了这个问题,

    play.Play.application().plugin(EhCachePlugin.class).cache().flush();
    

    这是在@After 的测试基类中添加的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多