我嘗試將異步登錄過程的結果保存到數據庫中,以便在登錄方法期間節省時間。但不知何故,如果我添加一個thread.sleep到異步方法登錄過程需要更長的時間。這是爲什麼?我認爲authenticate方法不會等待writeResultToStats方法完成。EJB中的異步調用方法
@Stateless
@LocalBean
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CustomerBeanTest {
@PersistenceContext(unitName = WebPersistenceUnits.QISADS)
private EntityManager em_local;
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void authenticate(Long loginid, String cmppassword) {
try {
Login l = em_local.find(Login.class, loginid);
String s = l.getPassword();
if (!s.equalsIgnoreCase(cmppassword))
throw new PasswordMissmatchException();
writeResultToStats(loginid, true);
} catch (PasswordMissmatchException e) {
writeResultToStats(loginid, false);
}
}
@Asynchronous
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void writeResultToStats(Long loginID, boolean success) {
try { // just for testing
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LogUtils log = new LogUtils(this);
Login l = em_local.find(Login.class, loginID);
if (success) {
l.setSuccessLast(new Date());
l.setSuccessCount(l.getSuccessCount()+1);
log.log(Level.INFO, "Update Login Stat Success [%d, %s, %d]", l.getId(), l.getName(), Thread.currentThread().getId());
} else {
l.setFailureLast(new Date());
l.setFailureCount(l.getFailureCount()+1);
log.log(Level.INFO, "Update Login Stat Fail [%d, %s, %d]", l.getId(), l.getName(), Thread.currentThread().getId());
}
}
}
我認爲你需要打出來的異步方法進入另一個EJB。就像現在一樣,它只是作爲本地方法調用來處理。 –
讓我試試這個並回復你 –
它的工作原理。謝謝。 –