简体   繁体   中英

How to run parallel processes in Java EE

In my test application (JSF, PrimeFaces, Wildfly 8) I have a simple messaging interface for writing a message and viewing those that were sent to me. I now would like to create an automatic respond for those messages that were not answered after 5 minutes. This automatic respond is meant to be independent of any person sitting behind the browser window and clicking the refresh button.

My current idea is to create a new class annotated with @ApplicationScoped. This class shall run an individual thread handling the responding process. Would that be a good approach? Or is there a functionality somewhere that already covers this matter?

I think the option worth considering would be to use javax.ejb.TimerService:

@Stateless
public class AutomaticResponseSender {

    private static final Integer _5_MINS = 300000;

    @Resource
    private TimerService timerService;

    public void waitFiveMinutes() {
        Timer nextTimer = timerService.createSingleActionTimer(_5_MINS, null);
    }

    @Timeout
    public void autoRespondForNotAnswered() {
          // here send an auto response
    }

    public void cancel() {
        timerService.cancel();
    }
}

Upon sending the manual answer you need to call cancel() to avoid autoresponse or, alternatively, you can check in autoRespondForNotAnswered() method if the answer was send.

Another approach could be to use @Schedule and, let's say every 5 or 10 seconds, check for messages not answered in 5 mins and sending automatic answers for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM