@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 } } }
/** * A variant of {@link #ReactorNettyTcpClient(String, int, ReactorNettyCodec)} * that still manages the lifecycle of the {@link TcpClient} and underlying * resources, but allows for direct configuration of other properties of the * client through a {@code Function<TcpClient, TcpClient>}. * @param clientConfigurer the configurer function * @param codec for encoding and decoding the input/output byte streams * @since 5.1.3 * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(Function<TcpClient, TcpClient> clientConfigurer, ReactorNettyCodec<P> codec) { Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = clientConfigurer.apply(TcpClient .create(this.poolResources) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel()))); }
@Override public ListenableFuture<Void> shutdown() { if (this.stopping) { SettableListenableFuture<Void> future = new SettableListenableFuture<>(); future.set(null); return future; } this.stopping = true; Mono<Void> result; if (this.channelGroup != null) { result = FutureMono.from(this.channelGroup.close()); if (this.loopResources != null) { result = result.onErrorResume(ex -> Mono.empty()).then(this.loopResources.disposeLater()); } if (this.poolResources != null) { result = result.onErrorResume(ex -> Mono.empty()).then(this.poolResources.disposeLater()); } result = result.onErrorResume(ex -> Mono.empty()).then(stopScheduler()); } else { result = stopScheduler(); } return new MonoToListenableFutureAdapter<>(result); }
connectionProvider = ConnectionProvider.newConnection(); } else if (pool.getType() == FIXED) { connectionProvider = ConnectionProvider.fixed(pool.getName(), pool.getMaxConnections(), pool.getAcquireTimeout()); } else { connectionProvider = ConnectionProvider.elastic(pool.getName());
@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()); }
/** * Callback for server channel factory selection. * * @param group the source {@link EventLoopGroup} to assign a loop from * * @return a {@link Class} target for the underlying {@link ServerChannel} factory */ default Class<? extends ServerChannel> onServerChannel(EventLoopGroup group) { return preferNative() ? DefaultLoopNativeDetector.getInstance().getServerChannel(group) : NioServerSocketChannel.class; }
/** * Callback for client or generic channel factory selection. * * @param group the source {@link EventLoopGroup} to assign a loop from * * @return a {@link Class} target for the underlying {@link Channel} factory */ default Class<? extends Channel> onChannel(EventLoopGroup group) { return preferNative() ? DefaultLoopNativeDetector.getInstance().getChannel(group) : NioSocketChannel.class; }
/** * Callback for UDP channel factory selection. * * @param group the source {@link EventLoopGroup} to assign a loop from * * @return a {@link Class} target for the underlying {@link Channel} factory */ default Class<? extends DatagramChannel> onDatagramChannel(EventLoopGroup group) { return preferNative() ? DefaultLoopNativeDetector.getInstance().getDatagramChannel(group) : NioDatagramChannel.class; }
/** * Simple constructor with the host and port to use to connect to. * <p>This constructor manages the lifecycle of the {@link TcpClient} and * underlying resources such as {@link ConnectionProvider}, * {@link LoopResources}, and {@link ChannelGroup}. * <p>For full control over the initialization and lifecycle of the * TcpClient, use {@link #ReactorNettyTcpClient(TcpClient, ReactorNettyCodec)}. * @param host the host to connect to * @param port the port to connect to * @param codec for encoding and decoding the input/output byte streams * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(String host, int port, ReactorNettyCodec<P> codec) { Assert.notNull(host, "host is required"); Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = TcpClient.create(this.poolResources) .host(host).port(port) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel())); }
@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 } } }
@Override public ListenableFuture<Void> shutdown() { if (this.stopping) { SettableListenableFuture<Void> future = new SettableListenableFuture<>(); future.set(null); return future; } this.stopping = true; Mono<Void> result; if (this.channelGroup != null) { result = FutureMono.from(this.channelGroup.close()); if (this.loopResources != null) { result = result.onErrorResume(ex -> Mono.empty()).then(this.loopResources.disposeLater()); } if (this.poolResources != null) { result = result.onErrorResume(ex -> Mono.empty()).then(this.poolResources.disposeLater()); } result = result.onErrorResume(ex -> Mono.empty()).then(stopScheduler()); } else { result = stopScheduler(); } return new MonoToListenableFutureAdapter<>(result); }
@Override public boolean isDisposed() { return defaultLoops.isDisposed() && defaultProvider.isDisposed(); }
/** * Callback for server channel factory selection. * * @param group the source {@link EventLoopGroup} to assign a loop from * * @return a {@link Class} target for the underlying {@link ServerChannel} factory */ default Class<? extends ServerChannel> onServerChannel(EventLoopGroup group) { return preferNative() ? DefaultLoopNativeDetector.getInstance().getServerChannel(group) : NioServerSocketChannel.class; }
/** * Callback for client or generic channel factory selection. * * @param group the source {@link EventLoopGroup} to assign a loop from * * @return a {@link Class} target for the underlying {@link Channel} factory */ default Class<? extends Channel> onChannel(EventLoopGroup group) { return preferNative() ? DefaultLoopNativeDetector.getInstance().getChannel(group) : NioSocketChannel.class; }
/** * Callback for UDP channel factory selection. * * @param group the source {@link EventLoopGroup} to assign a loop from * * @return a {@link Class} target for the underlying {@link Channel} factory */ default Class<? extends DatagramChannel> onDatagramChannel(EventLoopGroup group) { return preferNative() ? DefaultLoopNativeDetector.getInstance().getDatagramChannel(group) : NioDatagramChannel.class; }
/** * A variant of {@link #ReactorNettyTcpClient(String, int, ReactorNettyCodec)} * that still manages the lifecycle of the {@link TcpClient} and underlying * resources, but allows for direct configuration of other properties of the * client through a {@code Function<TcpClient, TcpClient>}. * @param clientConfigurer the configurer function * @param codec for encoding and decoding the input/output byte streams * @since 5.1.3 * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(Function<TcpClient, TcpClient> clientConfigurer, ReactorNettyCodec<P> codec) { Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = clientConfigurer.apply(TcpClient .create(this.poolResources) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel()))); }
@Test public void localResourcesViaSupplier() throws Exception { this.resourceFactory.setUseGlobalResources(false); this.resourceFactory.setConnectionProviderSupplier(() -> this.connectionProvider); this.resourceFactory.setLoopResourcesSupplier(() -> this.loopResources); this.resourceFactory.afterPropertiesSet(); ConnectionProvider connectionProvider = this.resourceFactory.getConnectionProvider(); LoopResources loopResources = this.resourceFactory.getLoopResources(); assertSame(this.connectionProvider, connectionProvider); assertSame(this.loopResources, loopResources); verifyNoMoreInteractions(this.connectionProvider, this.loopResources); this.resourceFactory.destroy(); // Managed (destroy disposes).. verify(this.connectionProvider).dispose(); verify(this.loopResources).dispose(); verifyNoMoreInteractions(this.connectionProvider, this.loopResources); }
/** * Simple constructor with the host and port to use to connect to. * <p>This constructor manages the lifecycle of the {@link TcpClient} and * underlying resources such as {@link ConnectionProvider}, * {@link LoopResources}, and {@link ChannelGroup}. * <p>For full control over the initialization and lifecycle of the * TcpClient, use {@link #ReactorNettyTcpClient(TcpClient, ReactorNettyCodec)}. * @param host the host to connect to * @param port the port to connect to * @param codec for encoding and decoding the input/output byte streams * @see org.springframework.messaging.simp.stomp.StompReactorNettyCodec */ public ReactorNettyTcpClient(String host, int port, ReactorNettyCodec<P> codec) { Assert.notNull(host, "host is required"); Assert.notNull(codec, "ReactorNettyCodec is required"); this.channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); this.loopResources = LoopResources.create("tcp-client-loop"); this.poolResources = ConnectionProvider.elastic("tcp-client-pool"); this.codec = codec; this.tcpClient = TcpClient.create(this.poolResources) .host(host).port(port) .runOn(this.loopResources, false) .doOnConnected(conn -> this.channelGroup.add(conn.channel())); }