我正在开发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;
    }

}


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

最佳答案

您在这里有几处错误。


如果要调用@Asynchronous方法,则无需使用ExecutorService。容器为您管理这些东西;
您正在从@Asynchronous方法返回的期货上调用get方法。 get方法将阻塞,直到异步完成了将来。通常,您延迟调用Future.get()直到您真正需要该值为止,其想法是在此期间您可能还要执行许多其他处理。


尝试

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;
    }

}

10-08 01:41