0

I don't really have a good knowledge of EJB components, CDI and stuff. I'm having a lot of trouble with asynchronous EJB. I tried some tutorials but I couldn't understand or make them work adapting to the code I'm maintaining here.

I would like my EJB to return an answer to my Managed Bean when it finishes executing. ManagedBean should listen to it and then do other stuff (in my case I need to update view components, which could be done with a redirect), but it can't block navigation and other actions from user.

So far, this is what I got:

1 - this is my managed Bean:

@Named
@ViewScoped
public class MyManagedBean {

@Inject
private ExecutionService executionService;

public void executeAsyncTask(Suite suite) {
    try {
        Future<Boolean> result = executionService.executeStuff(suite);
        if (result.isDone()) {
            // send a redirect if he is in the same page of the button that calls this method. It doesn't work, it's result is always false because execution is not finished and method moves on with his life
        } 
}

2 - And this is my EJB:

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class ExecutionService extends BaseBean implements Serializable {
//
    @Inject private ExecutionDAO execDao;
//
    @Asynchronous
    public Future<Boolean> executeStuff(Suite suite) throws BusinessException {

        Boolean areStiffExecuted = false;

        Process process;

        try {
            process = Runtime.getRuntime().exec(new String[]{ });
            // and other crazy stuff that take some time.
        } catch (Exception e) {
            // log exception and throw it to managed bean
        } finally {
            // do some stuff like destroying process
            return new AsyncResult<>(true);
        }
    }
}

Do you guys have any idea of how to get what I want?

Denis Klein
  • 101
  • 1
  • 11
  • [This](https://stackoverflow.com/questions/25947790/real-time-updates-from-database-using-jsf-java-ee) is a good place to start. – Kukeltje Jul 17 '18 at 19:39
  • And [this](https://stackoverflow.com/questions/3787514/how-can-server-push-asynchronous-changes-to-a-html-page-created-by-jsf) – Kukeltje Jul 17 '18 at 19:57
  • Man, believe it or not, none of my previous researches led me to the websocket solutions. I had to go with Omnifaces (apparently WildFly 10, that is a requirement here, does not accept JSF 2.3). I also had a hard time understanding how to use Consumer objects but your links were the tip I needed to find examples of them being used and create my own answer to it. I was so far from an answer... thank you so much! – Denis Klein Jul 18 '18 at 04:13
  • Possible duplicate of [How can server push asynchronous changes to a HTML page created by JSF?](https://stackoverflow.com/questions/3787514/how-can-server-push-asynchronous-changes-to-a-html-page-created-by-jsf) – Kukeltje Jul 18 '18 at 06:01
  • You are welcome! – Kukeltje Jul 18 '18 at 06:03

1 Answers1

0

Following the links provided by Kukeltje I found stuff about websockets (yeah, I had never heard of them) and went with Omnifaces version since it was already a part of the project. I followed the websocket instructions from Omnifaces showcase.

I also followed BalusC explanation about them and complemented my complete ignorance on this subject in this post about Consumer objects. It looks like I made something that works out of it all!

Denis Klein
  • 101
  • 1
  • 11
  • Hi, this is 'officially' not an answer to the question (or at least not a 'good' one). It only contains links to external sites without any detail, see [answer]. Cheers (I marked the Q as a duplicate btw since the basics are a duplicte.) – Kukeltje Jul 18 '18 at 06:33