comparison src/org/eclipse/jetty/server/Server.java @ 876:2efdb98f3543

use just one fixed Connector in Server
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 04 Oct 2016 14:05:45 -0600
parents b9aa175d9a29
children fef4392f4905
comparison
equal deleted inserted replaced
875:b9aa175d9a29 876:2efdb98f3543
18 18
19 package org.eclipse.jetty.server; 19 package org.eclipse.jetty.server;
20 20
21 import java.io.IOException; 21 import java.io.IOException;
22 import java.net.InetSocketAddress; 22 import java.net.InetSocketAddress;
23 import java.util.Collections;
23 import java.util.Enumeration; 24 import java.util.Enumeration;
24 import java.util.concurrent.LinkedBlockingQueue; 25 import java.util.concurrent.LinkedBlockingQueue;
25 import java.util.concurrent.ThreadPoolExecutor; 26 import java.util.concurrent.ThreadPoolExecutor;
26 import java.util.concurrent.TimeUnit; 27 import java.util.concurrent.TimeUnit;
27 28
60 61
61 private static final String __version = "8"; 62 private static final String __version = "8";
62 63
63 private final AttributesMap _attributes = new AttributesMap(); 64 private final AttributesMap _attributes = new AttributesMap();
64 public final ThreadPoolExecutor threadPool; 65 public final ThreadPoolExecutor threadPool;
65 private Connector[] _connectors; 66 public final Connector connector;
66 private boolean _sendServerVersion = true; //send Server: header 67 private boolean _sendServerVersion = true; //send Server: header
67 private boolean _sendDateHeader = false; //send Date: header 68 private boolean _sendDateHeader = false; //send Date: header
68 private int _graceful=0; 69 private int _graceful=0;
69 private boolean _stopAtShutdown; 70 private boolean _stopAtShutdown;
70 private boolean _dumpAfterStart=false; 71 private boolean _dumpAfterStart=false;
78 */ 79 */
79 public Server(int port) 80 public Server(int port)
80 { 81 {
81 setServer(this); 82 setServer(this);
82 83
83 Connector connector=new SelectChannelConnector(); 84 connector = new SelectChannelConnector();
84 connector.setPort(port); 85 connector.setPort(port);
85 setConnectors(new Connector[]{connector}); 86 connector.setServer(this);
86 87
87 threadPool = new ThreadPoolExecutor(256, 256, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); 88 threadPool = new ThreadPoolExecutor(256, 256, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
88 } 89 }
89 90
90 91
118 ShutdownThread.deregister(this); 119 ShutdownThread.deregister(this);
119 120
120 _stopAtShutdown=stop; 121 _stopAtShutdown=stop;
121 } 122 }
122 123
123 /* ------------------------------------------------------------ */
124 /**
125 * @return Returns the connectors.
126 */
127 public Connector[] getConnectors()
128 {
129 return _connectors;
130 }
131
132 /* ------------------------------------------------------------ */
133 public void addConnector(Connector connector)
134 {
135 setConnectors((Connector[])LazyList.addToArray(getConnectors(), connector, Connector.class));
136 }
137
138 /* ------------------------------------------------------------ */
139 /**
140 * Conveniance method which calls {@link #getConnectors()} and {@link #setConnectors(Connector[])} to
141 * remove a connector.
142 * @param connector The connector to remove.
143 */
144 public void removeConnector(Connector connector) {
145 setConnectors((Connector[])LazyList.removeFromArray (getConnectors(), connector));
146 }
147
148 /* ------------------------------------------------------------ */
149 /** Set the connectors for this server.
150 * Each connector has this server set as it's ThreadPool and its Handler.
151 * @param connectors The connectors to set.
152 */
153 public void setConnectors(Connector[] connectors)
154 {
155 if (connectors!=null)
156 {
157 for (int i=0;i<connectors.length;i++)
158 connectors[i].setServer(this);
159 }
160
161 _connectors = connectors;
162 }
163
164 /** 124 /**
165 * @return true if {@link #dumpStdErr()} is called after starting 125 * @return true if {@link #dumpStdErr()} is called after starting
166 */ 126 */
167 public boolean isDumpAfterStart() 127 public boolean isDumpAfterStart()
168 { 128 {
218 catch(Throwable e) 178 catch(Throwable e)
219 { 179 {
220 mex.add(e); 180 mex.add(e);
221 } 181 }
222 182
223 if (_connectors!=null && mex.size()==0) 183 if (mex.size()==0)
224 { 184 {
225 for (int i=0;i<_connectors.length;i++) 185 try{connector.start();}
186 catch(Throwable e)
226 { 187 {
227 try{_connectors[i].start();} 188 mex.add(e);
228 catch(Throwable e)
229 {
230 mex.add(e);
231 }
232 } 189 }
233 } 190 }
234 191
235 if (isDumpAfterStart()) 192 if (isDumpAfterStart())
236 dumpStdErr(); 193 dumpStdErr();
247 204
248 MultiException mex=new MultiException(); 205 MultiException mex=new MultiException();
249 206
250 if (_graceful>0) 207 if (_graceful>0)
251 { 208 {
252 if (_connectors!=null) 209 LOG.info("Graceful shutdown {}",connector);
253 { 210 try{connector.close();}catch(Throwable e){mex.add(e);}
254 for (int i=_connectors.length;i-->0;)
255 {
256 LOG.info("Graceful shutdown {}",_connectors[i]);
257 try{_connectors[i].close();}catch(Throwable e){mex.add(e);}
258 }
259 }
260 211
261 Handler[] contexts = getChildHandlersByClass(Graceful.class); 212 Handler[] contexts = getChildHandlersByClass(Graceful.class);
262 for (int c=0;c<contexts.length;c++) 213 for (int c=0;c<contexts.length;c++)
263 { 214 {
264 Graceful context=(Graceful)contexts[c]; 215 Graceful context=(Graceful)contexts[c];
266 context.setShutdown(true); 217 context.setShutdown(true);
267 } 218 }
268 Thread.sleep(_graceful); 219 Thread.sleep(_graceful);
269 } 220 }
270 221
271 if (_connectors!=null) 222 try{connector.stop();}catch(Throwable e){mex.add(e);}
272 {
273 for (int i=_connectors.length;i-->0;)
274 try{_connectors[i].stop();}catch(Throwable e){mex.add(e);}
275 }
276 223
277 threadPool.shutdownNow(); 224 threadPool.shutdownNow();
278 225
279 try {super.doStop(); } catch(Throwable e) { mex.add(e);} 226 try {super.doStop(); } catch(Throwable e) { mex.add(e);}
280 227
479 /* ------------------------------------------------------------ */ 426 /* ------------------------------------------------------------ */
480 @Override 427 @Override
481 public void dump(Appendable out,String indent) throws IOException 428 public void dump(Appendable out,String indent) throws IOException
482 { 429 {
483 dumpThis(out); 430 dumpThis(out);
484 dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(),TypeUtil.asList(_connectors)); 431 dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(),Collections.singletonList(connector));
485 } 432 }
486 433
487 434
488 /* ------------------------------------------------------------ */ 435 /* ------------------------------------------------------------ */
489 public boolean isUncheckedPrintWriter() 436 public boolean isUncheckedPrintWriter()