Scala RDF4J 应用程序不会终止

Scala RDF4J application won't terminate

在过去的几个月里,我一直在编写大量的 Scala + RDF4J。到目前为止一切顺利。

我今天刚写的,应用程序不会终止。它打印出请求的三个三元组和单词 "done" 但不会 return 到 sbt 命令提示符(或者,在 Eclipse 中,停止按钮保持红色并悬停在 运行 按钮上调出 "already running message")

我到底做错了什么?我运行它针对几个不同的端点,包括 RDF4J 服务器、Virtuoso 和 Blazegraph。

我的来源

import org.eclipse.rdf4j.query.QueryLanguage
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository

object sharedMain {
  def main(args: Array[String]): Unit = {

    val sparqlEndpoint = "https://dbpedia.org/sparql"
    val SparqlRepo = new SPARQLRepository(sparqlEndpoint)
    SparqlRepo.initialize()

    var con = SparqlRepo.getConnection()

    var queryString = "SELECT ?x ?y WHERE { ?x ?p ?y } limit 3 "
    val tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString)
    var result = tupleQuery.evaluate()

    while (result.hasNext()) { // iterate over the result
      val bindingSet = result.next()
      val valueOfX = bindingSet.getValue("x")
      val valueOfY = bindingSet.getValue("y")
      val toPrint = valueOfX + " -> " + valueOfY
      println(toPrint)
    }

    result.close
    con.close

    println("done")
  }
}

我的build.sbt

name := "tripleStoreTesting"
version := "0.1"
scalaVersion := "2.12.0"
resolvers += Classpaths.typesafeReleases
libraryDependencies ++= Seq(
    "ch.qos.logback" % "logback-classic" % "1.1.5" % "runtime",
    "org.scalactic" %% "scalactic" % "3.0.1",
    "org.scalatest" %% "scalatest" % "3.0.1" % "test",
    "org.eclipse.rdf4j" % "rdf4j-runtime" % "2.2.2",
    "commons-logging" % "commons-logging" % "1.2"
)
mainClass := Some("sharedMain")

正如 AKSW 所建议的,我没有关闭或关闭我打开、启动、初始化等的所有内容

// Scala code snippet, 
// starting with my program's last interaction with the triplestore

while (result.hasNext) { // iterate over the result
  val bindingSet = result.next
  val valueOfX = bindingSet.getValue("x")
  val valueOfY = bindingSet.getValue("y")
  val toPrint = valueOfX + " -> " + valueOfY
  println(toPrint)
}

result.close
con.close

// ADD THIS for proper application termination
SparqlRepo.shutDown

除了 JavaDocs AKSW posted, the RDF4J 编程教程还很好地强调了许多需要正确关闭的东西。

  • 连接
  • 结果迭代器
  • 语句迭代器
  • 输入流
  • 当然,文件

它只是没有具体提到关闭 SPARQLRepository 个实例。