我正在开发一个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;
}
}我哪里做错了?如何执行此操作?
发布于 2015-12-24 06:32:21
您在这里有几个地方是错的。
如果要调用@方法,则不需要使用ExecutorService。容器为您管理这些东西;,
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;
}
}https://stackoverflow.com/questions/34444447
复制相似问题