Search in sources :

Example 26 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.

the class MBeanContainerLifeCycleTest method testAddBeanRegistersMBeanRemoveBeanUnregistersMBean.

@Test
public void testAddBeanRegistersMBeanRemoveBeanUnregistersMBean() throws Exception {
    // Adding a bean to the container should register the MBean.
    QueuedThreadPool bean = new QueuedThreadPool();
    container.addBean(bean);
    String pkg = bean.getClass().getPackage().getName();
    Set<ObjectName> objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
    Assert.assertEquals(1, objectNames.size());
    // Removing the bean should unregister the MBean.
    container.removeBean(bean);
    objectNames = mbeanServer.queryNames(ObjectName.getInstance(pkg + ":*"), null);
    Assert.assertEquals(0, objectNames.size());
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 27 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.

the class ObjectMBeanTest method testThreadPool.

@Test
@Ignore("ignore, used in testing jconsole atm")
public void testThreadPool() throws Exception {
    Derived derived = new Derived();
    ObjectMBean mbean = (ObjectMBean) ObjectMBean.mbeanFor(derived);
    ObjectMBean managed = (ObjectMBean) ObjectMBean.mbeanFor(derived.getManagedInstance());
    mbean.setMBeanContainer(container);
    managed.setMBeanContainer(container);
    QueuedThreadPool qtp = new QueuedThreadPool();
    ObjectMBean bqtp = (ObjectMBean) ObjectMBean.mbeanFor(qtp);
    bqtp.getMBeanInfo();
    container.beanAdded(null, derived);
    container.beanAdded(null, derived.getManagedInstance());
    container.beanAdded(null, mbean);
    container.beanAdded(null, managed);
    container.beanAdded(null, qtp);
    Thread.sleep(10000000);
}
Also used : Derived(com.acme.Derived) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.

the class HttpClient method doStart.

@Override
protected void doStart() throws Exception {
    if (sslContextFactory != null)
        addBean(sslContextFactory);
    String name = HttpClient.class.getSimpleName() + "@" + hashCode();
    if (executor == null) {
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setName(name);
        executor = threadPool;
    }
    addBean(executor);
    if (byteBufferPool == null)
        byteBufferPool = new MappedByteBufferPool();
    addBean(byteBufferPool);
    if (scheduler == null)
        scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false);
    addBean(scheduler);
    transport.setHttpClient(this);
    addBean(transport);
    if (resolver == null)
        resolver = new SocketAddressResolver.Async(executor, scheduler, getAddressResolutionTimeout());
    addBean(resolver);
    handlers.put(new ContinueProtocolHandler());
    handlers.put(new RedirectProtocolHandler(this));
    handlers.put(new WWWAuthenticationProtocolHandler(this));
    handlers.put(new ProxyAuthenticationProtocolHandler(this));
    decoderFactories.add(new GZIPContentDecoder.Factory(byteBufferPool));
    cookieManager = newCookieManager();
    cookieStore = cookieManager.getCookieStore();
    super.doStart();
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ScheduledExecutorScheduler(org.eclipse.jetty.util.thread.ScheduledExecutorScheduler)

Example 29 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.

the class JettyHttpServerProvider method createHttpServer.

@Override
public HttpServer createHttpServer(InetSocketAddress addr, int backlog) throws IOException {
    Server server = _server;
    boolean shared = true;
    if (server == null) {
        ThreadPool threadPool = new DelegatingThreadPool(new QueuedThreadPool());
        server = new Server(threadPool);
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { new ContextHandlerCollection(), new DefaultHandler() });
        server.setHandler(handlerCollection);
        shared = false;
    }
    JettyHttpServer jettyHttpServer = new JettyHttpServer(server, shared);
    jettyHttpServer.bind(addr, backlog);
    return jettyHttpServer;
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) HttpsServer(com.sun.net.httpserver.HttpsServer) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 30 with QueuedThreadPool

use of org.eclipse.jetty.util.thread.QueuedThreadPool in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testClientStopsServerDoesNotCloseClientCloses.

@Test
public void testClientStopsServerDoesNotCloseClientCloses() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        List<Session> sessions = new ArrayList<>();
        HTTP2Client h2Client = new HTTP2Client();
        HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client) {

            @Override
            protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
                sessions.add(session);
                return super.newHttpConnection(destination, session);
            }
        }, null);
        QueuedThreadPool clientExecutor = new QueuedThreadPool();
        clientExecutor.setName("client");
        client.setExecutor(clientExecutor);
        client.start();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.OK_200)
                resultLatch.countDown();
        });
        ByteBufferPool byteBufferPool = new MappedByteBufferPool();
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        Generator generator = new Generator(byteBufferPool);
        try (Socket socket = server.accept()) {
            socket.setSoTimeout(1000);
            OutputStream output = socket.getOutputStream();
            InputStream input = socket.getInputStream();
            ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() {

                @Override
                public void onHeaders(HeadersFrame request) {
                    // Server's preface.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
                    // Reply to client's SETTINGS.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), true));
                    // Response.
                    MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true);
                    generator.control(lease, response);
                    try {
                        // Write the frames.
                        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
                    } catch (Throwable x) {
                        x.printStackTrace();
                    }
                }
            }, 4096, 8192);
            byte[] bytes = new byte[1024];
            while (true) {
                try {
                    int read = input.read(bytes);
                    if (read < 0)
                        Assert.fail();
                    parser.parse(ByteBuffer.wrap(bytes, 0, read));
                } catch (SocketTimeoutException x) {
                    break;
                }
            }
            Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
            // The client will send a GO_AWAY, but the server will not close.
            client.stop();
            // Give some time to process the stop/close operations.
            Thread.sleep(1000);
            Assert.assertTrue(h2Client.getBeans(Session.class).isEmpty());
            for (Session session : sessions) {
                Assert.assertTrue(session.isClosed());
                Assert.assertTrue(((HTTP2Session) session).isDisconnected());
            }
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SocketTimeoutException(java.net.SocketTimeoutException) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) 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