@Override public void afterPropertiesSet() { if (this.useGlobalResources) { Assert.isTrue(this.loopResources == null && this.connectionProvider == null, "'useGlobalResources' is mutually exclusive with explicitly configured resources"); HttpResources httpResources = HttpResources.get(); if (this.globalResourcesConsumer != null) { this.globalResourcesConsumer.accept(httpResources); } this.connectionProvider = httpResources; this.loopResources = httpResources; } else { if (this.loopResources == null) { this.manageLoopResources = true; this.loopResources = this.loopResourcesSupplier.get(); } if (this.connectionProvider == null) { this.manageConnectionProvider = true; this.connectionProvider = this.connectionProviderSupplier.get(); } } }
@Override public void destroy() { if (this.useGlobalResources) { HttpResources.disposeLoopsAndConnections(); } else { try { ConnectionProvider provider = this.connectionProvider; if (provider != null && this.manageConnectionProvider) { provider.dispose(); } } catch (Throwable ex) { // ignore } try { LoopResources resources = this.loopResources; if (resources != null && this.manageLoopResources) { resources.dispose(); } } catch (Throwable ex) { // ignore } } }
@Test public void globalResources() throws Exception { this.resourceFactory.setUseGlobalResources(true); this.resourceFactory.afterPropertiesSet(); HttpResources globalResources = HttpResources.get(); assertSame(globalResources, this.resourceFactory.getConnectionProvider()); assertSame(globalResources, this.resourceFactory.getLoopResources()); assertFalse(globalResources.isDisposed()); this.resourceFactory.destroy(); assertTrue(globalResources.isDisposed()); }
/** * * Return a new cookies holder from client response headers * @param headers client response headers * @return a new cookies holder from client response headers */ public static Cookies newClientResponseHolder(HttpHeaders headers, ClientCookieDecoder decoder) { return new Cookies(headers, HttpHeaderNames.SET_COOKIE, true, decoder); }
@Override public HttpClientOperations addHandlerLast(ChannelHandler handler) { super.addHandlerLast(handler); return this; }
@Override public HttpClientOperations addHandlerFirst(ChannelHandler handler) { super.addHandlerFirst(handler); return this; }
/** * Update event loops resources and return the global HTTP resources * * @return the global HTTP resources */ public static HttpResources set(ConnectionProvider provider) { return getOrCreate(httpResources, null, provider, ON_HTTP_NEW, "http"); }
@Override public HttpClientOperations replaceHandler(String name, ChannelHandler handler) { super.replaceHandler(name, handler); return this; }
@Override public HttpClientOperations removeHandler(String name) { super.removeHandler(name); return this; }
@Override public final HttpClientOperations onDispose(Disposable onDispose) { super.onDispose(onDispose); return this; }
@Override public <S> NettyOutbound sendUsing(Callable<? extends S> sourceInput, BiFunction<? super Connection, ? super S, ?> mappedInput, Consumer<? super S> sourceCleanup) { return parent.sendUsing(sourceInput, mappedInput, sourceCleanup); }
@Override @SuppressWarnings("FutureReturnValueIgnored") public HttpClientOperations addHandler(String name, ChannelHandler handler) { // Returned value is deliberately ignored super.addHandler(name, handler); return this; }
@Override public void afterPropertiesSet() { if (this.useGlobalResources) { Assert.isTrue(this.loopResources == null && this.connectionProvider == null, "'useGlobalResources' is mutually exclusive with explicitly configured resources"); HttpResources httpResources = HttpResources.get(); if (this.globalResourcesConsumer != null) { this.globalResourcesConsumer.accept(httpResources); } this.connectionProvider = httpResources; this.loopResources = httpResources; } else { if (this.loopResources == null) { this.manageLoopResources = true; this.loopResources = this.loopResourcesSupplier.get(); } if (this.connectionProvider == null) { this.manageConnectionProvider = true; this.connectionProvider = this.connectionProviderSupplier.get(); } } }
@Override public void destroy() { if (this.useGlobalResources) { HttpResources.disposeLoopsAndConnections(); } else { try { ConnectionProvider provider = this.connectionProvider; if (provider != null && this.manageConnectionProvider) { provider.dispose(); } } catch (Throwable ex) { // ignore } try { LoopResources resources = this.loopResources; if (resources != null && this.manageLoopResources) { resources.dispose(); } } catch (Throwable ex) { // ignore } } }
/** * * Return a new cookies holder from client response headers * @param headers client response headers * @return a new cookies holder from client response headers */ public static Cookies newClientResponseHolder(HttpHeaders headers, ClientCookieDecoder decoder) { return new Cookies(headers, HttpHeaderNames.SET_COOKIE, true, decoder); }
@Override public HttpClientOperations addHandlerLast(String name, ChannelHandler handler) { super.addHandlerLast(name, handler); return this; }
@Override public HttpClientOperations addHandlerFirst(String name, ChannelHandler handler) { super.addHandlerFirst(name, handler); return this; }
/** * Update pooling resources and return the global HTTP resources * * @return the global HTTP resources */ public static HttpResources set(LoopResources loops) { return getOrCreate(httpResources, loops, null, ON_HTTP_NEW, "http"); }
@Override public HttpClientOperations replaceHandler(String name, ChannelHandler handler) { super.replaceHandler(name, handler); return this; }
@Test public void localResources() throws Exception { this.resourceFactory.setUseGlobalResources(false); this.resourceFactory.afterPropertiesSet(); ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); LoopResources loopResources = this.resourceFactory.getLoopResources(); assertNotSame(HttpResources.get(), connectionProvider); assertNotSame(HttpResources.get(), loopResources); // The below does not work since ConnectionPoolProvider simply checks if pool is empty. // assertFalse(connectionProvider.isDisposed()); assertFalse(loopResources.isDisposed()); this.resourceFactory.destroy(); assertTrue(connectionProvider.isDisposed()); assertTrue(loopResources.isDisposed()); }