基于afMongo实例,我目前正在这样做:
mongoClient := MongoClient(ActorPool(), `mongodb://localhost:27017`)
collection := mongoClient.db("mydb").collection("mycollection")
...
// inserts or queries here
...
mongoClient.shutdown我的理解是,MongoClient使用池连接。如果这是正确的,那么我相信我可以在所有DAO之间共享MongoClient,并且只有在afBedSheet应用程序关闭时才关闭它。
发布于 2014-08-16 10:42:13
MongoClient。我会把ConnectionManager作为一项服务,然后关闭它。所以在你的AppModule里
@Build
static ConnectionManager buildConnectionManager() {
ConnectionManagerPooled(ActorPool(), `mongodb://localhost:27017`)
}
@Contribute { serviceType=RegistryShutdown# }
static Void contributeRegistryShutdown(Configuration config, ConnectionManager conMgr) {
config.add(|->| { conMgr.shutdown } )
}MongoClient也可以是一种服务。
你也可以把上面写的再写一点,嗯,对。我倾向于使用ActorPools服务来监视它们。
static Void bind(ServiceBinder binder) {
binder.bind(MongoClient#)
}
@Build { serviceId="afMongo::ConnectionManager" }
static ConnectionManager buildConnectionManager(ConfigSource configSrc, ActorPools actorPools) {
actorPool := actorPools.get("myPod.connectionManager")
return ConnectionManagerPooled(actorPool , `mongodb://localhost:27017`)
}
@Contribute { serviceType=ActorPools# }
static Void contributeActorPools(Configuration config) {
config["myPod.connectionManager"] = ActorPool() { it.name = "myPod.connectionManager"; it.maxThreads = 1 }
}
@Contribute { serviceType=RegistryShutdown# }
static Void contributeRegistryShutdown(Configuration config, ConnectionManager conMgr) {
config["myPod.closeConnections"] = |->| {
conMgr.shutdown
}
}myPod.closeConnections只是一个任意的名称,在示例中它不在其他任何地方使用。
但您可以使用它覆盖或删除贡献。一些未来的测试场景可以添加具有以下内容的MyTestAppModule:
@Contribute { serviceType=RegistryShutdown# }
static Void contributeRegistryShutdown(Configuration config) {
config.remove("myPod.closeConnections")
}在这个特殊的例子中可能没有用,但是了解一下是有用的。
https://stackoverflow.com/questions/25334959
复制相似问题