Search code examples
javajettycxfcxfrs

Configure jetty that is launched via CXF programmatically


I launch a jetty instance indirectly when creating a JAX-RS endpoint using cxf

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(HelloWorldResource.class);
    sf.setResourceProvider(HelloWorldResource.class, new SingletonResourceProvider(new HelloWorldResource()));
    sf.setAddress("http://localhost:9000/");
    sf.create();

This works just fine, but how can i configure the size of the jetty threadpool minThreads and maxThreads programmatically when launching it via CXF?


Solution

  • here you are:

        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    
            // you need to provide a default configuration
        JettyHTTPServerEngineFactory serverEngineFactory = sf.getBus().getExtension(JettyHTTPServerEngineFactory.class);
    
        JettyHTTPServerEngine eng = new JettyHTTPServerEngine();
        eng.setPort(0); // with the port zero
        ThreadingParameters defaultThreadingParams = new ThreadingParameters();
        defaultThreadingParams.setMinThreads(5);
        defaultThreadingParams.setMaxThreads(10);
        defaultThreadingParams.setThreadNamePrefix("myjetty");
        eng.setThreadingParameters(defaultThreadingParams);
        serverEngineFactory.setEnginesList(Arrays.asList(eng));
    
        sf.setResourceProvider(HelloWorldResource.class, new SingletonResourceProvider(new HelloWorldResource()));
        sf.setAddress("http://localhost:9000/");
        sf.create();
    

    According to my tests, at least 4 threads should be specified.

    Tested with CXF 3.3.3