播放2.2测试失败,异常'播放缓存不活动(STATUS_SHUTDOWN)'

时间:2013-11-19 03:40:38

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

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

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

如何配置播放以使用多实例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())
      .....
    }
  }
}

2 个答案:

答案 0 :(得分:4)

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

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)
    }
  }

}

答案 1 :(得分:0)

根据johanandren的帖子,我为Java做了这个工作,

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

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