Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait() on Servlet throws Exception

I'm running a web app in Jboss application server and I'm trying to implement an event base response from the server.

To accomplish that I'm using .wait() and .notify() on the servlets class. Basicly there is an Ajax request, the servlet blocks with wait until there is an event on the server and if so notify is fired on the servlet.

The problem is when I do wait(1000*60) on the Servlet I get:

Servlet.service() for servlet ProcessesServlet threw exception: java.lang.IllegalMonitorStateException

Is it even possible to do a wait() on a HttpServlet class?

like image 296
out_sid3r Avatar asked Jun 08 '12 18:06

out_sid3r


1 Answers

Before you wait on an object, you must take ownership.

This is usualy done with a synchronized statement.

    synchronized (obj) {
        try {
            obj.wait(someTime);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
like image 199
Denys Séguret Avatar answered Sep 20 '22 04:09

Denys Séguret