java @Asynchronous方法:不运行异步 [英] java @Asynchronous Methods: not running async

查看:344
本文介绍了java @Asynchronous方法:不运行异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让异步进程运行。
基于此示例: http://tomee.apache。 org / examples-trunk / async-methods / README.html

I try to get an async process running. Based on this example: http://tomee.apache.org/examples-trunk/async-methods/README.html

但方法 addWorkflow(工作流工作流程)仅在运行(工作流工作流程)中的代码完全完成时返回。

But the method addWorkflow(Workflow workflow) will only return when the code in run(Workflow workflow) is fully completed.

然后当它返回并且 result.get(); 被调用我将获得异常:

Then when it returns and result.get(); is called I'll get the exception:


引起:java.lang.IllegalStateException:对象不表示真正的未来

Caused by: java.lang.IllegalStateException: Object does not represent an acutal Future

有什么建议我缺少什么?

Any suggestion what I'm missing?

@Singleton
public class WorkflowProcessor {

@EJB
private  WorkflowManager workflowManager;

private final static Logger log = Logger.getLogger(WorkflowProcessor.class.getName());



public void runWorkflows(Collection<Workflow> workflows) throws Exception{  
    final long start = System.nanoTime();
    final long numberOfWorkflows = workflows.size();
    Collection<Future<Workflow>> asyncWorkflows = new ArrayList<>();

    for(Workflow workflow : workflows){
        Future<Workflow> w = addWorkflow(workflow);
        asyncWorkflows.add(w);
    }    
    log.log(Level.INFO, "workflow jobs added {0}", new Object[]{numberOfWorkflows});
    for(Future<Workflow> result : asyncWorkflows){
       result.get();
    }

    final long total = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start);
    log.log(Level.INFO, "WorkflowProcessor->runWorkflows {0} workflows completed in:{1}", new Object[]{numberOfWorkflows, total});

}

@Asynchronous
@Lock(LockType.READ)
@AccessTimeout(-1)
private Future<Workflow> addWorkflow(Workflow workflow){

    run(workflow);

    return new AsyncResult<Workflow>(workflow);                
}


private void run(Workflow workflow){
    this.workflowManager.runWorkflow(workflow);
}


推荐答案

问题在于Java可以不要修饰隐含的this指针。

The issue is that Java can't decorate the implicit this pointer.

换句话说,@ Asynchronous注释将不会被处理,你正在进行普通的方法调用。

In other words, the @Asynchronous annotation won't be processed and you're doing an ordinary method call.

你可以注入你的单身人士,引用自己(称之为自我),然后调用self.addWorkflow。

You can inject your so singleton with a reference to itself (call this e.g. "self"), then call self.addWorkflow.

您可能还想考虑在无状态bean中运行异步代码。您正在为addWorkflow使用读锁定,但runWorkflow仍然具有写锁定。我认为你现在有一个死锁:你持有锁直到工作完成,但是在写锁被释放之前无法完成任务。

You might also want to consider running your async code in a stateless bean. You are using a read lock for addWorkflow, but runWorkflow still has a write lock. I think you have a dead lock now: you're holding the lock until work is done, but no work can be done until the write lock is released.

这篇关于java @Asynchronous方法:不运行异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