List of usage examples for io.netty.channel ChannelFutureListener CLOSE
ChannelFutureListener CLOSE
To view the source code for io.netty.channel ChannelFutureListener CLOSE.
Click Source Link
From source file:com.flysoloing.learning.network.netty.http2.tiles.Http1RequestHandler.java
License:Apache License
@Override protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency, final FullHttpResponse response, final FullHttpRequest request) { HttpUtil.setContentLength(response, response.content().readableBytes()); ctx.executor().schedule(new Runnable() { public void run() { if (isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.writeAndFlush(response); } else { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }// ww w. j a v a2 s . c o m } }, latency, TimeUnit.MILLISECONDS); }
From source file:com.flysoloing.learning.network.netty.spdy.server.SpdyServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; if (is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }//from ww w .j a va 2 s .com boolean keepAlive = isKeepAlive(req); ByteBuf content = Unpooled.copiedBuffer("Hello World " + new Date(), CharsetUtil.UTF_8); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); } } }
From source file:com.friz.update.network.UpdateSessionContext.java
License:Open Source License
public void writeFailure(int status) { channel.writeAndFlush(new UpdateResponseEvent(status)).addListener(ChannelFutureListener.CLOSE); }
From source file:com.gdut.Netty_testing.time_server.server.TimeServerHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { System.out.println("Inbound --- TimeServerHandler"); ChannelFuture f = ctx.writeAndFlush(new UnixTime()); f.addListener(ChannelFutureListener.CLOSE); }
From source file:com.github.ambry.rest.NettyResponseChannel.java
License:Open Source License
/** * Completes the request by closing the request and network channel (if {@code closeNetworkChannel} is {@code true}). * </p>/* w w w. ja v a 2 s . c o m*/ * May also close the channel if the class internally is forcing a close (i.e. if {@link #close()} is called. * @param closeNetworkChannel network channel is closed if {@code true}. */ private void completeRequest(boolean closeNetworkChannel) { if ((closeNetworkChannel || forceClose) && ctx.channel().isOpen()) { writeFuture.addListener(ChannelFutureListener.CLOSE); logger.trace("Requested closing of channel {}", ctx.channel()); } closeRequest(); responseComplete = true; }
From source file:com.github.http.helloworld.HttpHelloWorldServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }//from w w w . j a va2 s . c o m boolean keepAlive = HttpHeaders.isKeepAlive(req); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); ctx.write(response); } } }
From source file:com.github.jonbonazza.puni.core.handlers.RequestHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { if (!req.getDecoderResult().isSuccess()) { sendError(ctx, HttpResponseStatus.BAD_REQUEST); return;// w ww .j a va 2 s . c o m } HttpRequest httpRequest = new HttpRequest(req); HttpHandler handler = muxer.mux(httpRequest); if (handler == null) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } HttpResponse resp = handler.handle(httpRequest); ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE); }
From source file:com.github.jonbonazza.puni.core.handlers.RequestHandler.java
License:Apache License
private void sendError(ChannelHandlerContext context, HttpResponseStatus status) { HttpResponse resp = new HttpResponse(status, String.valueOf(status)); resp.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); context.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE); }
From source file:com.github.nettybook.ch7.junit.TelnetServerHandlerNetty.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String request) throws Exception { System.out.println(request + " channelActive "); // Generate and write a response. String response;//from w w w . j a va 2s . c om boolean close = false; if (request.isEmpty()) { response = "Please type something.\r\n"; } else if ("bye".equals(request.toLowerCase())) { response = "Have a good day!\r\n"; close = true; } else { response = "Did you say '" + request + "'?\r\n"; } // We do not need to write a ChannelBuffer here. // We know the encoder inserted at TelnetPipelineFactory will do the conversion. ChannelFuture future = ctx.write(response); // Close the connection after sending 'Have a good day!' // if the client has sent 'bye'. System.out.println(response); if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.github.nettybook.ch7.junit.TelnetServerHandlerV3.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String request) throws Exception { ResponseGenerator generator = new ResponseGenerator(request); // Generate and write a response. String response = generator.response(); // We do not need to write a ChannelBuffer here. // We know the encoder inserted at TelnetPipelineFactory will do the // conversion. ChannelFuture future = ctx.write(response); // Close the connection after sending 'Have a good day!' // if the client has sent 'bye'. if (generator.isClose()) { future.addListener(ChannelFutureListener.CLOSE); }//from www. j a v a2 s. c om }