Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. package dao
  2.  
  3. import java.sql.{Connection, Driver, DriverManager, PreparedStatement}
  4. import java.util.concurrent.{Executors, ThreadFactory}
  5.  
  6. import models.cockroach.Order
  7. import org.slf4j.MDC
  8. import play.api.Logger
  9.  
  10. import scala.concurrent.{Await, ExecutionContext, Future, Promise}
  11. import scala.concurrent.duration._
  12.  
  13. object AsyncRequestor {
  14.  
  15. lazy val ec = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(20))
  16.  
  17. def executeRequest(f : () => Unit) : Future[Int] = {
  18. val promise = Promise[Int]()
  19. ec.execute(() => {
  20. val startTime = System.currentTimeMillis()
  21. f()
  22. val totalTime : Int = (System.currentTimeMillis() - startTime).toInt
  23. promise.success(totalTime)
  24. })
  25. promise.future
  26. }
  27.  
  28. }
  29.  
  30. trait Repository {
  31. def executeAsync(f : () => Unit) : Future[Int] = {
  32. AsyncRequestor.executeRequest(f)
  33. }
  34. }
  35.  
  36. trait ProductRepository extends Repository {
  37.  
  38. def getProductByProductId(id: String): Future[Option[Product]]
  39.  
  40. def getProductByListProductID(productsID: List[String]): Future[List[Option[Product]]]
  41.  
  42. }
  43.  
  44. trait OrderRepository extends Repository {
  45.  
  46. def insert(order: Order): Future[Int]
  47.  
  48. def validationOrder(orderID: String): Future[Int]
  49.  
  50. }
  51.  
  52. class OrderRepositoryCockroach() extends OrderRepository {
  53. val logger: Logger = Logger(this.getClass())
  54. Class.forName("org.postgresql.Driver")
  55.  
  56. import java.sql.Connection
  57. import java.sql.DriverManager
  58.  
  59. //val db: Connection = DriverManager.getConnection("jdbc:postgresql://0.0.0.0:26257/octo?sslmode=disable", "root", "")
  60.  
  61. //val preparedInsert: PreparedStatement = db.prepareStatement("insert into orders(id, customer_id, state) values(?, ?, ?);")
  62.  
  63. override def insert(order: Order): Future[Int] = {
  64. MDC.put("method", "insert")
  65. val customerID: String = order.customer_id
  66. val orderID: String = order.id
  67. val orderState: String = order.state
  68. val db: Connection = DriverManager.getConnection("jdbc:postgresql://0.0.0.0:26257/octo?sslmode=disable", "root", "")
  69. val preparedInsert: PreparedStatement = db.prepareStatement("insert into orders(id, customer_id, state) values(?, ?, ?);")
  70. preparedInsert.setString(1, orderID)
  71. preparedInsert.setString(2, customerID)
  72. preparedInsert.setString(3, orderState)
  73. val result = executeAsync { () =>
  74. preparedInsert.executeUpdate()
  75. }
  76. //println(s"Insert order with orderID: $orderID and customerID : $customerID. Connection : " + db.getClientInfo)
  77. result
  78. }
  79.  
  80. override def validationOrder(orderID: String): Future[Int] = ???
  81. }
  82.  
  83. object MyApp {
  84.  
  85. def main(args: Array[String]): Unit = {
  86. val repo = new OrderRepositoryCockroach()
  87.  
  88. val startTime = System.currentTimeMillis()
  89.  
  90. val orders = (1 to 100)
  91. .map( i =>
  92. Order(s"$i", s"c-$i", "", "VALIDATE")
  93. )
  94. .map{ order =>
  95. repo.insert(order)
  96. }
  97.  
  98. import scala.concurrent.ExecutionContext.Implicits.global
  99. val resultTimes = Await.result(Future.sequence(orders), 10 second)
  100.  
  101. val totalTime = System.currentTimeMillis() - startTime
  102.  
  103. println("Mean time : " + resultTimes.sum.toDouble / resultTimes.length)
  104. println("Total time : " + totalTime)
  105.  
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement