[undertow-dev] IO and Worker Thread based on custom preferences

Stuart Douglas sdouglas at redhat.com
Mon Jan 2 17:52:59 EST 2017


The easiest way to do this is to use a ServletExtension. Create the
extension class, add it to your deployment, and then create a
META-INF/services/io.undertow.servlet.ServletExtension entry for it.
It will be invoked on deployment. Something like the following class
should do what you are after:



private class MyExtension implements ServletExtension {

    @Override
    public void handleDeployment(DeploymentInfo deploymentInfo,
ServletContext servletContext) {
        final ExecutorService executor =
Executors.newFixedThreadPool(1); //create the executor
        deploymentInfo.addInitialHandlerChainWrapper(new
HandlerWrapper() {         //handlers that will run before any servlet
functionality
            @Override
            public HttpHandler wrap(final HttpHandler handler) {
                return new PredicateHandler(Predicates.path("/myurl"),
new HttpHandler() {  //only run this if the predicate matches (in this
case /myurl)
                    @Override
                    public void handleRequest(HttpServerExchange
exchange) throws Exception {
                        exchange.setDispatchExecutor(executor);
                        handler.handleRequest(exchange);
                    }
                }, handler);
            }
        });
        //add a listener to shut down the pool on undeploy
        servletContext.addListener(new ServletContextListener() {
            @Override
            public void contextInitialized(ServletContextEvent sce) {

            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                executor.shutdown();
            }
        });
    }
}

On Wed, Dec 28, 2016 at 7:46 PM, tone randomnaja <randomnaja at gmail.com> wrote:
> Hi Stuart,
>
> Request limit might be insufficient, could you share how to implement the
> custom handler and possibly how to deploy this custom handler to Wildfly ?
>
> Regards,
>
> On Dec 23, 2016 04:36, "Stuart Douglas" <sdouglas at redhat.com> wrote:
>>
>> You would need to write a custom handler to set the thread pool if you
>> want to do this in Wildfly, however if you just want to limit the number of
>> active requests in a given path you may want to just use the request
>> limiting handler instead.
>>
>> In WEB-INF/undertow-handlers.conf try adding something like the following:
>>
>> path-prefix(/mypath) -> request-limit(10)
>>
>> This will limit the number of requests that can be active in /mypath/* to
>> 10.
>>
>> If you actually need to use a thread pool and the request limit is not
>> sufficient let me know and I can point you in the right direction.
>>
>> Stuart
>>
>>
>> On Fri, Dec 23, 2016 at 12:07 AM, Bill O'Neil <bill at dartalley.com> wrote:
>>>
>>> I only use undertow directly I haven't looked at Wildfly before so I
>>> won't be able to answer your question sorry.
>>>
>>> On Thu, Dec 22, 2016 at 7:55 AM, tone randomnaja <randomnaja at gmail.com>
>>> wrote:
>>>>
>>>> Thanks,
>>>>
>>>> And let's say if I had custom executor, how could I set the
>>>> DispatchExecutor ? (from Wildfly point-of-view)
>>>> Could it be another module being placed inside system lib of Wildfly ?
>>>>
>>>> ps. I'm pretty sure, I'd not have done this within Application (EAR/WAR)
>>>> itself ?
>>>>
>>>> On Thu, Dec 22, 2016 at 6:43 PM, Bill O'Neil <bill at dartalley.com> wrote:
>>>>>
>>>>> This might be what you are looking for.
>>>>>
>>>>>
>>>>> https://github.com/undertow-io/undertow/blob/master/core/src/main/java/io/undertow/server/HttpServerExchange.java#L821
>>>>>
>>>>> You can change the dispatch executor to your own custom executor before
>>>>> you call dispatch. This will allow you to have different worker pools and
>>>>> configure them per HttpHandler.
>>>>>
>>>>> On Thu, Dec 22, 2016 at 6:16 AM, tone randomnaja <randomnaja at gmail.com>
>>>>> wrote:
>>>>>>
>>>>>> Hi there !
>>>>>>
>>>>>> Undertow has `IO Thread` and `Worker Thread` configuration (<subsystem
>>>>>> xmlns="urn:jboss:domain:io:1.1"><worker name="default".....) which could be
>>>>>> bounded per Listener (<http-listener....).
>>>>>>
>>>>>> In my case I have 1 Listenner (AJP) and 1 Application (EAR),
>>>>>> I'd like to be able to priority and manage Worker Thread base on some
>>>>>> preferences, such as URL path.
>>>>>>
>>>>>> Above for a reason of controlling the load of specific URL (under the
>>>>>> same Web Context).
>>>>>>
>>>>>> Any suggestions or ideas ?
>>>>>>
>>>>>> _______________________________________________
>>>>>> undertow-dev mailing list
>>>>>> undertow-dev at lists.jboss.org
>>>>>> https://lists.jboss.org/mailman/listinfo/undertow-dev
>>>>>
>>>>>
>>>>
>>>
>>>
>>> _______________________________________________
>>> undertow-dev mailing list
>>> undertow-dev at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/undertow-dev
>>
>>
>


More information about the undertow-dev mailing list