简体   繁体   中英

Two methods at same time using annotation @Asynchronous

I´m developping a Web Service Application(JDK 1.7). I want to execute two methods at same time using annotation @Asynchronous. My code is described below:

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

}

What I'm doing wrong? How can I perform this operation?

You have a couple of things wrong here.

  1. You do not need to use an ExecutorService if you're calling @Asynchronous methods. The container manages this stuff for you;

  2. You're calling the get method on the futures that are returned from the @Asynchronous methods. The get method blocks until the future has been fulfilled asynchronously. Typically, you delay calling Future.get() until you really really need the value, the idea being that there's lots of other processing you could be doing in the meantime.

Try

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM