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

Java 同时使用注释@Asynchronous的两个方法,java,jakarta-ee,asynchronous,concurrency,executorservice,Java,Jakarta Ee,Asynchronous,Concurrency,Executorservice,我正在开发一个Web服务应用程序(JDK1.7)。我想使用annotation@Asynchronous同时执行两个方法。我的代码描述如下: @Stateless @Asynchronous public class One { // Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method") @Asynchronous

我正在开发一个Web服务应用程序(JDK1.7)。我想使用annotation@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;
    }

}
@无状态
@异步的
公共一级{
//在下面添加业务逻辑。(右键单击编辑器并选择
//“插入代码>添加业务方法”)
@异步的
公共未来doSomething1(字符串arg1){
返回新的异步结果(arg1);
}
}
@无国籍
@异步的
公共二班{
//在下面添加业务逻辑。(右键单击编辑器并选择
//“插入代码>添加业务方法”)
@异步的
公共未来doSomething2(字符串arg2){
返回新的异步结果(arg2);
}
}
公共类一加二{
私有字符串操作1;
私有字符串操作2;
/**
*@返回操作1
*/
公共字符串getOperation1(){
返回操作1;
}
/**
*@param operation1要设置的操作1
*/
公共void设置操作1(字符串操作1){
此操作1=操作1;
}
/**
*@返回操作2
*/
公共字符串getOperation2(){
返回操作2;
}
/**
*@param operation2要设置的操作2
*/
公共void设置操作2(字符串操作2){
此操作2=操作2;
}
}
@无国籍
公营部门通力合作{
@EJB
一个,;
@EJB
两个,;
public OnePlusTwo getOnePlusTwo(){
ExecutorService ExecutorService=Executors.newFixedThreadPool(2);
OnePlusTwo OnePlusTwo=新的OnePlusTwo();
//在这里做什么????
executorService.execute(onePlusTwo.setOperation1(one.doSomething1(“at”).get());
executorService.execute(onePlusTwo.setOperation2(两个.doSomething2(“同时”).get());
executorService.shutdown();
返回一加二;
}
}

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

您这里有几处错误

  • 如果调用@Asynchronous方法,则不需要使用ExecutorService。容器为您管理这些内容

  • 您正在对@Asynchronous方法返回的未来调用
    get
    方法。
    get
    方法会一直阻塞,直到异步完成未来。通常,您会延迟调用Future.get(),直到您真的需要该值为止,因为在此期间您还可以进行很多其他处理

  • 试一试

    公共类OnePlusTwo{
    私人最终未来运营1;
    私人最终未来运营2;
    公共一加二(未来运营1,未来运营2){
    此操作1=操作1;
    此操作2=操作2;
    }
    /**
    *@返回操作1
    */
    公共字符串getOperation1(){
    返回操作1.get();
    }
    /**
    *@返回操作2
    */
    公共字符串getOperation2(){
    返回操作2.get();
    }
    }
    @无国籍
    公营部门通力合作{
    @EJB
    一个,;
    @EJB
    两个,;
    public OnePlusTwo getOnePlusTwo(){
    未来f1=1.doSomething1(“at”);
    未来f2=两个doSomething2(“同时”);
    OnePlusTwo OnePlusTwo=新的OnePlusTwo(f1,f2);
    返回一加二;
    }
    }
    
    这里有几件事你做错了

  • 如果调用@Asynchronous方法,则不需要使用ExecutorService。容器为您管理这些内容

  • 您正在对@Asynchronous方法返回的未来调用
    get
    方法。
    get
    方法会一直阻塞,直到异步完成未来。通常,您会延迟调用Future.get(),直到您真的需要该值为止,因为在此期间您还可以进行很多其他处理

  • 试一试

    公共类OnePlusTwo{
    私人最终未来运营1;
    私人最终未来运营2;
    公共一加二(未来运营1,未来运营2){
    此操作1=操作1;
    此操作2=操作2;
    }
    /**
    *@返回操作1
    */
    公共字符串getOperation1(){
    返回操作1.get();
    }
    /**
    *@返回操作2
    */
    公共字符串getOperation2(){
    返回操作2.get();
    }
    }
    @无国籍
    公营部门通力合作{
    @EJB
    一个,;
    @EJB
    两个,;
    public OnePlusTwo getOnePlusTwo(){
    未来f1=1.doSomething1(“at”);
    未来f2=两个doSomething2(“同时”);
    OnePlusTwo OnePlusTwo=新的OnePlusTwo(f1,f2);
    返回一加二;
    }
    }
    
    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;
        }
    
    }