AsyncContext (GWT not Servlet 3.0)

Hi,

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?

Thanx

Bruno

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

Here a example:

http://demo.vaadin.com/sampler#ProgressIndicators

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

André

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: http://blog.frankel.ch/first-try-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.

Cheers,
Bobby

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

//


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

//


@Stateless(..)
@TransactionManagement(TransactionManagementType.BEAN)
public class SyncSessionBean implements SyncSessionBeanLocal {

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

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

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

    @Override
    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
try
to start threads, heh heh. Some EE implementations might not catch that you’re doing it. I don’t recommend it though!

Cheers,
Bobby