use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.
the class AbstractProxyServlet method createHttpClient.
/**
* <p>Creates a {@link HttpClient} instance, configured with init parameters of this servlet.</p>
* <p>The init parameters used to configure the {@link HttpClient} instance are:</p>
* <table>
* <caption>Init Parameters</caption>
* <thead>
* <tr>
* <th>init-param</th>
* <th>default</th>
* <th>description</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <td>maxThreads</td>
* <td>256</td>
* <td>The max number of threads of HttpClient's Executor. If not set, or set to the value of "-", then the
* Jetty server thread pool will be used.</td>
* </tr>
* <tr>
* <td>maxConnections</td>
* <td>32768</td>
* <td>The max number of connections per destination, see {@link HttpClient#setMaxConnectionsPerDestination(int)}</td>
* </tr>
* <tr>
* <td>idleTimeout</td>
* <td>30000</td>
* <td>The idle timeout in milliseconds, see {@link HttpClient#setIdleTimeout(long)}</td>
* </tr>
* <tr>
* <td>timeout</td>
* <td>60000</td>
* <td>The total timeout in milliseconds, see {@link Request#timeout(long, java.util.concurrent.TimeUnit)}</td>
* </tr>
* <tr>
* <td>requestBufferSize</td>
* <td>HttpClient's default</td>
* <td>The request buffer size, see {@link HttpClient#setRequestBufferSize(int)}</td>
* </tr>
* <tr>
* <td>responseBufferSize</td>
* <td>HttpClient's default</td>
* <td>The response buffer size, see {@link HttpClient#setResponseBufferSize(int)}</td>
* </tr>
* </tbody>
* </table>
*
* @return a {@link HttpClient} configured from the {@link #getServletConfig() servlet configuration}
* @throws ServletException if the {@link HttpClient} cannot be created
*/
protected HttpClient createHttpClient() throws ServletException {
ServletConfig config = getServletConfig();
HttpClient client = newHttpClient();
// Redirects must be proxied as is, not followed.
client.setFollowRedirects(false);
// Must not store cookies, otherwise cookies of different clients will mix.
client.setCookieStore(new HttpCookieStore.Empty());
Executor executor;
String value = config.getInitParameter("maxThreads");
if (value == null || "-".equals(value)) {
executor = (Executor) getServletContext().getAttribute("org.eclipse.jetty.server.Executor");
if (executor == null)
throw new IllegalStateException("No server executor for proxy");
} else {
QueuedThreadPool qtp = new QueuedThreadPool(Integer.parseInt(value));
String servletName = config.getServletName();
int dot = servletName.lastIndexOf('.');
if (dot >= 0)
servletName = servletName.substring(dot + 1);
qtp.setName(servletName);
executor = qtp;
}
client.setExecutor(executor);
value = config.getInitParameter("maxConnections");
if (value == null)
value = "256";
client.setMaxConnectionsPerDestination(Integer.parseInt(value));
value = config.getInitParameter("idleTimeout");
if (value == null)
value = "30000";
client.setIdleTimeout(Long.parseLong(value));
value = config.getInitParameter("timeout");
if (value == null)
value = "60000";
_timeout = Long.parseLong(value);
value = config.getInitParameter("requestBufferSize");
if (value != null)
client.setRequestBufferSize(Integer.parseInt(value));
value = config.getInitParameter("responseBufferSize");
if (value != null)
client.setResponseBufferSize(Integer.parseInt(value));
try {
client.start();
// Content must not be decoded, otherwise the client gets confused.
client.getContentDecoderFactories().clear();
// Pass traffic to the client, only intercept what's necessary.
ProtocolHandlers protocolHandlers = client.getProtocolHandlers();
protocolHandlers.clear();
protocolHandlers.put(new ProxyContinueProtocolHandler());
return client;
} catch (Exception x) {
throw new ServletException(x);
}
}
use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method startServer.
private void startServer(HttpServlet servlet) throws Exception {
QueuedThreadPool serverPool = new QueuedThreadPool();
serverPool.setName("server");
server = new Server(serverPool);
serverConnector = new ServerConnector(server);
server.addConnector(serverConnector);
ServletContextHandler appCtx = new ServletContextHandler(server, "/", true, false);
ServletHolder appServletHolder = new ServletHolder(servlet);
appCtx.addServlet(appServletHolder, "/*");
server.start();
}
use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method startProxy.
private void startProxy(AsyncMiddleManServlet proxyServlet, Map<String, String> initParams) throws Exception {
QueuedThreadPool proxyPool = new QueuedThreadPool();
proxyPool.setName("proxy");
proxy = new Server(proxyPool);
HttpConfiguration configuration = new HttpConfiguration();
configuration.setSendDateHeader(false);
configuration.setSendServerVersion(false);
String value = initParams.get("outputBufferSize");
if (value != null)
configuration.setOutputBufferSize(Integer.valueOf(value));
proxyConnector = new ServerConnector(proxy, new HttpConnectionFactory(configuration));
proxy.addConnector(proxyConnector);
ServletContextHandler proxyContext = new ServletContextHandler(proxy, "/", true, false);
ServletHolder proxyServletHolder = new ServletHolder(proxyServlet);
proxyServletHolder.setInitParameters(initParams);
proxyContext.addServlet(proxyServletHolder, "/*");
proxy.start();
stackless = new StacklessLogging(proxyServlet._log);
}
use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method startClient.
private void startClient() throws Exception {
QueuedThreadPool clientPool = new QueuedThreadPool();
clientPool.setName("client");
client = new HttpClient();
client.setExecutor(clientPool);
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyConnector.getLocalPort()));
client.start();
}
use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.
the class AsyncListenerTest method test_StartAsync_OnTimeout_CalledBy_PooledThread.
@Test
public void test_StartAsync_OnTimeout_CalledBy_PooledThread() throws Exception {
String threadNamePrefix = "async_listener";
threadPool = new QueuedThreadPool();
threadPool.setName(threadNamePrefix);
ServletContextHandler context = new ServletContextHandler();
context.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(1000);
asyncContext.addListener(new AsyncListenerAdapter() {
@Override
public void onTimeout(AsyncEvent event) throws IOException {
if (Thread.currentThread().getName().startsWith(threadNamePrefix))
response.setStatus(HttpStatus.OK_200);
else
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
asyncContext.complete();
}
});
}
}), "/*");
startServer(context);
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n"));
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
Aggregations