Add java friendly methods

This commit is contained in:
Derek Williams 2011-02-28 16:29:03 -07:00
parent 3ba5c9bc0e
commit fbd3bd635c
2 changed files with 13 additions and 17 deletions

View file

@ -5,12 +5,11 @@
package akka.dispatch
import akka.AkkaException
import akka.actor.Actor.spawn
import akka.routing.Dispatcher
import java.util.concurrent.locks.ReentrantLock
import akka.japi.Procedure
import java.util.concurrent. {ConcurrentLinkedQueue, TimeUnit}
import java.util.concurrent. {ConcurrentLinkedQueue, TimeUnit, Callable}
import java.util.concurrent.TimeUnit.{NANOSECONDS => NANOS, MILLISECONDS => MILLIS}
import akka.actor.Actor
import annotation.tailrec
@ -20,20 +19,17 @@ class FutureTimeoutException(message: String) extends AkkaException(message)
object Futures {
/**
* Module with utility methods for working with Futures.
* <pre>
* val future = Futures.future(1000) {
* ... // do stuff
* }
* </pre>
*/
def future[T](body: => T, timeout: Long = Actor.TIMEOUT,
dispatcher: MessageDispatcher = Dispatchers.defaultGlobalDispatcher): Future[T] = {
val f = new DefaultCompletableFuture[T](timeout)
dispatcher.dispatchFuture(FutureInvocation(f.asInstanceOf[CompletableFuture[Any]], () => body))
f
}
def future[T](body: Callable[T]): Future[T] =
Future(body.call)
def future[T](body: Callable[T], timeout: Long): Future[T] =
Future(body.call, timeout)
def future[T](body: Callable[T], dispatcher: MessageDispatcher): Future[T] =
Future(body.call)(dispatcher)
def future[T](body: Callable[T], timeout: Long, dispatcher: MessageDispatcher): Future[T] =
Future(body.call, timeout)(dispatcher)
/**
* (Blocking!)

View file

@ -210,7 +210,7 @@ class FutureSpec extends JUnitSuite {
}).start
}
def futures = actors.zipWithIndex map { case (actor: ActorRef, idx: Int) => actor.!!![Int]((idx, idx * 200 )) }
assert(futures.foldLeft(Futures.future(0))((fr, fa) => fr flatMap (r => fa map (_ + r))).awaitBlocking.result.get === 45)
assert(futures.foldLeft(Future(0))((fr, fa) => for (r <- fr; a <- fa) yield (r + a)).awaitBlocking.result.get === 45)
}
@Test def shouldFoldResultsWithException {