1
我有一個EJB來計算某物。只要計算運行,精度就越高。 所以一個異步函數開始計算,一個異步應該停止它。但在Glassfish 3.1.2上運行,調用stopCalculating()
不會創建新線程,而是等到startCalculating()
結束時,顯然永遠不會發生。Glassfish 3.1.2似乎不在一個EJB中運行多個線程
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Singleton;
@Singleton
public class Calculator {
private boolean calculating = false;
private String result = "Empty";
@Asynchronous
public void startCalculating() {
calculating = true;
Logger.getGlobal().log(Level.INFO, "Starting!");
calculate();
}
private void calculate() {
result = "";
while(calculating) {
/*Calculate and update result*/
Logger.getGlobal().log(Level.INFO, "Calculate...");
}
}
@Asynchronous
public Future<String> stopCalculating() {
Logger.getGlobal().log(Level.INFO, "Stopping!");
calculating = false;
return new AsyncResult<String>(result);
}
}
我怎樣才能得到Glassfish的到在其它線程中運行stopCalculating()
?
謝謝,這幫了我很多。 – user2140206 2013-03-12 14:21:42