From fe430891acdf390ae9b12fba9dd5c3135245f1c5 Mon Sep 17 00:00:00 2001 From: ymlam Date: Fri, 16 Jul 2021 01:00:05 -0400 Subject: [PATCH] Give derived class chance to wait for service activity to end before progressing to COMPLETED state and issue state change notification to portal. Also make state change report to be a single line for ease of grep'ing log file. --- src/main/java/altk/comm/engine/Broadcast.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main/java/altk/comm/engine/Broadcast.java b/src/main/java/altk/comm/engine/Broadcast.java index 4bd304a..9c74624 100644 --- a/src/main/java/altk/comm/engine/Broadcast.java +++ b/src/main/java/altk/comm/engine/Broadcast.java @@ -829,16 +829,15 @@ public abstract class Broadcast + "' recipient_count='" + recipientList.size() + "'"); if (launchRecordId != null) { - statusBf.append(" launch_record_id='" + launchRecordId + "'"); + statusBf.append(" launch_record_id='" + launchRecordId + "'>"); } - statusBf.append(">\r\n"); - statusBf.append("" + System.currentTimeMillis() + "\r\n"); + statusBf.append("" + System.currentTimeMillis() + ""); if (serviceStartTime > 0) statusBf.append("" + serviceStartTime - + "\r\n"); + + ""); if (serviceEndTime > 0) statusBf.append("" + serviceEndTime - + "\r\n"); + + ""); statusBf.append("" + state + "" + changeStateTime - + "\r\n"); + + ""); if (state == BroadcastState.PAUSED || state == BroadcastState.ABORTED) { @@ -846,7 +845,7 @@ public abstract class Broadcast { // Escaping '&' and '<' in haltReason before enclosing it in tag statusBf.append("" + Util.xmlEscape(haltReason) - + "\r\n"); + + ""); } if (stateErrorText != null) { @@ -860,8 +859,8 @@ public abstract class Broadcast "' ready='" + getPendingJobCount() + "'"); statusBf.append(" active='" + getActiveJobCount() + "'"); statusBf.append(">"); - statusBf.append("\r\n" + additionalStatusXML() + "\r\n"); - statusBf.append("\r\n"); + statusBf.append(additionalStatusXML()); + statusBf.append(""); String statusReport = statusBf.toString(); return statusReport; } @@ -1030,15 +1029,23 @@ public abstract class Broadcast myLogger.error("Caught exception while waiting for a Service thread to terminate:" + e); } } + waitForEndOfService(); setState(BroadcastState.COMPLETED); } destroyResources(); postback.wrapup(); postback = null; - //setState(BroadcastState.ALLDONE); myLogger.info("Broadcast " + getId() + " terminated"); } + /** + * Derived class should wait for end of service before returning. + * At this point all service threads have already ended. If the derived + * class has other threads still taking part in providing service, wait for + * them to terminate. + */ + protected void waitForEndOfService() {} + /** * Derived class destroy resources needed for providing service */ @@ -1174,13 +1181,12 @@ public abstract class Broadcast */ protected void postJobStatus(Job job, long rescheduleTimeMS) { - //logJobCount("Entering postJobStatus"); + if (job.jobStatus == JobStatus.SUCCESS) successCount++; if (postback != null) { JobReport report = mkJobReport(); report.initBase(job, broadcastId, launchRecordId, activityRecordIdParamName, jobReportRootNodeName); report.init(job); - if (job.jobStatus == JobStatus.SUCCESS) successCount++; postback.queueReport(report.toString()); } @@ -1299,17 +1305,9 @@ public abstract class Broadcast public PostbackThreadActionOnEmpty getPostbackThreadActionOnEmpty() { //logJobCount("getPostbackThreadActionOnEmpty"); - if (state==BroadcastState.CANCELED || state==BroadcastState.ALLDONE) return PostbackThreadActionOnEmpty.STOP; + if (state.isFinal) return PostbackThreadActionOnEmpty.STOP; if (setState(BroadcastState.ALLDONE).stateChangeStatus == StateChangeStatus.SUCCESS) return PostbackThreadActionOnEmpty.CONTINUE; return PostbackThreadActionOnEmpty.WAIT; } - - public boolean postbackThreadsShouldStop() { - if (state==BroadcastState.CANCELED || state==BroadcastState.ALLDONE) return true; - boolean shouldContinue = state==BroadcastState.COMPLETED && setState(BroadcastState.ALLDONE).stateChangeStatus == StateChangeStatus.SUCCESS; - return !shouldContinue; - //boolean doStop = setState(BroadcastState.ALLDONE).stateChangeStatus != StateChangeStatus.SUCCESS; - //return doStop; - } - + }