两个方法同时使用注释@Asynchronous

时间:2015-12-23 22:05:58

标签: java java-ee asynchronous concurrency executorservice

我正在开发一个Web服务应用程序(JDK 1.7)。我想使用注释@Asynchronous同时执行两个方法。我的代码如下所述:

@Stateless
@Asynchronous
public class One {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Asynchronous
    public Future<String> doSomething1(String arg1) {
        return new AsyncResult(arg1);
    }
}


@Stateless
@Asynchronous
public class Two {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Asynchronous
    public Future<String> doSomething2(String arg2) {
        return new AsyncResult(arg2);
    }
}

public class OnePlusTwo {   

    private String operation1;
    private String operation2;

    /**
     * @return the operation1
     */
    public String getOperation1() {
        return operation1;
    }

    /**
     * @param operation1 the operation1 to set
     */
    public void setOperation1(String operation1) {
        this.operation1 = operation1;
    }

    /**
     * @return the operation2
     */
    public String getOperation2() {
        return operation2;
    }

    /**
     * @param operation2 the operation2 to set
     */
    public void setOperation2(String operation2) {
        this.operation2 = operation2;
    }
}

@Stateless
public class WholeOperation {

    @EJB
    One one;

    @EJB
    Two two;

    public OnePlusTwo getOnePlusTwo() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        OnePlusTwo onePlusTwo = new OnePlusTwo();
        //WHAT TO DO HERE????
        executorService.execute(onePlusTwo.setOperation1(one.doSomething1("at").get()));
        executorService.execute(onePlusTwo.setOperation2(two.doSomething2("at same time").get()));
        executorService.shutdown();
        return onePlusTwo;
    }

}

我做错了什么?我该如何执行此操作?

1 个答案:

答案 0 :(得分:3)

你在这里犯了一些错误。

  1. 如果您正在调用@Asynchronous方法,则无需使用ExecutorService。容器为您管理这些东西;

  2. 您正在调用@Asynchronous方法返回的期货的get方法。 get方法阻塞,直到异步完成未来。通常情况下,你会延迟调用Future.get(),直到你确实需要这个值为止,这个想法是你可以在此期间进行大量的其他处理。

  3. 尝试

    public class OnePlusTwo {   
    
        private final Future<String> operation1;
        private final Future<String> operation2;
    
        public OnePlusTwo(Future<String> operation1, Future<String> operation2) {
            this.operation1 = operation1;
            this.operation2 = operation2;           
        }
    
        /**
         * @return the operation1
         */
        public String getOperation1() {
            return operation1.get();
        }
    
        /**
         * @return the operation2
         */
        public String getOperation2() {
            return operation2.get();
        }
    
    }
    
    @Stateless
    public class WholeOperation {
    
        @EJB
        One one;
    
        @EJB
        Two two;
    
        public OnePlusTwo getOnePlusTwo() {
    
            Future<String> f1 = one.doSomething1("at");
            Future<String> f2 = two.doSomething2("at same time");
    
            OnePlusTwo onePlusTwo = new OnePlusTwo(f1, f2);
    
            return onePlusTwo;
        }
    
    }