AsyncContext (GWT not Servlet 3.0)


I would like to use something like the AsyncContext from Servlet 3.0. I have to import a large xml in the database. This could be done in the background, after upload the file.

So I’ve found the AsyncContext and even managed to use the servlet 3.0 api with tomcat 7. But now GWT doesnt support this.

Is there an other way to achieve what I want to do?



Just start a new Thread which does the processing
For updating the UI you must take care to handle it correctly.

Here a example:

It’s important to have a widget on the screen which periodically polls for updates, the ProgressIndicator just does this.


Wow, seems too good to be true :slight_smile:

I have not thought of this because I thought its not allowed to make new Threads in an application server.

Maybe I’ve had to much the google app engine in mind. But there seems to be no problem with cloudfoundry:

You can’t, at least not directly, from an EJB (exceptfor a @Singleton bean). But from a Servlet, ServletContextListener, Filter, etc., you can. Just make sure you close them in an orderly way when the application is undeployed.


EJB has @Asynchronous for methods to indicate it run in a separate thread (automatically).


public interface SyncSessionBeanLocal {
    public java.util.concurrent.Future<com.vissimail.session.MailFetch> fetchMail(final SyncListener listener);


public class SyncSessionBean implements SyncSessionBeanLocal {

    protected SessionContext ctx;
    @PersistenceContext(name = "...")
    protected EntityManager em;

    public Future<MailFetch> fetchMail(final SyncListener listener) {
        final MailFetch mailFetch = new MailFetch();
        // This runs in a separate thread than the caller.... 
        return new AsyncResult<MailFetch>(mailFetch);

puvlic class Caller .... {
    private SyncSessionBeanLocal stateless;
    private Future<com.vissimail.session.MailFetch> job = null;

    public void buttonClick(ClickEvent event) {
        if (( this.job == null ) || ( this.job.isDone() )) {
            this.job = stateless.fetchMail(new SyncListenerWrapper(getSyncListener()));
        if (!this.job.isDone()) {
            final Lang lang = getLang();
            event.getButton().getWindow().showNotification("Sync is still running in background"), Window.Notification.TYPE_HUMANIZED_MESSAGE);
        } else {
            event.getButton().getWindow().showNotification("Sync is completed"), Window.Notification.TYPE_HUMANIZED_MESSAGE);

Make sure to synchronize on your application instance (if you access/modify any component from the thread) and not an injected proxy…

Getting the updates to your browser is a different story

Thanks, yes. I was only saying that your code can’t (*) start threads directly in an EJB unless it’s a singleton. You can, of course, get the container to start threads for you.

(*) Well, you can always
to start threads, heh heh. Some EE implementations might not catch that you’re doing it. I don’t recommend it though!