Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dao
- import java.sql.{Connection, Driver, DriverManager, PreparedStatement}
- import java.util.concurrent.{Executors, ThreadFactory}
- import models.cockroach.Order
- import org.slf4j.MDC
- import play.api.Logger
- import scala.concurrent.{Await, ExecutionContext, Future, Promise}
- import scala.concurrent.duration._
- object AsyncRequestor {
- lazy val ec = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(20))
- def executeRequest(f : () => Unit) : Future[Int] = {
- val promise = Promise[Int]()
- ec.execute(() => {
- val startTime = System.currentTimeMillis()
- f()
- val totalTime : Int = (System.currentTimeMillis() - startTime).toInt
- promise.success(totalTime)
- })
- promise.future
- }
- }
- trait Repository {
- def executeAsync(f : () => Unit) : Future[Int] = {
- AsyncRequestor.executeRequest(f)
- }
- }
- trait ProductRepository extends Repository {
- def getProductByProductId(id: String): Future[Option[Product]]
- def getProductByListProductID(productsID: List[String]): Future[List[Option[Product]]]
- }
- trait OrderRepository extends Repository {
- def insert(order: Order): Future[Int]
- def validationOrder(orderID: String): Future[Int]
- }
- class OrderRepositoryCockroach() extends OrderRepository {
- val logger: Logger = Logger(this.getClass())
- Class.forName("org.postgresql.Driver")
- import java.sql.Connection
- import java.sql.DriverManager
- //val db: Connection = DriverManager.getConnection("jdbc:postgresql://0.0.0.0:26257/octo?sslmode=disable", "root", "")
- //val preparedInsert: PreparedStatement = db.prepareStatement("insert into orders(id, customer_id, state) values(?, ?, ?);")
- override def insert(order: Order): Future[Int] = {
- MDC.put("method", "insert")
- val customerID: String = order.customer_id
- val orderID: String = order.id
- val orderState: String = order.state
- val db: Connection = DriverManager.getConnection("jdbc:postgresql://0.0.0.0:26257/octo?sslmode=disable", "root", "")
- val preparedInsert: PreparedStatement = db.prepareStatement("insert into orders(id, customer_id, state) values(?, ?, ?);")
- preparedInsert.setString(1, orderID)
- preparedInsert.setString(2, customerID)
- preparedInsert.setString(3, orderState)
- val result = executeAsync { () =>
- preparedInsert.executeUpdate()
- }
- //println(s"Insert order with orderID: $orderID and customerID : $customerID. Connection : " + db.getClientInfo)
- result
- }
- override def validationOrder(orderID: String): Future[Int] = ???
- }
- object MyApp {
- def main(args: Array[String]): Unit = {
- val repo = new OrderRepositoryCockroach()
- val startTime = System.currentTimeMillis()
- val orders = (1 to 100)
- .map( i =>
- Order(s"$i", s"c-$i", "", "VALIDATE")
- )
- .map{ order =>
- repo.insert(order)
- }
- import scala.concurrent.ExecutionContext.Implicits.global
- val resultTimes = Await.result(Future.sequence(orders), 10 second)
- val totalTime = System.currentTimeMillis() - startTime
- println("Mean time : " + resultTimes.sum.toDouble / resultTimes.length)
- println("Total time : " + totalTime)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement