Skip to content

Instantly share code, notes, and snippets.

@viktorklang
Created December 19, 2012 16:57
Show Gist options
  • Save viktorklang/4338250 to your computer and use it in GitHub Desktop.
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
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