|
Play Framework/Scala example source code file (NettyFuture.scala)
The NettyFuture.scala Play Framework example source code
/*
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
*/
package play.core.server.netty
import org.jboss.netty.channel.{ Channel, ChannelFuture, ChannelFutureListener }
import play.api.libs.concurrent._
import scala.concurrent.{ Future, Promise, ExecutionContext, CanAwait }
import scala.concurrent.duration.Duration
import java.util.concurrent.TimeUnit
import scala.util._
/**
* Allows a NettyFuture to be convert to a Scala Future
*/
object NettyFuture {
implicit class ToScala(channelFuture: ChannelFuture) {
def toScala: Future[Channel] = {
val promise = Promise[Channel]()
channelFuture.addListener(new ChannelFutureListener {
def operationComplete(future: ChannelFuture) = {
if (future.isSuccess) {
promise.success(future.getChannel)
} else if (future.isCancelled) {
promise.failure(new RuntimeException("Future cancelled"))
} else {
promise.failure(future.getCause)
}
}
})
promise.future
}
}
}
Other Play Framework source code examplesHere is a short list of links related to this Play Framework NettyFuture.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.