|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.http.client.reactive; |
| 17 | + |
| 18 | +import reactor.netty.http.HttpResources; |
| 19 | +import reactor.netty.resources.ConnectionProvider; |
| 20 | +import reactor.netty.resources.LoopResources; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.DisposableBean; |
| 23 | +import org.springframework.beans.factory.InitializingBean; |
| 24 | +import org.springframework.lang.Nullable; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * Factory to manage Reactor Netty resources, i.e. {@link LoopResources} for |
| 29 | + * event loop threads, and {@link ConnectionProvider} for the connection pool, |
| 30 | + * within the lifecycle of a Spring {@code ApplicationContext}. |
| 31 | + * |
| 32 | + * <p>This factory implements {@link InitializingBean} and {@link DisposableBean} |
| 33 | + * and is expected typically to be declared as a Spring-managed bean. |
| 34 | + * |
| 35 | + * @author Rossen Stoyanchev |
| 36 | + * @since 5.1 |
| 37 | + */ |
| 38 | +public class ReactorResourceFactory implements InitializingBean, DisposableBean { |
| 39 | + |
| 40 | + private boolean globalResources = true; |
| 41 | + |
| 42 | + @Nullable |
| 43 | + private ConnectionProvider connectionProvider; |
| 44 | + |
| 45 | + @Nullable |
| 46 | + private LoopResources loopResources; |
| 47 | + |
| 48 | + private String threadPrefix = "reactor-http"; |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Whether to expose and manage the global Reactor Netty resources from the |
| 53 | + * {@link HttpResources} holder. |
| 54 | + * <p>Default is "true" in which case this factory helps to configure and |
| 55 | + * shut down the global Reactor Netty resources within the lifecycle of a |
| 56 | + * Spring {@code ApplicationContext}. |
| 57 | + * <p>If set to "false" then the factory creates and manages its own |
| 58 | + * {@link LoopResources} and {@link ConnectionProvider}, independent of the |
| 59 | + * global ones in the {@link HttpResources} holder. |
| 60 | + * @param globalResources whether to expose and manage the global resources |
| 61 | + */ |
| 62 | + public void setGlobalResources(boolean globalResources) { |
| 63 | + this.globalResources = globalResources; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Configure the {@link ConnectionProvider} to use. |
| 68 | + * <p>By default, initialized with {@link ConnectionProvider#elastic(String)}. |
| 69 | + * @param connectionProvider the connection provider to use |
| 70 | + */ |
| 71 | + public void setConnectionProvider(@Nullable ConnectionProvider connectionProvider) { |
| 72 | + this.connectionProvider = connectionProvider; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Configure the {@link LoopResources} to use. |
| 77 | + * <p>By default, initialized with {@link LoopResources#create(String)}. |
| 78 | + * @param loopResources the loop resources to use |
| 79 | + */ |
| 80 | + public void setLoopResources(@Nullable LoopResources loopResources) { |
| 81 | + this.loopResources = loopResources; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Configure the thread prefix to initialize {@link LoopResources} with. This |
| 86 | + * is used only when a {@link LoopResources} instance isn't |
| 87 | + * {@link #setLoopResources(LoopResources) provided}. |
| 88 | + * <p>By default set to "reactor-http". |
| 89 | + * @param threadPrefix the thread prefix to use |
| 90 | + */ |
| 91 | + public void setThreadPrefix(String threadPrefix) { |
| 92 | + Assert.notNull(threadPrefix, "Thread prefix is required"); |
| 93 | + this.threadPrefix = threadPrefix; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * Whether this factory exposes the global |
| 99 | + * {@link reactor.netty.http.HttpResources HttpResources} holder. |
| 100 | + */ |
| 101 | + public boolean isGlobalResources() { |
| 102 | + return this.globalResources; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Return the configured {@link ConnectionProvider}. |
| 107 | + */ |
| 108 | + @Nullable |
| 109 | + public ConnectionProvider getConnectionProvider() { |
| 110 | + return this.connectionProvider; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return the configured {@link LoopResources}. |
| 115 | + */ |
| 116 | + @Nullable |
| 117 | + public LoopResources getLoopResources() { |
| 118 | + return this.loopResources; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Return the configured prefix for event loop threads. |
| 123 | + */ |
| 124 | + public String getThreadPrefix() { |
| 125 | + return this.threadPrefix; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Override |
| 130 | + public void afterPropertiesSet() throws Exception { |
| 131 | + if (this.loopResources == null) { |
| 132 | + this.loopResources = LoopResources.create(this.threadPrefix); |
| 133 | + } |
| 134 | + if (this.connectionProvider == null) { |
| 135 | + this.connectionProvider = ConnectionProvider.elastic("http"); |
| 136 | + } |
| 137 | + if (this.globalResources) { |
| 138 | + HttpResources.set(this.loopResources); |
| 139 | + HttpResources.set(this.connectionProvider); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void destroy() { |
| 145 | + |
| 146 | + Assert.notNull(this.connectionProvider, "No ConnectionProvider"); |
| 147 | + this.connectionProvider.dispose(); |
| 148 | + |
| 149 | + Assert.notNull(this.loopResources, "No LoopResources"); |
| 150 | + this.loopResources.dispose(); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments