|
| 1 | +/* |
| 2 | + * Copyright 2002-2014 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 | + |
| 17 | +package org.springframework.http.client; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URI; |
| 21 | + |
| 22 | +import io.netty.bootstrap.Bootstrap; |
| 23 | +import io.netty.channel.ChannelInitializer; |
| 24 | +import io.netty.channel.ChannelPipeline; |
| 25 | +import io.netty.channel.EventLoopGroup; |
| 26 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 27 | +import io.netty.channel.socket.SocketChannel; |
| 28 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 29 | +import io.netty.handler.codec.http.HttpClientCodec; |
| 30 | +import io.netty.handler.codec.http.HttpObjectAggregator; |
| 31 | +import io.netty.handler.ssl.SslContext; |
| 32 | + |
| 33 | +import org.springframework.beans.factory.DisposableBean; |
| 34 | +import org.springframework.beans.factory.InitializingBean; |
| 35 | +import org.springframework.http.HttpMethod; |
| 36 | +import org.springframework.util.Assert; |
| 37 | + |
| 38 | +/** |
| 39 | + * {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that |
| 40 | + * uses <a href="http://netty.io/">Netty 4</a> to create requests. |
| 41 | + * |
| 42 | + * <p>Allows to use a pre-configured {@link EventLoopGroup} instance - useful for sharing |
| 43 | + * across multiple clients. |
| 44 | + * |
| 45 | + * @author Arjen Poutsma |
| 46 | + * @since 4.2 |
| 47 | + */ |
| 48 | +public class Netty4ClientHttpRequestFactory |
| 49 | + implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, |
| 50 | + InitializingBean, DisposableBean { |
| 51 | + |
| 52 | + /** |
| 53 | + * The default maximum request size. |
| 54 | + * @see #setMaxRequestSize(int) |
| 55 | + */ |
| 56 | + public static final int DEFAULT_MAX_REQUEST_SIZE = 1024 * 1024 * 10; |
| 57 | + |
| 58 | + private final EventLoopGroup eventLoopGroup; |
| 59 | + |
| 60 | + private final boolean defaultEventLoopGroup; |
| 61 | + |
| 62 | + private SslContext sslContext; |
| 63 | + |
| 64 | + private int maxRequestSize = DEFAULT_MAX_REQUEST_SIZE; |
| 65 | + |
| 66 | + private Bootstrap bootstrap; |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a new {@code Netty4ClientHttpRequestFactory} with a default |
| 70 | + * {@link NioEventLoopGroup}. |
| 71 | + */ |
| 72 | + public Netty4ClientHttpRequestFactory() { |
| 73 | + int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2; |
| 74 | + eventLoopGroup = new NioEventLoopGroup(ioWorkerCount); |
| 75 | + defaultEventLoopGroup = true; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Creates a new {@code Netty4ClientHttpRequestFactory} with the given |
| 80 | + * {@link EventLoopGroup}. |
| 81 | + * |
| 82 | + * <p><b>NOTE:</b> the given group will <strong>not</strong> be |
| 83 | + * {@linkplain EventLoopGroup#shutdownGracefully() shutdown} by this factory; doing |
| 84 | + * so becomes the responsibility of the caller. |
| 85 | + */ |
| 86 | + public Netty4ClientHttpRequestFactory(EventLoopGroup eventLoopGroup) { |
| 87 | + Assert.notNull(eventLoopGroup, "'eventLoopGroup' must not be null"); |
| 88 | + this.eventLoopGroup = eventLoopGroup; |
| 89 | + this.defaultEventLoopGroup = false; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Sets the default maximum request size. The default is |
| 94 | + * {@link #DEFAULT_MAX_REQUEST_SIZE}. |
| 95 | + * @see HttpObjectAggregator#HttpObjectAggregator(int) |
| 96 | + */ |
| 97 | + public void setMaxRequestSize(int maxRequestSize) { |
| 98 | + this.maxRequestSize = maxRequestSize; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets the SSL context. |
| 103 | + */ |
| 104 | + public void setSslContext(SslContext sslContext) { |
| 105 | + this.sslContext = sslContext; |
| 106 | + } |
| 107 | + |
| 108 | + private Bootstrap getBootstrap() { |
| 109 | + if (this.bootstrap == null) { |
| 110 | + Bootstrap bootstrap = new Bootstrap(); |
| 111 | + bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) |
| 112 | + .handler(new ChannelInitializer<SocketChannel>() { |
| 113 | + @Override |
| 114 | + protected void initChannel(SocketChannel ch) throws Exception { |
| 115 | + ChannelPipeline pipeline = ch.pipeline(); |
| 116 | + |
| 117 | + if (sslContext != null) { |
| 118 | + pipeline.addLast(sslContext.newHandler(ch.alloc())); |
| 119 | + } |
| 120 | + pipeline.addLast(new HttpClientCodec()); |
| 121 | + pipeline.addLast(new HttpObjectAggregator(maxRequestSize)); |
| 122 | + } |
| 123 | + }); |
| 124 | + this.bootstrap = bootstrap; |
| 125 | + } |
| 126 | + return this.bootstrap; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void afterPropertiesSet() throws Exception { |
| 131 | + getBootstrap(); |
| 132 | + } |
| 133 | + |
| 134 | + private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) { |
| 135 | + return new Netty4ClientHttpRequest(getBootstrap(), uri, httpMethod, maxRequestSize); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) |
| 140 | + throws IOException { |
| 141 | + return createRequestInternal(uri, httpMethod); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) |
| 146 | + throws IOException { |
| 147 | + return createRequestInternal(uri, httpMethod); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void destroy() throws InterruptedException { |
| 152 | + if (defaultEventLoopGroup) { |
| 153 | + // clean up the EventLoopGroup if we created it in the constructor |
| 154 | + eventLoopGroup.shutdownGracefully().sync(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +} |
0 commit comments