Search in sources :

Example 96 with QueuedThreadPool

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);
    }
}
Also used : ServletConfig(javax.servlet.ServletConfig) HttpCookieStore(org.eclipse.jetty.util.HttpCookieStore) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) UnknownHostException(java.net.UnknownHostException) UnavailableException(javax.servlet.UnavailableException) ServletException(javax.servlet.ServletException) Executor(java.util.concurrent.Executor) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) ProtocolHandlers(org.eclipse.jetty.client.ProtocolHandlers)

Example 97 with QueuedThreadPool

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();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 98 with QueuedThreadPool

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);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 99 with QueuedThreadPool

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();
}
Also used : HttpProxy(org.eclipse.jetty.client.HttpProxy) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient)

Example 100 with QueuedThreadPool

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());
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Test(org.junit.Test)

Aggregations

QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)126 Server (org.eclipse.jetty.server.Server)65 ServerConnector (org.eclipse.jetty.server.ServerConnector)51 Test (org.junit.Test)30 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)26 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)23 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)22 HttpClient (org.eclipse.jetty.client.HttpClient)18 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)18 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)18 IOException (java.io.IOException)17 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)11 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)11 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)10 File (java.io.File)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 InputStream (java.io.InputStream)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)8