Starting background threads when starting GWT

I have a GWT application that displays some charts created by JFreeChart. Every few minutes, the page is updated, which forces the application to create new charts. (In other words, the whole process of creating a diagram is loaded by a client request.) The problem is that several clients that arrive on the same server will lead to several requests for creating diagrams, but since the diagrams are the same for all users, in fact there reasons for this. I would like to fix the diagrams in the background thread, which will be launched when the application starts, and then just serve the already processed diagrams to the client upon request.

I do not see the โ€œauthorizedโ€ way in GWT to execute your own code at server startup. The only way I can do this is to create a servlet loaded at launch by the application container and start the flow of generating the diagram in the init () method.

Is there a more preferable way to do this?

Note. Assuming this is true, no is an acceptable answer.

+4
source share
4 answers

To answer your question: None. GWT is the front end technology, and the only GWT bit that crosses this line is the RPC mechanism. The only โ€œGWTโ€ way you could do this is to check if the chart files exist the first time the user requests them, and generate them if they donโ€™t. This would mean using the file system as a check whether it was created or not.

The best way is to do what you said, for example: configure your web project to run the class at startup. You do this in your web.xml, as described here:

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

Here is an example of how Stripes does this:

<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> 
+6
source

I do not agree that you should put the code in the initialization of the servlet to create these threads. When the web application is stopped, you cannot control when you need to destroy these threads. Also, when you start the web application again, will it create these threads again or not?

What would be better if you would use the J2EE ServletContextListener event named contextInitialized to create threads and the contextDestroyed event to destroy your threads. It also allows you to control the destruction of threads and wait for the code in the threads to complete before destroying them.

This seems to explain how it works: http://www.java-tips.org/java-ee-tips/java-servlet/how-to-work-with-servletcontextlistener.html

+3
source

First of all, I do not agree with the previous poster that GWT is only an interface technology, since everything is used asynchronously in the infrastructure to create and call services. See For example, RemoteServiceServlet and usages. GWT provides client and server components.

Having said that, GWT does not seem to have servlet-like servlets. But, since the back end is based on servlets, I would be inclined, for the sake of consistency, to use the startup servlet (search for Loading servlets at startup on google), and generate diagrams at startup, and then periodically as needed.

An alternative would be to have a cron job that periodically rebuilds charts either directly or by calling a private service.

In either case, the client requests then simply retrieve the pre-generated chart. When a new schedule is created, simply replace it with the old one so that there is no noticeable lag from the user.

+2
source

GWT is an interface infrastructure. I have a specific server component, but they are only used to serialize / deserialize client requests. Remember that the front end is in JavaScript and the back end is in java. Thus, the server components convert the JavaScript object into this java representation. If you look at your code, you may notice that the GWT RemoteServiceServlet simply inherits the standards "javax.servlet.http.HttpServlet", like any servlet. It just implements its own doPost and doGet method for serialization.

Having said that, your question can be applied to many other frameworks. It is not dependent on GWT. You just need to configure the servlet container to start the GWT RemoteServiceServlet at startup. A servlet can have a static cache that is used by all clients and passes the same schedule. You can then start the second thread at startup, which periodically updates the thread.

0
source

All Articles