Skip to content

[Blog] Configuring Jetty for handling heavy server load

Pradeeban Kathiravelu edited this page Aug 24, 2018 · 1 revision

Jetty can be configured to handle the server load more efficiently by changing the default ThreadPool queue size as shown below:

Edit the following file https://bitbucket.org/BMI/bindaas/src/9fc8b289987da11581b8f76f837556478ca53956/source/projects/core/bindaas-commons-cxf-wrapper/src/edu/emory/cci/bindaas/commons/cxf/ResourceScanner.java?at=master#cl-96

package edu.emory.cci.bindaas.commons.cxf;
.....
.....
public class ResourceScanner implements ServiceListener , EventHandler {
 
    private synchronized void  addResource(ServiceReference<?> srf)
    {
        Object serviceObj = bundleContext.getService(srf);
        String name = (String) srf.getProperty(JAX_RS_SERVICE_NAME);
        if(serviceObj!=null && name!=null && !resourceContexts.containsKey(name))
        {
            try {
                 
                String publishAddress = (String) srf.getProperty(JAX_RS_SERVICE_ADDRESS);
                 
                Long serviceId =  (Long) srf.getProperty("service.id");
                @SuppressWarnings("unchecked")
                List<RequestHandler> listOfRequestHandlers = (List<RequestHandler>) srf.getProperty(JAX_RS_PROVIDER);
                @SuppressWarnings("unchecked")
                List<Interceptor<? extends Message>> listOfPhaseInInterceptors = (List<Interceptor<? extends Message>>) srf.getProperty(JAX_RS_IN_INTERCEPTOR);
                @SuppressWarnings("unchecked")
                List<Interceptor<? extends Message>> listOfPhaseOutInterceptors = (List<Interceptor<? extends Message>>) srf.getProperty(JAX_RS_OUT_INTERCEPTOR);
                 
                JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
                sf.setAddress(publishAddress);
                sf.setServiceBeanObjects(serviceObj);
                 
                if(listOfPhaseInInterceptors!=null)
                    sf.setInInterceptors(listOfPhaseInInterceptors);
                 
                if(listOfPhaseOutInterceptors!=null)
                    sf.setOutInterceptors(listOfPhaseOutInterceptors);
                 
                if(listOfRequestHandlers!=null)
                    sf.setProviders(listOfRequestHandlers);
                 
                Server server = sf.create();
                 
                /** New Code Begin **/
                org.eclipse.jetty.server.Server jettyServer = org.eclipse.jetty.server.Server.class.cast(server);
                jettyServer.getThreadPool.setMinThreads(MIN_THREADS);
                jettyServer.getThreadPool.setMaxThreads(MAX_THREADS);
                /** New Code Ends **/
                ResourceContext resourceContext = new ResourceContext();
                resourceContext.setBundleId(srf.getBundle().getBundleId());
                resourceContext.setServer(server);
                resourceContext.setEndpointUrl(publishAddress);
                resourceContext.setServerStarted(new Date());
                resourceContext.setServiceId(serviceId);
                resourceContext.setName(name);
                resourceContexts.put(name, resourceContext);
                log.info(String.format("Started service [%s] at [%s]", name , publishAddress));
            }
            catch(Exception e)
            {
                log.error("Failed to start the endpoint" , e);
            }
        }
        else
        {
            log.warn("Service not available from Reference. Cannot create ResourceContext");
        }
    }
    

MIN_THREADS and MAX_THREADS should be chosen appropriately. For more details read http://wiki.eclipse.org/Jetty/Howto/High_Load#Load_Generation_for_Load_Testing

Clone this wiki locally