Author: camunda
Date: 2009-02-27 07:25:51 -0500 (Fri, 27 Feb 2009)
New Revision: 4062
Added:
jbpm3/trunk/modules/core/src/test/java/org/jbpm/db/JobSessionDbTest.java
Modified:
jbpm3/trunk/modules/core/src/main/java/org/jbpm/db/JobSession.java
Log:
JBPM-2042. Added implementation again (with hibernate criterias, not named queries)
Modified: jbpm3/trunk/modules/core/src/main/java/org/jbpm/db/JobSession.java
===================================================================
--- jbpm3/trunk/modules/core/src/main/java/org/jbpm/db/JobSession.java 2009-02-27 11:55:39
UTC (rev 4061)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/db/JobSession.java 2009-02-27 12:25:51
UTC (rev 4062)
@@ -28,6 +28,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
@@ -110,6 +111,21 @@
+ monitoredJobs, e);
}
}
+
+ /**
+ * get all failed jobs. Failed job have a retry count
+ * of 0 and the occured exception set.
+ */
+ public List<Job> findFailedJobs() {
+ try {
+ List jobs = session.createCriteria(Job.class)
+ .add(Restrictions.eq("retries", 0))
+ .add(Restrictions.isNotNull("exception")).list();
+ return CollectionUtil.checkList(jobs, Job.class);
+ } catch (Exception e) {
+ throw new JbpmException("couldn't find failed jobs", e);
+ }
+ }
public void saveJob(Job job) {
try {
Added: jbpm3/trunk/modules/core/src/test/java/org/jbpm/db/JobSessionDbTest.java
===================================================================
--- jbpm3/trunk/modules/core/src/test/java/org/jbpm/db/JobSessionDbTest.java
(rev 0)
+++ jbpm3/trunk/modules/core/src/test/java/org/jbpm/db/JobSessionDbTest.java 2009-02-27
12:25:51 UTC (rev 4062)
@@ -0,0 +1,112 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jbpm.db;
+
+import java.util.List;
+
+import org.jbpm.graph.def.ActionHandler;
+import org.jbpm.graph.def.ProcessDefinition;
+import org.jbpm.graph.exe.ExecutionContext;
+import org.jbpm.graph.exe.ProcessInstance;
+import org.jbpm.job.ExecuteNodeJob;
+import org.jbpm.job.Job;
+
+public class JobSessionDbTest extends AbstractDbTestCase {
+
+ public static final int timeout = 60000;
+
+ public static class FailingAction implements ActionHandler {
+ private static final long serialVersionUID = 1L;
+ public void execute(ExecutionContext executionContext) throws Exception
+ {
+ throw new RuntimeException("TEST-EXCEPTION");
+ }
+ }
+
+ /**
+ * Test case which generates a {@link Job} ({@link ExecuteNodeJob} via async=true)
+ * which causes an excpetion. Afterwards it is checked if this job
+ * is found by the getFailedJobs method
+ */
+ public void testLoadFailedJobs() throws Exception {
+ String xml =
+ "<process-definition name='TestJob'>"
+ + " <start-state>"
+ + " <transition to='async state' />"
+ + " </start-state>"
+ + " <node name='async state' async='true'>"
+ + " <transition to='end'>"
+ + " <action name='throw exception'
class='org.jbpm.db.JobSessionDbTest$FailingAction' />"
+ + " </transition>"
+ + " </node>"
+ + " <end-state name='end' />"
+ + "</process-definition>";
+
+ // create a process definition
+ ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(xml);
+ // save it in the database
+ graphSession.saveProcessDefinition(processDefinition);
+
+ newTransaction();
+
+ try {
+ // start a new process instance and signal it
+ ProcessInstance pi = jbpmContext.newProcessInstance("TestJob");
+ pi.getRootToken().signal();
+
+ newTransaction();
+
+ // job is created now, but not yet executed
+ assertEquals(1, getNbrOfJobsAvailable());
+ assertEquals(0, jobSession.findFailedJobs().size());
+
+ // start job executor wait for job to be executed
+ // and failure is written to database
+ startJobExecutor();
+ long startTime = System.currentTimeMillis();
+ while( jobSession.findFailedJobs().size() <= 0 ) {
+ if (System.currentTimeMillis() - startTime > timeout) {
+ fail("test execution exceeded treshold of " + timeout + "
milliseconds");
+ }
+ Thread.sleep(500);
+ }
+ stopJobExecutor();
+ List<Job> failedJobs = jobSession.findFailedJobs();
+
+ // now the one job we have should be failed
+ assertEquals(1, getNbrOfJobsAvailable());
+ assertEquals(1, failedJobs.size());
+
+ // and information is set on the job
+ assertEquals(0, failedJobs.get(0).getRetries());
+ assertNotNull(failedJobs.get(0).getException() + "==null",
+ failedJobs.get(0).getException());
+ assertTrue(failedJobs.get(0).getException() + " contains TEST-EXCEPTION",
+ failedJobs.get(0).getException().indexOf("TEST-EXCEPTION")>0);
+ }
+ finally {
+ newTransaction();
+ // cleanup
+ jbpmContext.getGraphSession().deleteProcessDefinition(processDefinition.getId());
+ }
+ }
+}
\ No newline at end of file