svn commit: r1374618 - in /ofbiz/trunk/framework/service/src/org/ofbiz/service/job: AbstractJob.java GenericServiceJob.java Job.java JobInvoker.java JobPoller.java PersistedServiceJob.java PurgeJob.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1374618 - in /ofbiz/trunk/framework/service/src/org/ofbiz/service/job: AbstractJob.java GenericServiceJob.java Job.java JobInvoker.java JobPoller.java PersistedServiceJob.java PurgeJob.java

adrianc
Author: adrianc
Date: Sat Aug 18 17:43:03 2012
New Revision: 1374618

URL: http://svn.apache.org/viewvc?rev=1374618&view=rev
Log:
Moved JobInvoker.java code to AbstractJob.java, removed JobInvoker.java. Also worked on the Job API - the meaning of "runtime" was a little muddled, so I made "runtime" mean job execution elapsed time, and added a getStartTime method to the Job interface. This should make the distinction clearer.

This commit completes the Job Scheduler overhaul.

Removed:
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobInvoker.java
Modified:
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/AbstractJob.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/GenericServiceJob.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/Job.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java
    ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/AbstractJob.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/AbstractJob.java?rev=1374618&r1=1374617&r2=1374618&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/AbstractJob.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/AbstractJob.java Sat Aug 18 17:43:03 2012
@@ -18,16 +18,25 @@
  *******************************************************************************/
 package org.ofbiz.service.job;
 
+import java.util.Date;
+
 import org.ofbiz.base.util.Assert;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.entity.transaction.GenericTransactionException;
+import org.ofbiz.entity.transaction.TransactionUtil;
 
 /**
  * Abstract Job.
  */
 public abstract class AbstractJob implements Job {
 
+    public static final String module = AbstractJob.class.getName();
+
     private final String jobId;
     private final String jobName;
     protected State currentState = State.CREATED;
+    private long elapsedTime = 0;
+    private final Date startTime = new Date();
 
     protected AbstractJob(String jobId, String jobName) {
         Assert.notNull("jobId", jobId, "jobName", jobName);
@@ -65,4 +74,45 @@ public abstract class AbstractJob implem
         }
         this.currentState = State.CREATED;
     }
