|
| 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 | + |
| 17 | +package org.springframework.http.client.reactive; |
| 18 | + |
| 19 | + |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.util.concurrent.Executor; |
| 22 | + |
| 23 | +import org.eclipse.jetty.io.ByteBufferPool; |
| 24 | +import org.eclipse.jetty.io.MappedByteBufferPool; |
| 25 | +import org.eclipse.jetty.util.ProcessorUtils; |
| 26 | +import org.eclipse.jetty.util.component.ContainerLifeCycle; |
| 27 | +import org.eclipse.jetty.util.thread.QueuedThreadPool; |
| 28 | +import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; |
| 29 | +import org.eclipse.jetty.util.thread.Scheduler; |
| 30 | +import org.eclipse.jetty.util.thread.ThreadPool; |
| 31 | + |
| 32 | +import org.springframework.beans.factory.DisposableBean; |
| 33 | +import org.springframework.beans.factory.InitializingBean; |
| 34 | +import org.springframework.lang.Nullable; |
| 35 | +import org.springframework.util.Assert; |
| 36 | + |
| 37 | +/** |
| 38 | + * Factory to manage Jetty resources, i.e. {@link Executor}, {@link ByteBufferPool} and |
| 39 | + * {@link Scheduler}, within the lifecycle of a Spring {@code ApplicationContext}. |
| 40 | + * |
| 41 | + * <p>This factory implements {@link InitializingBean} and {@link DisposableBean} |
| 42 | + * and is expected typically to be declared as a Spring-managed bean. |
| 43 | + * |
| 44 | + * @author Sebastien Deleuze |
| 45 | + * @since 5.1 |
| 46 | + */ |
| 47 | +public class JettyResourceFactory implements InitializingBean, DisposableBean { |
| 48 | + |
| 49 | + @Nullable |
| 50 | + private Executor executor; |
| 51 | + |
| 52 | + @Nullable |
| 53 | + private ByteBufferPool byteBufferPool; |
| 54 | + |
| 55 | + @Nullable |
| 56 | + private Scheduler scheduler; |
| 57 | + |
| 58 | + private String threadPrefix = "jetty-http"; |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Configure the {@link Executor} to use. |
| 63 | + * <p>By default, initialized with a {@link QueuedThreadPool}. |
| 64 | + * @param executor the executor to use |
| 65 | + */ |
| 66 | + public void setExecutor(@Nullable Executor executor) { |
| 67 | + this.executor = executor; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Configure the {@link ByteBufferPool} to use. |
| 72 | + * <p>By default, initialized with a {@link MappedByteBufferPool}. |
| 73 | + * @param byteBufferPool the {@link ByteBuffer} pool to use |
| 74 | + */ |
| 75 | + public void setByteBufferPool(@Nullable ByteBufferPool byteBufferPool) { |
| 76 | + this.byteBufferPool = byteBufferPool; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Configure the {@link Scheduler} to use. |
| 81 | + * <p>By default, initialized with a {@link ScheduledExecutorScheduler}. |
| 82 | + * @param scheduler the {@link Scheduler} to use |
| 83 | + */ |
| 84 | + public void setScheduler(@Nullable Scheduler scheduler) { |
| 85 | + this.scheduler = scheduler; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Configure the thread prefix to initialize {@link QueuedThreadPool} executor with. This |
| 90 | + * is used only when a {@link Executor} instance isn't |
| 91 | + * {@link #setExecutor(Executor) provided}. |
| 92 | + * <p>By default set to "jetty-http". |
| 93 | + * @param threadPrefix the thread prefix to use |
| 94 | + */ |
| 95 | + public void setThreadPrefix(String threadPrefix) { |
| 96 | + Assert.notNull(threadPrefix, "Thread prefix is required"); |
| 97 | + this.threadPrefix = threadPrefix; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Return the configured {@link Executor}. |
| 102 | + */ |
| 103 | + @Nullable |
| 104 | + public Executor getExecutor() { |
| 105 | + return this.executor; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Return the configured {@link ByteBufferPool}. |
| 110 | + */ |
| 111 | + @Nullable |
| 112 | + public ByteBufferPool getByteBufferPool() { |
| 113 | + return this.byteBufferPool; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Return the configured {@link Scheduler}. |
| 118 | + */ |
| 119 | + @Nullable |
| 120 | + public Scheduler getScheduler() { |
| 121 | + return this.scheduler; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void afterPropertiesSet() throws Exception { |
| 126 | + String name = this.threadPrefix + "@" + Integer.toHexString(hashCode()); |
| 127 | + if (this.executor == null) { |
| 128 | + QueuedThreadPool threadPool = new QueuedThreadPool(); |
| 129 | + threadPool.setName(name); |
| 130 | + threadPool.start(); |
| 131 | + this.executor = threadPool; |
| 132 | + } |
| 133 | + if (this.byteBufferPool == null) { |
| 134 | + this.byteBufferPool = new MappedByteBufferPool(2048, |
| 135 | + this.executor instanceof ThreadPool.SizedThreadPool |
| 136 | + ? ((ThreadPool.SizedThreadPool) executor).getMaxThreads() / 2 |
| 137 | + : ProcessorUtils.availableProcessors() * 2); |
| 138 | + } |
| 139 | + if (this.scheduler == null) { |
| 140 | + Scheduler scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false); |
| 141 | + scheduler.start(); |
| 142 | + this.scheduler = scheduler; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void destroy() throws Exception { |
| 148 | + if (this.executor instanceof ContainerLifeCycle) { |
| 149 | + ((ContainerLifeCycle)this.executor).stop(); |
| 150 | + } |
| 151 | + if (this.scheduler != null) { |
| 152 | + this.scheduler.stop(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments