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.

2
  • are you using Spring by any chance?
    – Eugene
    Aug 24, 2012 at 9:48
  • No it's just Java EE 5 + Seam (old) Aug 24, 2012 at 9:52

1 Answer 1

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.

2
  • No way to do it propertly without EJB? (I'm on JBoss 4.x, long way to Java EE 6) Aug 24, 2012 at 9:54
  • @PiotrGwiazda: TimerService was introduced in EJB 2.1, so you should be fine on JBoss 4.x. Aug 24, 2012 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.