+
+    /**
+     *  Executes this Job. The {@link #run()} method calls this method.
+     */
+    public abstract void exec() throws InvalidJobException;
+
+    @Override
+    public void run() {
+        long startMillis = System.currentTimeMillis();
+        try {
+            exec();
+        } catch (InvalidJobException e) {
+            Debug.logWarning(e.getMessage(), module);
+        }
+        // sanity check; make sure we don't have any transactions in place
+        try {
+            // roll back current TX first
+            if (TransactionUtil.isTransactionInPlace()) {
+                Debug.logWarning("*** NOTICE: JobInvoker finished w/ a transaction in place! Rolling back.", module);
+                TransactionUtil.rollback();
+            }
+            // now resume/rollback any suspended txs
+            if (TransactionUtil.suspendedTransactionsHeld()) {
+                int suspended = TransactionUtil.cleanSuspendedTransactions();
+                Debug.logWarning("Resumed/Rolled Back [" + suspended + "] transactions.", module);
+            }
+        } catch (GenericTransactionException e) {
+            Debug.logWarning(e, module);
+        }
+        elapsedTime = System.currentTimeMillis() - startMillis;
+    }
+
+    @Override
+    public long getRuntime() {
+        return elapsedTime;
+    }
+
+    @Override
+    public Date getStartTime() {
+        return startTime;
+    }
 }

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/GenericServiceJob.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/GenericServiceJob.java?rev=1374618&r1=1374617&r2=1374618&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/GenericServiceJob.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/GenericServiceJob.java Sat Aug 18 17:43:03 2012
@@ -29,7 +29,7 @@ import org.ofbiz.service.LocalDispatcher
 import org.ofbiz.service.ServiceUtil;
 
 /**
- * Generic Service Job - A generic async-service Job.
+ * A generic async-service job.
  */
 @SuppressWarnings("serial")
 public class GenericServiceJob extends AbstractJob implements Serializable {
@@ -40,7 +40,6 @@ public class GenericServiceJob extends A
     protected final transient DispatchContext dctx;
     private final String service;
     private final Map<String, Object> context;
-    protected long runtime = System.currentTimeMillis();
 
     public GenericServiceJob(DispatchContext dctx, String jobId, String jobName, String service, Map<String, Object> context, GenericRequester req) {
         super(jobId, jobName);
@@ -131,16 +130,11 @@ public class GenericServiceJob extends A
      * Gets the name of the service as defined in the definition file.
      * @return The name of the service to be invoked.
      */
-    protected String getServiceName() throws InvalidJobException {
+    protected String getServiceName() {
         return service;
     }
 
     @Override
-    public long getRuntime() {
-        return runtime;
-    }
-
-    @Override
     public boolean isValid() {
         return currentState == State.CREATED;
     }

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/Job.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/Job.java?rev=1374618&r1=1374617&r2=1374618&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/Job.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/Job.java Sat Aug 18 17:43:03 2012
@@ -18,6 +18,8 @@
  *******************************************************************************/
 package org.ofbiz.service.job;
 
+import java.util.Date;
+
 /**
  * A scheduled job.
  * <p>A job starts out in the created state. When the job is queued for execution, it
@@ -25,7 +27,7 @@ package org.ofbiz.service.job;
  * When the job execution ends, it transitions to the finished or failed state - depending
  * on the outcome of the task that was performed.</p>
  */
-public interface Job {
+public interface Job extends Runnable {
 
     public static enum State {CREATED, QUEUED, RUNNING, FINISHED, FAILED};
 
@@ -35,11 +37,6 @@ public interface Job {
     State currentState();
 
     /**
-     *  Executes this Job.
-     */
-    void exec() throws InvalidJobException;
-
-    /**
      * Returns the ID of this Job.
      */
     String getJobId();
@@ -50,7 +47,8 @@ public interface Job {
     String getJobName();
 
     /**
-     *  Returns the time to run in milliseconds.
+     *  Returns the job execution time in milliseconds.
+     *  Returns zero if the job has not run.
      */
     long getRuntime();
 
@@ -69,5 +67,10 @@ public interface Job {
      * Transitions this job to the queued state.
      */
     void queue() throws InvalidJobException;
+
+    /**
+     * Returns the time this job is scheduled to start.
+     */
+    Date getStartTime();
 }
 

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java?rev=1374618&r1=1374617&r2=1374618&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java Sat Aug 18 17:43:03 2012
@@ -177,13 +177,17 @@ public final class JobPoller {
         List<Map<String, Object>> taskList = new ArrayList<Map<String, Object>>();
         Map<String, Object> taskInfo = null;
         for (Runnable task : queue) {
-            JobInvoker jobInvoker = (JobInvoker) task;
+            Job job = (Job) task;
             taskInfo = new HashMap<String, Object>();
-            taskInfo.put("id", jobInvoker.getJobId());
-            taskInfo.put("name", jobInvoker.getJobName());
-            taskInfo.put("serviceName", jobInvoker.getServiceName());
-            taskInfo.put("time", jobInvoker.getTime());
-            taskInfo.put("runtime", jobInvoker.getCurrentRuntime());
+            taskInfo.put("id", job.getJobId());
+            taskInfo.put("name", job.getJobName());
+            String serviceName = "";
+            if (job instanceof GenericServiceJob) {
+                serviceName = ((GenericServiceJob) job).getServiceName();
+            }
+            taskInfo.put("serviceName", serviceName);
+            taskInfo.put("time", job.getStartTime());
+            taskInfo.put("runtime", job.getRuntime());
             taskList.add(taskInfo);
         }
         poolState.put("taskList", taskList);
@@ -203,7 +207,7 @@ public final class JobPoller {
     public void queueNow(Job job) throws InvalidJobException {
         job.queue();
         try {
-            executor.execute(new JobInvoker(job));
+            executor.execute(job);
         } catch (Exception e) {
             job.deQueue();
         }

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java?rev=1374618&r1=1374617&r2=1374618&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PersistedServiceJob.java Sat Aug 18 17:43:03 2012
@@ -67,6 +67,7 @@ public class PersistedServiceJob extends
     private final long maxRetry;
     private final long currentRetryCount;
     private final GenericValue jobValue;
+    private final long startTime;
 
     /**
      * Creates a new PersistedServiceJob
@@ -79,7 +80,7 @@ public class PersistedServiceJob extends
         this.delegator = dctx.getDelegator();
         this.jobValue = jobValue;
         Timestamp storedDate = jobValue.getTimestamp("runTime");
-        this.runtime = storedDate.getTime();
+        this.startTime = storedDate.getTime();
         this.maxRetry = jobValue.get("maxRetry") != null ? jobValue.getLong("maxRetry").longValue() : -1;
         Long retryCount = jobValue.getLong("currentRetryCount");
         if (retryCount != null) {
@@ -189,7 +190,7 @@ public class PersistedServiceJob extends
 
     private void createRecurrence(long next, boolean isRetryOnFailure) throws GenericEntityException {
         if (Debug.verboseOn()) Debug.logVerbose("Next runtime returned: " + next, module);
-        if (next > runtime) {
+        if (next > startTime) {
             String pJobId = jobValue.getString("parentJobId");
             if (pJobId == null) {
                 pJobId = jobValue.getString("jobId");
@@ -267,7 +268,7 @@ public class PersistedServiceJob extends
     }
 
     @Override
-    protected String getServiceName() throws InvalidJobException {
+    protected String getServiceName() {
         if (jobValue == null || jobValue.get("serviceName") == null) {
             return null;
         }

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java?rev=1374618&r1=1374617&r2=1374618&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/PurgeJob.java Sat Aug 18 17:43:03 2012
@@ -72,11 +72,6 @@ public class PurgeJob extends AbstractJo
     }
 
     @Override
-    public long getRuntime() {
-        return System.currentTimeMillis();
-    }
-
-    @Override
     public boolean isValid() {
         return currentState == State.CREATED;
     }