1

約 10 から 15 のクエリを同時に実行する必要があります。これらのクエリは相互に依存していますが、これらのクエリを 1 つの未来にまとめました。それ以外の場合、クエリごとに 1 つの先物があります。ここで、すべてのクエリが実行されたときに、未来を含む関数からの成功または失敗を示す応答が必要です。

val f1 = future{SQL("insert into user_general_info (user_id, userFName, userLName, displayAs)" +
  "values(" + userId + ",'" + form.fname + "','" + form.lname + "','1')").executeInsert();
}

val f2 = future {
  SQL("insert into personal('" + userId + "',1,1,0,0,1,'ALL',0,'E')").executeInsert();
}

/*
and so on...upto about f1 to f14 futures...
*/

今私がしたことは:

val job = for {
  a1 <- f1
  a2 <- f2
  a3 <- f3
  a4 <- f4
  a5 <- f5
  a6 <- f6
  a7 <- f7
  a8 <- f8
  a9 <- f9
  a10 <- f10
  a11 <- f11
  a12 <- f12
  a13 <- f13
  a14 <- f14
} yield ()

var res: Boolean = false

job.onSuccess {
  case result => res = true
}

if(res)
  List((1, userId, username)) //1 means success
else
  List((-2, userId, username)) //-2 means failure

問題は、すべてのクエリが実行されず、応答List((-2,userId,username))が変数 res に基づいて送信されることです。しかし、List((1,userId,username))すべての先物が完了したときに返されるべきでした。ヘルプ...

4

1 に答える 1

6

を使用Futures.sequenceして、future のリストを 1 つの future に変換できます。

Composing Futuresのコード例:

// oddActor returns odd numbers sequentially from 1 as a List[Future[Int]]
val listOfFutures = List.fill(100)(akka.pattern.ask(oddActor, GetNext).mapTo[Int])

// now we have a Future[List[Int]]
val futureList = Future.sequence(listOfFutures)

// Find the sum of the odd numbers
val oddSum = futureList.map(_.sum)
oddSum foreach println

Option余談ですが、たとえば、成功または失敗を示すために を返すことをお勧めします。

if(res) Some(List(userId, userName))
else None
于 2013-10-10T14:40:25.023 に答える