Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching background threads on GWT startup

I have a GWT application that displays some charts rendered by JFreeChart. Every few minutes, the page refreshes, which causes the app to generate new charts. (In other words, the entire chart generation process is bootstrapped by a client request.) The problem with this is that multiple clients hitting the same server would result in multiple requests to generate charts, but since the charts are the same for all users, there's really no reason to do this. I would like to prerender the charts in a background thread, which would be kicked off when the application starts, and then just serve the already-rendered charts to the client on request.

I don't see any "sanctioned" way in GWT to execute your own code at server startup. The only way I can think of to accomplish this is to create a servlet that gets loaded at startup by the application container, and kick off the chart generation thread in the init() method.

Is there a more preferred way to do this?

Note: Assuming that it's true, "no" is a perfectly acceptable answer.

like image 846
Robert J. Walker Avatar asked Dec 22 '22 12:12

Robert J. Walker


1 Answers

To answer your question: No. GWT is a front end technology, and the only bit of GWT that crosses this line is the RPC mechanism. The only 'GWT' type way that you could do it would be to check if the chart files exist the first time a user requests them, and generate them if they don't. This would mean using the file system as your check of if it's been created yet or not.

The better way would be to do what you said, eg: configure your your web project to kick off a class on startup. You do this in your web.xml as described here:

http://wiki.metawerx.net/wiki/Web.xml.LoadOnStartup

Here's an example of how Stripes does it:

<servlet>
        <servlet-name>StripesDispatcher</servlet-name>
        <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>StripesDispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
like image 131
rustyshelf Avatar answered Jan 02 '23 19:01

rustyshelf