本文整理汇总了Scala中reactivemongo.api.MongoConnection类的典型用法代码示例。如果您正苦于以下问题:Scala MongoConnection类的具体用法?Scala MongoConnection怎么用?Scala MongoConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MongoConnection类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: Database
//设置package包名称以及导入依赖的类
package de.choffmeister.microserviceutils.mongodb
import akka.Done
import com.typesafe.config.ConfigFactory
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.api.{DefaultDB, MongoConnection, MongoDriver}
import scala.concurrent.{ExecutionContext, Future}
abstract class Database(val database: DefaultDB, collectionNamePrefix: String = "")(implicit ec: ExecutionContext) {
def collection(name: String): BSONCollection = database.collection[BSONCollection](collectionNamePrefix + name)
def init(): Future[Done]
def clear(): Future[Done]
}
object Database {
def open[DB <: Database](f: DefaultDB => DB)(implicit ec: ExecutionContext): Future[DB] = {
val config = ConfigFactory.load().getConfig("mongodb")
val uri = config.getString("uri")
val databaseName = config.getString("database-name")
val driver = MongoDriver()
val mongodbUri = MongoConnection.parseURI(uri)
val connection = Future.fromTry(mongodbUri.map(driver.connection))
connection
.flatMap(_.database(databaseName).map(f))
.flatMap(db => db.init().map(_ => db))
}
}
开发者ID:choffmeister,项目名称:microservice-utils,代码行数:32,代码来源:Database.scala
示例2: MongoUtils
//设置package包名称以及导入依赖的类
package com.ringcentral.gatling.mongo
import reactivemongo.api.MongoConnection.{ParsedURI, URIParsingException}
import reactivemongo.api.{DefaultDB, MongoConnection, MongoDriver}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success, Try}
// fixme remove global context
import scala.concurrent.ExecutionContext.Implicits.global
object MongoUtils {
private val defaultPort: Int = 27017
private lazy val mongoDriver = new MongoDriver()
private def establishConnection(uri: ParsedURI, dbName: String, connectionTimeout: FiniteDuration): DefaultDB = {
Await.result(establishConnection(uri, dbName), connectionTimeout)
}
private def establishConnection(uri: ParsedURI, dbName: String): Future[DefaultDB] =
Try(mongoDriver.connection(uri).database(dbName))
match {
case Success(db) => db
case Failure(err) =>
throw new IllegalStateException(s"Can't connect to database ${printHosts(uri.hosts)}: ${err.getMessage}", err)
}
private def printHosts(hosts: List[(String, Int)]): String = hosts.map { case (host, port) => s"$host:$port" }.mkString(", ")
def connectToDB(uri: ParsedURI, connectionTimeout: FiniteDuration): DefaultDB =
uri.db match {
case Some(dbName) => establishConnection(uri, dbName, connectionTimeout)
case None => throw new IllegalStateException(s"Can't connect to database $uri.")
}
def connectToDB(uri: String, connectionTimeout: FiniteDuration): DefaultDB = connectToDB(parseUri(uri), connectionTimeout)
def parseHosts(hosts: Seq[String]): Seq[(String, Int)] = hosts.map { hostAndPort =>
hostAndPort.split(':').toList match {
case host :: port :: Nil =>
host -> Try(port.toInt).filter(p => p > 0 && p < 65536)
.getOrElse(throw new URIParsingException(s"Could not parse hosts '$hosts' from URI: invalid port '$port'"))
case host :: Nil =>
host -> defaultPort
case _ => throw new URIParsingException(s"Could not parse hosts from URI: invalid definition '$hosts'")
}
}
def parseUri(uri: String): ParsedURI = {
MongoConnection.parseURI(uri) match {
case Success(parsedUri) => parsedUri
case Failure(err) => throw new IllegalStateException(s"Can't parse database uri. $err")
}
}
}
开发者ID:RC-Platform-Disco-Team,项目名称:gatling-mongodb-protocol,代码行数:58,代码来源:MongoUtils.scala
示例3:
//设置package包名称以及导入依赖的类
package com.themillhousegroup.mondrian.test
import org.specs2.mock.mockito.{MockitoStubs, MocksCreation}
import org.specs2.specification.Scope
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.commands._
import reactivemongo.api.{DefaultDB, MongoConnection, MongoConnectionOptions}
trait ScopedMockedReactiveApi extends Scope with MocksCreation with MockitoStubs with MockedConnection {
val mockDB = mock[DefaultDB]
val mockReactiveApi = mock[ReactiveMongoApi]
mockReactiveApi.db returns mockDB
mockDB.connection returns mockConnection
}
trait MockedConnection extends MocksCreation with MockitoStubs {
val mockConnection = mock[MongoConnection]
val mockConnectionOptions = mock[MongoConnectionOptions]
mockConnection.options returns mockConnectionOptions
mockConnectionOptions.writeConcern returns WriteConcern.Default
}
开发者ID:themillhousegroup,项目名称:mondrian,代码行数:24,代码来源:MockedReactiveApi.scala
示例4: Blog
//设置package包名称以及导入依赖的类
package me.lsbengine.server
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import com.github.nscala_time.time.Imports._
import com.typesafe.config.ConfigFactory
import reactivemongo.api.{MongoConnection, MongoDriver}
import sun.misc.Signal
import scala.concurrent.ExecutionContext
object Blog extends App {
val conf = ConfigFactory.load()
implicit val system = ActorSystem("server", conf)
implicit val context = ExecutionContext.Implicits.global
implicit val materializer = ActorMaterializer()(system)
val driver = new MongoDriver
val mongoHost = BlogConfiguration.mongoDBHost
val mongodbName = BlogConfiguration.mongoDBName
val connection: MongoConnection = driver.connection(List(mongoHost))
val hostName = BlogConfiguration.hostName
val publicPort = BlogConfiguration.publicPort
val adminPort = BlogConfiguration.adminPort
val publicService = new PublicService(connection, mongodbName, Http().system.log)
val adminService = new AdminService(connection, mongodbName, Http().system.log)
DateTimeZone.setDefault(DateTimeZone.forID("UTC"))
Http().bindAndHandle(publicService.routes, hostName, publicPort)
Http().bindAndHandle(adminService.routes, hostName, adminPort)
Signal.handle(new Signal("INT"), (_: Signal) => {
shutdown()
})
Signal.handle(new Signal("TERM"), (_: Signal) => {
shutdown()
})
private def shutdown(): Unit = {
println("System is shutting down...")
Http().shutdownAllConnectionPools()
Http().system.terminate()
connection.close()
driver.system.terminate()
driver.close()
system.terminate()
}
}
开发者ID:jrabasco,项目名称:LSBEngine,代码行数:53,代码来源:Blog.scala
示例5: map
//设置package包名称以及导入依赖的类
package utils
import de.flapdoodle.embed.mongo.MongodStarter
import de.flapdoodle.embed.mongo.config.{MongodConfigBuilder, Net}
import de.flapdoodle.embed.mongo.distribution.Version
import de.flapdoodle.embed.process.runtime.Network
import org.specs2.mutable.Specification
import org.specs2.specification.core.Fragments
import reactivemongo.api.{MongoConnection, MongoDriver}
trait EmbeddedConnection extends Specification {
val Port = 12345
private val starter = MongodStarter.getDefaultInstance
private val mongodConfig = new MongodConfigBuilder()
.version(Version.Main.PRODUCTION)
.net(new Net(Port, Network.localhostIsIPv6()))
.build()
private val mongodExecutable = starter.prepare(mongodConfig)
override def map(fs: => Fragments) = startMongo ^ fs ^ stopMongo
private def startMongo = step {
mongodExecutable.start
Thread.sleep(2000)
success
}
private def stopMongo() = step {
Thread.sleep(10000)
mongodExecutable.stop()
success
}
def getConnection: MongoConnection = {
Thread.sleep(2000)
val driver = new MongoDriver
val connection = driver.connection("localhost:12345" :: Nil)
Thread.sleep(2000)
connection
}
}
开发者ID:salceson,项目名称:toik-games-price-comparator,代码行数:42,代码来源:EmbeddedConnection.scala
注:本文中的reactivemongo.api.MongoConnection类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论