public void start() throws InterruptedException, URISyntaxException { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class); b.handler( new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", new RtspResponseDecoder()); pipeline.addLast("encoder", new RtspRequestEncoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(1024 * 1024 * 64)); pipeline.addLast("handler", new ResponseHandler()); } }); b.option(ChannelOption.SO_KEEPALIVE, true); ChannelFuture channelFutrue = b.connect(host, port).sync(); if (channelFutrue.isSuccess()) { channel = channelFutrue.channel(); URI uri = new URI("rtsp://127.0.0.1:554/hello-world"); HttpRequest request = this.buildOptionsRequest(uri.toASCIIString()); this.send(request); channel.closeFuture().sync(); } } finally { // 优雅退出,释放NIO线程组 group.shutdownGracefully(); } }
public FileChunk get() throws IOException { if (useLocalFile) { startTime = System.currentTimeMillis(); finishTime = System.currentTimeMillis(); state = TajoProtos.FetcherState.FETCH_FINISHED; return fileChunk; } this.startTime = System.currentTimeMillis(); this.state = TajoProtos.FetcherState.FETCH_FETCHING; ChannelFuture future = null; try { future = bootstrap .clone() .connect(new InetSocketAddress(host, port)) .addListener(ChannelFutureListener.CLOSE_ON_FAILURE); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().channel(); if (!future.isSuccess()) { state = TajoProtos.FetcherState.FETCH_FAILED; throw new IOException(future.cause()); } String query = uri.getPath() + (uri.getRawQuery() != null ? "?" + uri.getRawQuery() : ""); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, query); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); LOG.info("Status: " + getState() + ", URI:" + uri); // Send the HTTP request. channel.writeAndFlush(request); // Wait for the server to close the connection. throw exception if failed channel.closeFuture().syncUninterruptibly(); fileChunk.setLength(fileChunk.getFile().length()); return fileChunk; } finally { if (future != null && future.channel().isOpen()) { // Close the channel to exit. future.channel().close().awaitUninterruptibly(); } this.finishTime = System.currentTimeMillis(); LOG.info( "Fetcher finished:" + (finishTime - startTime) + " ms, " + getState() + ", URI:" + uri); } }