users@glassfish.java.net

Timer and Timeouts

From: <forums_at_java.net>
Date: Tue, 28 Jun 2011 16:48:39 -0500 (CDT)

 

Hey guys, I'm confused about the use of Timer in EJBs. It seems that the
timer doesn't start until after the method invocation is complete... which
seems wrong. I'm trying to have a long running process that could listen for
a JMS message to cancel itself in a safe manner.

I have the code:

<pre>

boolean interupt = false;

public String echo(String echoString) {

service.createTimer(1L, 1L, UUID.randomUUID());

log.info("echo() invoked. echoString:{}", echoString);

//do long running process

for (long i = 0; i < 9999999999L; i++) {

}

return "complete";

}

@Timeout

void checkForInteruptSignal(Timer timer) {

System.out.println("Checking for interupt...");

if (interupt) {

timer.cancel();

return;

}

try {

Connection conn = connFactory.createConnection();

Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);

MessageConsumer consumer = session.createConsumer(interuptNotifications);

Message message = consumer.receiveNoWait();

if (message != null) {

interupt = true;

timer.cancel();

}

} catch (JMSException e) {

e.printStackTrace();

}

</pre>

The problem here is that the timer doesn't seem to actually start until the
echo() method completes. I would expect that the timer start immediately
after createTimer() is called. Why is that?


--
[Message sent by forum member 'exabrial']
View Post: http://forums.java.net/node/817104