}
final boolean isRetrying = (retCnt > 0 && retCnt < maxRetry-1);
ProcessManagerFactoryBean pmfb = new ProcessManagerFactoryBean();
ProcessManagerRemote pm = null;
ProcessInstance instance = null;
try{
if(act.isQueuingEnabled()){
pm = pmfb.getProcessManager();
instance = pm.getProcessInstance(instanceId);
}else{
instance = finalInstance;
}
long timeInMillis_start = System.currentTimeMillis();
System.out.println("- [uEngine] Start Executing Activity: " + act.getName() + " (" + act.getTracingTag() + ")");
instance.execute(act.getTracingTag());
long elapsedTime = (System.currentTimeMillis() - timeInMillis_start);
PrintStream logWriter = (elapsedTime < ERROR_LEVEL_TIMEINMS ? System.out : System.err);
logWriter.println("- [uEngine] End Executing Activity: " + act.getName() + " (" + act.getTracingTag() + ") - Elapsed Time : " + elapsedTime);
if(pm!=null)
pm.applyChanges();
success = true;
}catch(Exception e){
if(instance instanceof SimulatorProcessInstance){
try {
act.fireFault(instance, e);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
UEngineException ue = null;
if(!(e instanceof UEngineException)){
ByteArrayOutputStream bao = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bao));
try{
ue = new UEngineException("uEngine Exception: " + e + "("+e.getMessage()+")", e);
ue.setDetails(bao.toString());
}catch(Exception e3){
e3.printStackTrace();
}
}else
ue = (UEngineException)e;
if(GlobalContext.useEJB)
WorkProcessorBean.fireFault(instance, act.getTracingTag(), ue);
else{
final UEngineException finalUE = ue;
/**
* run it after roll-back the main transaction to prevent that the fault marking job
* would be rolled back as well.
*/
instance.getProcessTransactionContext().addTransactionListener(new TransactionListener(){
public void beforeCommit(TransactionContext tx) throws Exception {
// TODO Auto-generated method stub
}
public void beforeRollback(TransactionContext tx) throws Exception {
// TODO Auto-generated method stub
}
public void afterCommit(TransactionContext tx) throws Exception {
afterRollback(tx);
}
public void afterRollback(TransactionContext tx) throws Exception {
Thread faultMarker = new Thread(){
public void run() {
ProcessManagerFactoryBean pmfb = new ProcessManagerFactoryBean();
ProcessManagerRemote pm = null;
ProcessInstance instanceForFaultMarking = null;
try{
pm = pmfb.getProcessManager();
instanceForFaultMarking = pm.getProcessInstance(instanceId);
try{
//String oldStatus = act.getStatus(instanceForFaultMarking);
act.fireFault(instanceForFaultMarking, finalUE);
if(isRetrying){//Activity.STATUS_RETRYING.equals(oldStatus)){
act.setStatus(instanceForFaultMarking, Activity.STATUS_RETRYING);
}
}catch(Exception e){
throw new RuntimeException(e);
//e.printStackTrace();
}
pm.applyChanges();
} catch (Exception e1) {
if(pm!=null)
try {
pm.cancelChanges();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally{
try {
pm.remove();
} catch (RemoteException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (RemoveException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
};
//faultMarker.run();
faultMarker.start(); //run it in a different thread if you want to make sure to separate the transaction.
}
});
}
}
/* if(act.isQueuingEnabled()){
try {
if(retCnt < act.getRetryLimit()){
act.setStatus(instance, Activity.STATUS_RETRYING);
act.setRetryCount(instance, retCnt+1);
run(); //recursive call to retry
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}*/
if(!act.isQueuingEnabled() && instance.getProcessTransactionContext().getSharedContext("faultTolerant")==null){
UEngineException richException = new UEngineException(e.getMessage(), null, e, instance, act);
throw new RuntimeException(richException);
}
if(pm!=null){
try {
pm.cancelChanges();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}finally{
if(pm!=null)
try {
pm.remove();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoveException e) {
// TODO Auto-generated catch block