Created
December 19, 2012 16:57
-
-
Save viktorklang/4338250 to your computer and use it in GitHub Desktop.
Shows how you can use the Promise API of SIP-14 to bridge between Netty ChannelFutures and scala.concurrent.Future
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
object NettyFutureBridge { | |
import scala.concurrent.{ Promise, Future } | |
import scala.util.Try | |
import java.util.concurrent.CancellationException | |
import org.jboss.netty.channel.{ Channel, ChannelFuture, ChannelFutureListener } | |
def apply(nettyFuture: ChannelFuture): Future[Channel] = { | |
val p = Promise[Channel]() | |
nettyFuture.addListener(new ChannelFutureListener { | |
def operationComplete(future: ChannelFuture): Unit = p complete Try( | |
if (future.isSuccess) future.getChannel | |
else if (future.isCancelled) throw new CancellationException | |
else throw future.getCause) | |
}) | |
p.future | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment