This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stackTrace(implicit ec: ExecutionContext): Any = { | |
Await.result( | |
Future.successful(1) | |
.map(v => v + 1) | |
.flatMap(v => Future.successful(v)) | |
.map { v => | |
new IllegalStateException().printStackTrace() | |
v | |
}, | |
10.seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RpcClient(ownEc: ExecutionContext, appEc: ExecutionContext) { | |
def call: Future[String] = | |
Future("a")(ownEc) | |
.map(identity)(appEc) // here we move back to an application execution context | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rpcCall: Future[String] | |
def daoCall(id: String): Future[Int] | |
def extractId(idStr: String): Future[String] | |
def convertDbValue(value: Int): Future[Int] | |
import directExecutionContext | |
for { | |
idStr <- rpcCall | |
id <- extractId(idStr) // executed in RPC execution context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def map[U](f: T => U) | |
(implicit ec: ExecutionContext): Future[U] = { | |
val promise = Promise() | |
if (isCompleted) // in real code this is an atomic operation + pattern matching | |
// even if it's completed, callback is submitted to ExecutionContext | |
ec.execute(() => { | |
f(result) | |
}) | |
else | |
registerCallback(f)(ec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def smartMap[U](f: T => U)(implicit ec: ExecutionContext): Future[U] = | |
if (future.isCompleted) { | |
if (future.value.get.isSuccess) | |
wrap(f(future.value.get.get)) | |
else | |
future.asInstanceOf[Future[U]] | |
} else { | |
future.map(f)(ec) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var future = Future.successful(0) | |
for (_ <- 1 to 100) { | |
future = future.map(v => v + 1) | |
} | |
require(Await.result(future, 10.seconds) == 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rpcCall: Future[String] = ??? | |
rpcCall | |
.map(_.split('_')) | |
.filter(_.length < 4) | |
.map { | |
case Array(single) => single | |
case Array(first, second@_) => first | |
case Array(first@_, second, third@_) => third | |
case _ => "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RobustDao(jdbcTemplate: JdbcTemplate, | |
blockingEc: ExecutionContext, | |
applicationEc: ExecutionContext) { | |
def get(id: String): Future[Option[RichDomainObject]] = { | |
Future(getBlocking(id))(blockingEc) | |
.map(blob => blob.map(parseJson))(applicationEc) | |
} | |
private def getBlocking(id: String): Option[String] = | |
jdbcTemplate.queryForObject( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait ExecutionContext { | |
def execute(runnable: Runnable): Unit | |
def reportFailure(cause: Throwable): Unit | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit val blockingExecutionContext = | |
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(50)) | |
class MyDao(jdbcTemplate: JdbcTemplate)(implicit ec: ExecutionContext) { | |
def get(id: String): Future[Option[RichDomainObject]] = | |
Future(getBlocking(id)) | |
private def getBlocking(id: String): Option[RichDomainObject] = | |
jdbcTemplate.queryForObject( | |
"SELECT blob FROM table WHERE id = ?", |