public HttpClient getHttpClient() { if (httpClient == null) { synchronized (this) { if (httpClient == null) { httpClient = new NettyHttpClient(); } } } return httpClient; }
@Override public void onException(Throwable throwable) { callback.onException(throwable); endTime = System.currentTimeMillis(); destroy(); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { RequestContext context = ctx.channel().attr(client.requestKey).getAndSet(null); try { if (context != null && context.tryDone()) { LOGGER.debug("receive server response, request={}, response={}", context, msg); HttpResponse response = (HttpResponse) msg; if (isRedirect(response)) { if (context.onRedirect(response)) { String location = getRedirectLocation(context.request, response); if (location != null && location.length() > 0) { context.cancelled.set(false); context.request.setUri(location); client.request(context); return; } } } context.onResponse(response); } else { LOGGER.warn("receive server response but timeout, request={}, response={}", context, msg); } } finally { client.pool.tryRelease(ctx.channel()); ReferenceCountUtil.release(msg); } }
private void writeRequest(Channel channel, RequestContext context) { channel.attr(requestKey).set(context); pool.attachHost(context.host, channel); channel.writeAndFlush(context.request).addListener((ChannelFutureListener) future -> { if (!future.isSuccess()) { RequestContext info = future.channel().attr(requestKey).getAndSet(null); info.tryDone(); info.onFailure(503, "Service Unavailable"); LOGGER.debug("request failure request={}", info); pool.tryRelease(future.channel()); } }); }
Channel channel = pool.tryAcquire(host); if (channel == null) { final long startCreate = System.currentTimeMillis(); LOGGER.debug("create new channel cost={}", (System.currentTimeMillis() - startCreate)); if (future.isSuccess()) {//3.1.把请求写到http server writeRequest(future.channel(), context); } else {//3.2如果链接创建失败,直接返回客户端网关超时 context.tryDone(); context.onFailure(504, "Gateway Timeout"); LOGGER.warn("create new channel failure, request={}", context); } else { writeRequest(channel, context);
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { RequestContext context = ctx.channel().attr(client.requestKey).getAndSet(null); try { if (context != null && context.tryDone()) { context.onException(cause); } } finally { client.pool.tryRelease(ctx.channel()); } LOGGER.error("http client caught an ex, info={}", context, cause); }
private void call(String method, String url, final HttpCallback cb) { ... client.newCall(request).enqueue(new Callback() { Handler mainHandler = new Handler(context.getMainLooper()); @Override public void onFailure(Request request,final Throwable throwable) { mainHandler.post(new Runnable() { @Override public void run() { cb.onFailure(null, throwable); } }); } @Override public void onResponse(final Response response) throws IOException { mainHandler.post(new Runnable() { @Override public void run() { if (!response.isSuccessful()) { cb.onFailure(response, null); return; } cb.onSuccess(response); } }); } }); }
httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage
@Override public void onResponse(HttpResponse response) { callback.onResponse(response); endTime = System.currentTimeMillis(); destroy(); }
@Override public boolean onRedirect(HttpResponse response) { endTime = System.currentTimeMillis(); return callback.onRedirect(response); }
public RequestContext(FullHttpRequest request, HttpCallback callback) { this.callback = callback; this.request = request; this.uri = request.uri(); this.readTimeout = parseTimeout(); }
@Override protected void doStop(Listener listener) throws Throwable { pool.close(); workerGroup.shutdownGracefully(); timer.stop(); listener.onSuccess(); } }
@Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new HttpResponseDecoder()); ch.pipeline().addLast("aggregator", new HttpObjectAggregator(maxContentLength)); ch.pipeline().addLast("encoder", new HttpRequestEncoder()); ch.pipeline().addLast("handler", new HttpClientHandler(NettyHttpClient.this)); } });
@Override protected void start() { mPushServer.getHttpClient().syncStart(); DnsMappingManager.create().start(); startNext(); }
@Override protected void stop() { stopNext(); mPushServer.getHttpClient().syncStop(); DnsMappingManager.create().stop(); } }
httpClient.request(new RequestContext(request, new DefaultHttpCallback(message))); } catch (Exception e) { HttpResponseMessage
public HttpClient getHttpClient() { if (httpClient == null) { synchronized (this) { if (httpClient == null) { httpClient = new NettyHttpClient(); } } } return httpClient; }