Expose current progress of an @Asynchronous function to use in View

孤街浪徒 提交于 2019-12-07 17:55:29

Nobody? Ok so this is my solution. Im not sure if this is a big fat workaround or just a way to get this done.

Since an @Asynchronous method cannot access the Session context, and therefore also no Session Beans (at least i dont know how, i always got ConcurrentModificationErrors or similar ones) i created a Singleton ProgressEJB, which contains a HashMap:

@Singleton @LocalBean @Startup
public class ProgressEJB {
  private HashMap<String, Integer> progressMap = new HashMap<String, Integer>
  // getters and setters
}

This hashmap should map the SessionId (a String) to an Integer value (the progress 0->100). So a user session is associated with a progress. In my EmailEJB, i'm injecting this ProgressEJB, and in my @Asynchronous method, i'm increasing the value everytime an email has been sent:

@Stateful @LocalBean
public class EmailEJB {
@Inject
private ProgressEJB progress;
// Mail-Settings
...
@Asynchronous
public void sendEmails(user:User, message:Message, sessionId:String) {
  progress.progressMap.put(sessionId, 0);
  for (int i=0; i<mails.size; i++) {
    sendMail(mails[i])
    progress.getProgressMap().put(sessionId, (i / mails.size) * 100)
  }
  progress.getProgressMap().remove(sessionId);
}

The sessionId comes from my Managed (Weld) Bean, when calling the function:

@SessionScoped
@Named
public class EmailManager {
  @Inject 
  private ProgressEJB progress;
  @Inject
  private FacesContext facesContext;

  private String sessionId;

  @PostConstruct
  private void setSessionId() {
    this.sessionId = ((HttpSession)facesContext.getExternalContext().getSession(false)).getId();
  }

  public Integer getProgress() {
    if (progress.getProgressMap().get(sessionId) == null)
      return 100;
    else 
      return progress.getProgressMap().get(sessionId);
  }
}

Now i can access progress from EmailManager from my JSF view with Ajax Polling, telling the user how many mails already have been sent. Just tested it with 2 users, seems to work.

I also see only a @Singleton solution here. But this imply the need of Housekeeping in ProgressEJB. E.g. some effort is needed to prune old session from Hashmap.

Another solution is described in Is there any way to know the progress of a EJB Asynchronous process?

This solution does not need a Stateful Bean.

@Stateless
public class EmailEJB {
    // Mail-Settings
    ...
    @Asynchronous
    public void sendEmails(User user, Message message, WorkContext context) {
      progress.progressMap.put(sessionId, 0);
      for (int i=0; i<mails.size; i++) {
        sendMail(mails[i])
        context.setProgress((i / mails.size) * 100)
      }
      context.setRunning(false);
    }
}

The Context-Object, which holds the progress.

public class WorkContext {
    //volatile is important!
    private volatile Integer progress = 0;
    private volatile boolean running = false;    
    // getters & setters
}

The usage is very easy.

@SessionScoped
@Named
public class EmailManager {
  @Inject 
  private EmailEJB emailEJB;
  private WorkContext workContext;

  public void doStuff() {
        workContext = new WorkContext();
        emailEJB.sendEmails(user, message, workContext)
  }

  public Integer getProgress() {
        return workContext.getProgress();
  }
  ....
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!