diff --git a/cws-tasks/src/main/java/jpl/cws/task/CwsTask.java b/cws-tasks/src/main/java/jpl/cws/task/CwsTask.java index d446687a..a1f343b5 100644 --- a/cws-tasks/src/main/java/jpl/cws/task/CwsTask.java +++ b/cws-tasks/src/main/java/jpl/cws/task/CwsTask.java @@ -2,7 +2,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; import org.camunda.bpm.engine.delegate.BpmnError; import org.camunda.bpm.engine.delegate.DelegateExecution; @@ -16,7 +15,7 @@ /** * Abstract base class for all CWS built-in (and custom user-added) task * implementations. - * + * */ public abstract class CwsTask implements JavaDelegate { @@ -56,32 +55,32 @@ public CwsTask() { * 1) initializes parameters * 2) runs the task implementation * 3) throws any qualified exceptions - * + * */ public void execute(final DelegateExecution execution) { this.execution = execution; - + // setup tags on logging log.setProcTags(getProcDefKey(execution), - execution.getProcessInstanceId(), + execution.getProcessInstanceId(), execution.getActivityInstanceId()); - + try { // setup base params throwOnTruncatedVariableBoolean = getBooleanParam( throwOnTruncatedVariable, "throwOnTruncatedVariable", DEFAULT_THROW_ON_TRUNCATED_VARIABLE); - + // Evaluate preCondition. // If preCondition passes, then execute task, // otherwise skip task execution. if (evaluateTaskPreCondition()) { setOutputVariable("preConditionPassed", true); - + // get params log.trace("INITIALIZING PARAMETERS FOR TASK: " + this); initParams(); - + // execute the task log.trace("EXECUTING TASK: " + this); executeTask(); @@ -92,17 +91,17 @@ public void execute(final DelegateExecution execution) { } catch (BpmnError e) { log.warn("Propagating BpmnError(" + e.getErrorCode() + ")..."); setOutputVariable("bpmnErrorMessage", e.getErrorCode()); - + // We saw an error, but we want to check with Camunda because this is by-passing our end-event listener notifyWorkerOfFailedProcess(); - + throw e; // propagate so engine can handle (if boundary catch defined) } catch (Throwable t) { log.error("Unexpected Throwable while executing " + this, t); setOutputVariable("unexpectedErrorMessage", t.getMessage()); - + notifyWorkerOfFailedProcess(); - + // wrap and propagate so engine can (if boundary catch defined) handle throw new BpmnError(UNEXPECTED_ERROR); } finally { @@ -110,24 +109,27 @@ public void execute(final DelegateExecution execution) { this.execution = null; } } - + private void notifyWorkerOfFailedProcess() { log.debug("notifying workers of failed process..."); - try { - // This delay is necessary, because the process must actually - // complete in Camunda before we can look at Camunda's records - // to get the true status of the process instance. - TimeUnit.SECONDS.sleep(2); - SpringApplicationContext applicationContext = new SpringApplicationContext(); - ProcessService cwsProcessService = (ProcessService) SpringApplicationContext.getBean("cwsProcessService"); - cwsProcessService.sendProcEventTopicMessageWithRetries(null, null, null, null, "sync"); - } catch (InterruptedException e) { - e.printStackTrace(); - } + (new Thread() { + public void run() { + try { + // This delay is necessary, because the process must actually + // complete in Camunda before we can look at Camunda's records + // to get the true status of the process instance. + sleep(1000); + ProcessService cwsProcessService = (ProcessService) SpringApplicationContext.getBean("cwsProcessService"); + cwsProcessService.sendProcEventTopicMessageWithRetries(null, null, null, null, "sync"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); } - - + + private String getProcDefKey(final DelegateExecution execution) { String procDefKey = "UNKNOWN"; @@ -157,34 +159,34 @@ private String getProcDefKey(final DelegateExecution execution) { /** * Implementation must be filled out by subclasses. - * + * */ protected abstract void initParams() throws Exception; /** * Implementation must be filled out by subclasses. - * + * */ protected abstract void executeTask() throws Exception; /** * Evaluates the task preCondition. - * + * * @return true if preCondition passes false if preCondition fails - * + * * @throws Exception if unexpected exception occurs * @throws BpmnError if process is to be determined. */ private boolean evaluateTaskPreCondition() throws Exception { - + if (!getBooleanParam(preCondition, "preCondition", DEFAULT_PRE_CONDITION)) { - + // Check special case for preCondition is "none" and pass as true if (preCondition != null && preCondition.getValue(execution).equals("none")) { return true; } - + log.warn("preCondition was not satisfied"); PreConditionFailBehavior failBehavior = PreConditionFailBehavior .valueOf(getStringParam(onPreConditionFail, @@ -267,7 +269,7 @@ protected String getStringParam(Expression expression, String paramName) } protected String getStringParam(Expression expression, String paramName, - String defaultValue) throws Exception { + String defaultValue) throws Exception { if (expression == null) { // return default return defaultValue; @@ -290,7 +292,7 @@ protected Boolean getBooleanParam(Expression expression, String paramName) } protected Boolean getBooleanParam(Expression expression, String paramName, - Boolean defaultValue) throws Exception { + Boolean defaultValue) throws Exception { if (expression == null) { // return default return defaultValue; @@ -320,7 +322,7 @@ protected Integer getIntegerParam(Expression expression, String paramName) } protected Integer getIntegerParam(Expression expression, String paramName, - Integer defaultValue) throws Exception { + Integer defaultValue) throws Exception { if (expression == null) { // return default return defaultValue; @@ -350,7 +352,7 @@ protected Long getLongParam(Expression expression, String paramName) } protected Long getLongParam(Expression expression, String paramName, - Long defaultValue) throws Exception { + Long defaultValue) throws Exception { if (expression == null) { // return default return defaultValue; @@ -382,7 +384,7 @@ protected Float getFloatParam(Expression expression, String paramName) } protected Float getFloatParam(Expression expression, String paramName, - Float defaultValue) throws Exception { + Float defaultValue) throws Exception { if (expression == null) { // return default return defaultValue; @@ -412,7 +414,7 @@ protected Double getDoubleParam(Expression expression, String paramName) } protected Double getDoubleParam(Expression expression, String paramName, - Double defaultValue) throws Exception { + Double defaultValue) throws Exception { if (expression == null) { // return default return defaultValue; @@ -435,10 +437,10 @@ private Double getDoubleValue(Object value, String paramName) /** * For now only supports Map but may want to support * generics/others in future... - * + * */ protected Map getMapParam(Expression expression, - String paramName) throws Exception { + String paramName) throws Exception { if (expression == null) { // no default, so throw exception throw new Exception("Mandatory parameter '" + paramName + "' not specified"); @@ -447,7 +449,7 @@ protected Map getMapParam(Expression expression, } protected Map getMapParam(Expression expression, - String paramName, Map defaultValue) + String paramName, Map defaultValue) throws Exception { if (expression == null) { // return default @@ -480,4 +482,4 @@ private Map getMapValue(Object value, String paramName) } } -} +} \ No newline at end of file