我正在使用akka流与反应式兔库一起构建一个脚本,将一些信息推送到我当地的rabbitmq服务器上的交换。
将信息推送到队列后,我希望程序自行关闭。但是Connection
使程序保持活跃状态,我无法在Connection
或其他如何杀死它的示例中找到任何方法。我不得不手动杀死这个过程。
我的代码看起来像这样:
package prototype
import akka.actor.ActorSystem
import akka.stream.FlowMaterializer
import akka.stream.scaladsl.{Sink, Source}
import akka.util.ByteString
import io.scalac.amqp.{Message, Connection}
object PopulateTodoQueue extends App {
val connection = Connection()
val message = Message(ByteString("message"))
val source = Source(List(message))
val sink = Sink(connection.publishDirectly(queue = "todo"))
implicit val actorSystem = ActorSystem()
implicit val materializer = FlowMaterializer()
(source to sink).run()
// Quick hack to wait long enough for the message to send
Thread.sleep(1000)
actorSystem.shutdown()
}
这是我的build.sbt库依赖项的片段:
"com.typesafe.akka" %% "akka-actor" % "2.3.7",
"com.typesafe.akka" %% "akka-stream-experimental" % "0.11",
"io.scalac" %% "reactive-rabbit" % "0.2.1",
这些一次性任务是否有更好的模式 - 比如你将回调传递给的临时连接?我见过的示例中的所有用例都适用于长期运行的客户端,这些客户端会一直运行,直到用户明确杀死它们为止。
提前致谢!
答案 0 :(得分:0)
连接特征中有关闭方法
/** Shutdowns underlying connection.
* Publishers and subscribers are terminated and notified via `onError`.
* This method waits for all close operations to complete. */
def shutdown(): Future[Unit]
你可以这样打电话
connection.shutdown()