Created
February 13, 2010 22:52
-
-
Save cgbystrom/303712 to your computer and use it in GitHub Desktop.
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
import java.net.InetSocketAddress; | |
import java.net.URI; | |
import java.util.concurrent.Executors; | |
import org.jboss.netty.bootstrap.ClientBootstrap; | |
import org.jboss.netty.channel.ChannelFactory; | |
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; | |
import org.jboss.netty.handler.codec.http.DefaultHttpRequest; | |
import org.jboss.netty.handler.codec.http.HttpHeaders; | |
import org.jboss.netty.handler.codec.http.HttpMethod; | |
import org.jboss.netty.handler.codec.http.HttpRequest; | |
import org.jboss.netty.handler.codec.http.HttpVersion | |
import org.jboss.netty.channel.ChannelFutureListener | |
import org.jboss.netty.channel.ChannelPipelineCoverage | |
import org.jboss.netty.handler.codec.http.HttpResponseDecoder | |
import org.jboss.netty.channel.SimpleChannelHandler | |
import org.jboss.netty.handler.codec.http.HttpRequestEncoder | |
import org.jboss.netty.channel.ChannelFuture | |
import org.jboss.netty.handler.codec.http.HttpChunkAggregator; | |
@ChannelPipelineCoverage("one") | |
class ChannelHandlerOne extends SimpleChannelHandler {} | |
ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); | |
def getUrl = { url, onComplete -> | |
URI uri = new URI(url as String) | |
port = uri.port == -1 ? 80 : uri.port | |
ClientBootstrap bootstrap = new ClientBootstrap(factory); | |
def handler = [ | |
messageReceived: { ctx, e -> onComplete(e.message) }, | |
exceptionCaught: { ctx, e -> e.cause.printStackTrace(System.err); e.channel.close() } | |
] | |
bootstrap.pipeline.with { | |
addLast("decoder", new HttpResponseDecoder()) | |
addLast("aggregator", new HttpChunkAggregator(1048576)); | |
addLast("encoder", new HttpRequestEncoder()) | |
addLast("handler", handler as ChannelHandlerOne) | |
} | |
def onConnected = { ChannelFuture cf -> | |
//println "Channel connected" | |
if (!cf.success) { cf.getCause().printStackTrace(); return } | |
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, uri.getPath()); | |
request.addHeader(HttpHeaders.Names.HOST, uri.host); | |
request.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); | |
cf.channel.write(request) | |
def onClosed = { /*println "Channel closed";*/ } | |
cf.channel.closeFuture.addListener([operationComplete: onClosed] as ChannelFutureListener); | |
} | |
bootstrap.connect(new InetSocketAddress(uri.host, port)).addListener([operationComplete: onConnected] as ChannelFutureListener) | |
} | |
// Interface to expose | |
getUrl("http://localhost:8080/wait") { println "Slow site complete!" } | |
getUrl("http://basun.sunet.se/sunetinfo.html") { println "Fast site complete!" } | |
//factory.releaseExternalResources(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment