trautonen / freemarker-ext

Extensions for Freemarker template engine.
MIT License
3 stars 0 forks source link

Servlet processing #6

Open dzsessona opened 8 years ago

dzsessona commented 8 years ago

Hello, is it possible to use this project to add asyncronous processing to this servlet?

http://freemarker.org/docs/pgui_misc_servlet.html

Is there any example on how to do it?

trautonen commented 8 years ago

Do you just want support for asynchronous models or support for async processing introduced in Servlet 3.0?

Asynchronous models should be quite easy, you just need to extend the default servlet implementation like this:

public class CustomFreemarkerServlet extends FreemarkerServlet {

    private ExecutorService executor;

    @Override
    public void init() throws ServletException {
        executor = Executors.newFixedThreadPool(10);
        super.init();
    }

    @Override
    public void destroy() {
        super.destroy();
        executor.shutdownNow();
    }

    @Override
    protected ObjectWrapper createDefaultObjectWrapper() {
        return new CustomizableObjectWrapper(Configuration.getVersion())
                .registerTypedModelFactory(new FutureModelFactory())
                .registerTypedModelFactory(new CallableModelFactory(executor));
    }
}

And then use the correct servlet class in web.xml:

<servlet>
  <servlet-name>freemarker</servlet-name>
  <servlet-class>com.myorg.CustomFreemarkerServlet</servlet-class>

  ...
</servlet>

You get the idea from that.

For asynchronous servlet processing it's a lot trickier. You probably need to write quite a lot boilerplate code and use CompletableFuture to trigger the template processing when the models are calculated. For that I don't have out of the box example to give.

dzsessona commented 8 years ago

@trautonen sorry should have been a bit more specific. I meant async processing introduced in Servlet 3.0. I have it working on jsp pages (it is a scalatra app) . I will add the error I am getting here shortly and will notify you with updates if I manage to get it working. Meanwhile if you have any ideas, any suggestion is welcome :-)

Thanks again. Diego