Asynchronous 关联异步任务';s带会话的完成/进度监视器

Asynchronous 关联异步任务';s带会话的完成/进度监视器,asynchronous,java-8,wildfly,wildfly-10,stateless-session-bean,Asynchronous,Java 8,Wildfly,Wildfly 10,Stateless Session Bean,我希望能够在java中执行异步任务,并且能够保持与用户会话相关联的完成(如果可能,还有进度)监视器。这可能吗?如果可能,怎么做 目前,该任务是作为无状态会话bean方法同步实现的,该方法是从jax-rs端点调用的 我查看了,但是AsyncResult不可序列化,所以我想我无法将其添加到会话中。使用Spring注释@Async,可以使任何bean/方法异步。 容器将创建一个新线程,方法将异步执行。还可以将会话对象传递到此方法中,完成后,可以在会话对象中标记属性。 示例:-JSF示例,在Wildfl

我希望能够在java中执行异步任务,并且能够保持与用户会话相关联的完成(如果可能,还有进度)监视器。这可能吗?如果可能,怎么做

目前,该任务是作为无状态会话bean方法同步实现的,该方法是从jax-rs端点调用的


我查看了,但是
AsyncResult
不可序列化,所以我想我无法将其添加到会话中。

使用Spring注释@Async,可以使任何bean/方法异步。 容器将创建一个新线程,方法将异步执行。还可以将会话对象传递到此方法中,完成后,可以在会话对象中标记属性。
示例:-

JSF示例,在Wildfly中工作:

1内部视图(xhtml)我们有一个上传表单和进度表

<h:form>
    <div align="justify">
    <p:fileUpload style="width: auto" fileUploadListener="#{fileUploadCtrl.handleFileUpload}" mode="advanced" label="Please pick XLS file" update="messages" auto="true" sizeLimit="1000000" allowTypes="/(\.|\/)(xls|xlsx)$/" />
    <p:growl id="messages" showDetail="false" life="4000"/>
    </div>
</h:form>

 <h:form id="wholeform">
    <h:outputText id="statusot" value="#{fileUploadCtrl.message}" />
    <p:spacer width="10" height="10"/>
    <p:poll interval="1" listener="#{fileUploadCtrl.updateStatus}" update="wholeform" />
</h:form>

2在控制器中,这是一个托管bean,我们处理文件并更新一次状态

@ManagedBean
@ViewScoped

public class FileUploadCtrl {

@EJB
private SomeBusinessLogicClass model;

@EJB
private ProgressTracker progress;

private Future<List<String>> asyncResult;
private int progressId = 0;
private String message;
private boolean busy = false;

public void handleFileUpload(FileUploadEvent event) {

    Set<String> ids = model.populate(event.getFile().getContents());

    progressId = progress.newIndex();
    asyncResult = model.process(ids);
    busy = true;

    FacesMessage message = new FacesMessage("Loaded " + ids.size() + " objects", "");
    FacesContext.getCurrentInstance().addMessage(null, message);
}

public void updateStatus() {

    if (!busy)
        return;

    try {

        if (asyncResult.isDone()) {
            List<String> r = asyncResult.get();
            message = "Job done";
            busy = false;
            progress.delIndex(progressId);
        } else {
            message = progress.getIndex(progressId)+"-th element in work";
        }

    } catch (Exception e) {
        System.out.println("updateStatus " + e.toString());
    }
}
@ManagedBean
@视域
公共类FileUploadCtrl{
@EJB
私有类模型;
@EJB
私人进度跟踪进度;
私人未来结果;
private int progressId=0;
私有字符串消息;
私有布尔忙=假;
公共无效handleFileUpload(FileUploadEvent事件){
Set id=model.populate(event.getFile().getContents());
progressId=progress.newIndex();
asyncResult=model.process(ids);
忙=真;
FacesMessage=newFacesMessage(“加载的”+ids.size()+“对象”,”);
FacesContext.getCurrentInstance().addMessage(空,消息);
}
公共空间更新状态(){
如果(!忙)
返回;
试一试{
if(asyncResult.isDone()){
List r=asyncResult.get();
message=“工作完成”;
忙=假;
progress.delIndex(progressId);
}否则{
message=progress.getIndex(progressId)+“工作中的第四个元素”;
}
}捕获(例外e){
System.out.println(“updateStatus”+e.toString());
}
}
3所有的业务逻辑都在EJB中,就像某些BusinessLogicClass或许多其他的。另外,我们需要一个简单的ProgressManagerEJB,我完全发布了它

@Singleton
public class ProgressTracker {

private Map<Integer,Integer> indexes = new HashMap<>();

public Map<Integer, Integer> getIndexes() {
    return indexes;
}

public void setIndexes(Map<Integer, Integer> indexes) {
    this.indexes = indexes;
}

public Integer newIndex() {
    Integer size = indexes.size();
    indexes.put(size,0);
    return size;
}

public void incIndex(final Integer index) {

    int old = indexes.get(index);
    old++;
    indexes.put(index,old);
}

public Integer getIndex(final Integer index) {
    return indexes.get(index);
}

public void delIndex(Integer index) {
    indexes.remove(index);
}
@Singleton
公共类进度跟踪程序{
私有映射索引=新的HashMap();
公共映射getIndexes(){
收益指标;
}
公共void集合索引(映射索引){
this.index=索引;
}
公共整数newIndex(){
整数大小=索引。大小();
索引。put(大小,0);
返回大小;
}
索引中的公共void(最终整数索引){
int old=index.get(index);
old++;
索引。放置(索引,旧);
}
公共整数getIndex(最终整数索引){
返回索引。获取(索引);
}
公共void delIndex(整数索引){
删除(索引);
}
}


也许这个例子并不优雅,我对前端几乎是新手,但它很有效,比什么都没有好。

你的目标对我来说不清楚。你打算继续使用jax rs,或者你不能用其他方法吗?我可以分享一个wildfly+JSF的工作例子,它做以下事情:客户端上传一个文件,这个文件在后台和客户端处理可以在OutputExt中查看已处理的行数解决方案不需要绑定到
jax rs
。我想如果我可以将进度监视器保存到会话中,我使用什么作为端点应该无关紧要。我希望用户能够随时访问页面并查看其任务的进度(或者至少是是否完成),但解决方案不应包括添加数据库状态(仅会话)。因此,如果可能的话,请分享代码。我想知道为什么这个答案会得到提升和奖励。Spring是一种与Wildfly完全无关的技术。JavaEE和Wildfly有@Asynchronous annotation。那又怎么样?如果不明显,在模型中调用业务方法(如model.process(ids);应该使用@Asynchronous annotation完成