Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a proper way to start scheduled task on Java EE 5 (JBoss) platform?

I need to run a simple scheduled task that will start every 200ms and do something simple.

Is Executors.newSingleThreadScheduledExecutor() the proper way of obtaining scheduled executor service on JBoss?

It is said that spawning unmanaged threads on Java EE platform is not recommended. It seems that this thread will be an unmanaged one.

On the other hand I don't want to declare MBeans etc. for such simple thing.

Edit

There is something as org.jboss.resource.work.JBossWorkManager but I can't find an example of scheduled work.

like image 609
Piotr Gwiazda Avatar asked Aug 24 '12 09:08

Piotr Gwiazda


1 Answers

Calling Executors.newSingleThreadScheduledExecutor() is not terrible, but better avoid it in EE containers. In Java EE 5 use TimeoutService:

@Stateless
public class TimerSessionBean implements TimerSession {
    @Resource
    TimerService timerService;

    public void startTimer() {
        Timer timer = timerService.createTimer(200, "Created new timer");
    }

    @Timeout
    public void timeout(Timer timer) {
        logger.info("Timeout occurred");
    }
}

In Java EE 6 you have handy @Schedule annotation.

like image 187
Tomasz Nurkiewicz Avatar answered Oct 17 '22 16:10

Tomasz Nurkiewicz