From time to time Wicket users ask questions related to how to deal with background jobs in Wicket. E.g. “How do I make Application or Session available
to a background thread?” Or, “How do I deal with showing some progress information, or allow the user to cancel a background process?”. We have build a small toy
project to illustrate a possible way to do those things. We hope this project could help people get started on rolling their own solutions.
We start by defining an interface that represents a Task. i.e. some lengthy computation to be done in a background non-WEB thread.
the method doIt() receives a context/bridge class that can be used to communicate with the WEB layer.
As an example we provide a dummy task, that looks like.
This task does nothing but iterate from 1 to 100 and repeatedly call a service, IAnswerService, to get some text messages to pass to the WEB layer via the ExecutionBridge instance.
This class also uses information contained on bridge to determine if the task should be stopped or canceled. Mind that the task uses Wicket injection machinery to inject an implementation of IAnswerService, so, that mean we need to have Application as a thread local when Injector.get().inject(this); is called. This is achieved by providing a runnable that beside executing tasks, will
make sure Application and Session are attached, and properly detached, as thread locals.
Additionally, we provide custom Session and Application classes containing the machinery to track user’s running tasks and to launch tasks, respectively. See
and
The WEB layer to handle/manage tasks.
The WEB layer is more or less standard Wicket. The more complex class is TasksListPanel which uses an AjaxFallbackDefaultDataTable in order to display running tasks. This panel, contains an
AjaxSelfUpdatingTimerBehavior that takes care of repainting the panel to show tasks progress. There are other user interactions, like creating new tasks and pruning “dead” tasks, but we will
not get into the details.
Final words
We hope this example helps you get started in rolling your own solution.
Last but not least, I would like to thanks Martin Grigorov for guiding me in writing this small article, making nice amendments to it and for he’s invaluable work maintaining Apache Wicket.