Timer Service in ejb 3.1 - schedule calling timeout problem

Posted by Greg on Stack Overflow See other posts from Stack Overflow or by Greg
Published on 2010-04-22T14:48:15Z Indexed on 2010/04/23 5:23 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

Hi Guys,

I have created simple example with @Singleton, @Schedule and @Timeout annotations to try if they would solve my problem. The scenario is this: EJB calls 'check' function every 5 secconds, and if certain conditions are met it will create single action timer that would invoke some long running process in asynchronous fashion. (it's sort of queue implementation type of thing). It then continues to check, but as long as long running process is there it won't start another one.

Below is the code I came up with, but this solution does not work, because it looks like asynchronous call I'm making is in fact blocking my @Schedule method.

@Singleton @Startup public class GenerationQueue {

private Logger logger = Logger.getLogger(GenerationQueue.class.getName());

private List<String> queue = new ArrayList<String>();

private boolean available = true;

@Resource
TimerService timerService;

@Schedule(persistent=true, minute="*", second="*/5", hour="*")
public void checkQueueState() {

    logger.log(Level.INFO,"Queue state check: "+available+" size: "+queue.size()+", "+new Date());

    if (available) {

        timerService.createSingleActionTimer(new Date(), new TimerConfig(null, false));
    }

}

@Timeout
private void generateReport(Timer timer) {

    logger.info("!!--timeout invoked here "+new Date());

    available = false;

    try {

        Thread.sleep(1000*60*2); // something that lasts for a bit

    } catch (Exception e) {}

    available = true;

    logger.info("New report generation complete");

}

What am I missing here or should I try different aproach? Any ideas most welcome :)

Testing with Glassfish 3.0.1 latest build - forgot to mention

© Stack Overflow or respective owner

Related posts about java

Related posts about timer