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

1

6

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.

Smelter answered 24/8, 2012 at 9:47 Comment(2)
are you using Spring by any chance?Vermillion
No it's just Java EE 5 + Seam (old)Smelter
F
4

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.

Footstep answered 24/8, 2012 at 9:50 Comment(2)
No way to do it propertly without EJB? (I'm on JBoss 4.x, long way to Java EE 6)Smelter
@PiotrGwiazda: TimerService was introduced in EJB 2.1, so you should be fine on JBoss 4.x.Footstep

© 2022 - 2024 — McMap. All rights reserved.