我正在尝试将Lucene集成到Web应用程序中。(不用于全文索引,而是用于快速搜索和排序。)我创建了一个服务:
trait Index {
def indexAd(a: Ad): Unit
}
@Singleton
class ConcreteIndex @Inject()(conf: Configuration) extends Index {
val dir = FSDirectory.open(FileSystems.getDefault.getPath(conf.getString("index.dir").get))
val writer = new IndexWriter(dir, new IndexWriterConfig(new StandardAnalyzer))
override def indexAd(a: Ad): Unit = {
val doc = new Document
...
}
}
并尝试在控制器中使用它:
@Singleton
class AdsController @Inject()(cache: CacheApi, index:Index) extends Controller {
...
}
但是注射不成功。我有
Error injecting constructor, org.apache.lucene.store.LockObtainFailedException:
Lock held by this virtual machine: .../backend/index/write.lock
我试图删除锁定文件,然后重新运行。它仍然引发相同的异常。有人可以帮我吗?我正在使用Lucene 6.2.0。播放2.5.x,scala 2.11.8
您可能需要确保IndexWriter
在关机时将其关闭以清除锁定。这可能是一项昂贵的操作,因此您可能希望将索引编写器的生命周期与Play应用程序的生命周期联系在一起,在(单例)ConcreteIndex
实例的构造函数中启动它,然后通过向注入的ApplicationLifecycle
实例添加停止挂钩来关闭它。例如:
@ImplementedBy(classOf[ConcreteIndex])
trait Index {
def index(s: String): Unit
}
@Singleton
case class ConcreteIndex @Inject()(conf: Configuration,
lifecycle: play.api.inject.ApplicationLifecycle) extends Index {
private val dir = FSDirectory.open(FileSystems.getDefault.getPath(conf.getString("index.dir").get))
private val writer = new IndexWriter(dir, new IndexWriterConfig(new StandardAnalyzer()))
// Add a lifecycle stop hook that will be called when the Play
// server is cleanly shut down...
lifecycle.addStopHook(() => scala.concurrent.Future.successful(writer.close()))
// For good measure you could also add a JVM runtime shutdown hook
// which should be called even if the Play server is terminated.
// If the writer is already closed this will be a no-op.
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run() = writer.close()
})
def index(s: String): Unit = ???
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句