From 5e05a4df42bc2ada86f5cf4b77e9fe7d489127ae Mon Sep 17 00:00:00 2001 From: Adrian Crum Date: Fri, 10 Aug 2012 07:50:37 +0000 Subject: [PATCH] JobManager and PersistedServiceJob improvements - based on feedback from Scott. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1371593 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/ofbiz/service/job/JobManager.java | 13 ++++++++----- .../ofbiz/service/job/PersistedServiceJob.java | 18 +++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/framework/service/src/org/ofbiz/service/job/JobManager.java b/framework/service/src/org/ofbiz/service/job/JobManager.java index c42ab8f3be..0ffb6fdee2 100644 --- a/framework/service/src/org/ofbiz/service/job/JobManager.java +++ b/framework/service/src/org/ofbiz/service/job/JobManager.java @@ -185,11 +185,14 @@ public final class JobManager { jobsIterator = delegator.find("JobSandbox", mainCondition, null, null, UtilMisc.toList("runTime"), null); GenericValue jobValue = jobsIterator.next(); while (jobValue != null) { - jobValue.set("runByInstanceId", instanceId); // Claim ownership of this value. - jobValue.store(); - poll.add(new PersistedServiceJob(dctx, jobValue, null)); - if (poll.size() == limit) { - break; + // Claim ownership of this value. Using storeByCondition to avoid a race condition. + List updateExpression = UtilMisc.toList(EntityCondition.makeCondition("jobId", EntityOperator.EQUALS, jobValue.get("jobId")), EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null)); + int rowsUpdated = delegator.storeByCondition("JobSandbox", UtilMisc.toMap("runByInstanceId", instanceId), EntityCondition.makeCondition(updateExpression)); + if (rowsUpdated == 1) { + poll.add(new PersistedServiceJob(dctx, jobValue, null)); + if (poll.size() == limit) { + break; + } } jobValue = jobsIterator.next(); } diff --git a/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java b/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java index 3cf231c2b1..630abf4685 100644 --- a/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java +++ b/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java @@ -31,7 +31,6 @@ import org.apache.commons.lang.StringUtils; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilGenerics; -import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; @@ -94,6 +93,14 @@ public class PersistedServiceJob extends GenericServiceJob { @Override public void queue() throws InvalidJobException { super.queue(); + try { + jobValue.refresh(); + } catch (GenericEntityException e) { + throw new InvalidJobException("Unable to refresh JobSandbox value", e); + } + if (!JobManager.instanceId.equals(jobValue.getString("runByInstanceId"))) { + throw new InvalidJobException("Job has been accepted by a different instance"); + } Timestamp cancelTime = jobValue.getTimestamp("cancelDateTime"); Timestamp startTime = jobValue.getTimestamp("startDateTime"); if (cancelTime != null || startTime != null) { @@ -116,20 +123,17 @@ public class PersistedServiceJob extends GenericServiceJob { protected void init() throws InvalidJobException { super.init(); try { - // Job might have been canceled after it was placed in the queue. jobValue.refresh(); } catch (GenericEntityException e) { throw new InvalidJobException("Unable to refresh JobSandbox value", e); } + if (!JobManager.instanceId.equals(jobValue.getString("runByInstanceId"))) { + throw new InvalidJobException("Job has been accepted by a different instance"); + } if (jobValue.getTimestamp("cancelDateTime") != null) { // Job cancelled throw new InvalidJobException("Job [" + getJobId() + "] was cancelled"); } - String instanceId = UtilProperties.getPropertyValue("general.properties", "unique.instanceId", "ofbiz0"); - if (!instanceId.equals(jobValue.getString("runByInstanceId"))) { - // This condition isn't possible, but we will leave it here. - throw new InvalidJobException("Job has been accepted by a different instance!"); - } jobValue.set("startDateTime", UtilDateTime.nowTimestamp()); jobValue.set("statusId", "SERVICE_RUNNING"); try {