Skip to content

Commit

Permalink
Revert changes to CwsTask.java
Browse files Browse the repository at this point in the history
  • Loading branch information
wcgunter committed Feb 16, 2024
1 parent a0c1e68 commit 964b78f
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions cws-tasks/src/main/java/jpl/cws/task/CwsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +15,7 @@
/**
* Abstract base class for all CWS built-in (and custom user-added) task
* implementations.
*
*
*/
public abstract class CwsTask implements JavaDelegate {

Expand Down Expand Up @@ -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();
Expand All @@ -92,42 +91,45 @@ 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 {
// cleanup
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";

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -435,10 +437,10 @@ private Double getDoubleValue(Object value, String paramName)
/**
* For now only supports Map<String,String> but may want to support
* generics/others in future...
*
*
*/
protected Map<String, String> 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");
Expand All @@ -447,7 +449,7 @@ protected Map<String, String> getMapParam(Expression expression,
}

protected Map<String, String> getMapParam(Expression expression,
String paramName, Map<String, String> defaultValue)
String paramName, Map<String, String> defaultValue)
throws Exception {
if (expression == null) {
// return default
Expand Down Expand Up @@ -480,4 +482,4 @@ private Map<String, String> getMapValue(Object value, String paramName)
}
}

}
}

0 comments on commit 964b78f

Please sign in to comment.