Author: camunda
Date: 2009-02-16 08:28:25 -0500 (Mon, 16 Feb 2009)
New Revision: 3882
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
jbpm3/trunk/modules/core/src/main/resources/org/jbpm/db/hibernate.queries.hbm.xml
Log:
JBPM-2042: added method findFailedJobs to JobSession
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-16 10:49:48
UTC (rev 3881)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/db/JobSession.java 2009-02-16 13:28:25
UTC (rev 3882)
@@ -110,6 +110,19 @@
+ monitoredJobs, e);
}
}
+
+ /**
+ * get all failed jobs. Failed job have a retry count
+ * of 0 and the occured exception set.
+ */
+ public List<Job> findFailedJobs() {
+ try {
+ Query query = session.getNamedQuery("JobSession.findFailedJobs");
+ return CollectionUtil.checkList(query.list(), Job.class);
+ } catch (Exception e) {
+ throw new JbpmException("couldn't find failed jobs", e);
+ }
+ }
public void saveJob(Job job) {
try {
Modified:
jbpm3/trunk/modules/core/src/main/resources/org/jbpm/db/hibernate.queries.hbm.xml
===================================================================
---
jbpm3/trunk/modules/core/src/main/resources/org/jbpm/db/hibernate.queries.hbm.xml 2009-02-16
10:49:48 UTC (rev 3881)
+++
jbpm3/trunk/modules/core/src/main/resources/org/jbpm/db/hibernate.queries.hbm.xml 2009-02-16
13:28:25 UTC (rev 3882)
@@ -377,6 +377,14 @@
where job.token = :token
]]>
</query>
+
+ <query name="JobSession.findFailedJobs">
+ <![CDATA[
+ select job
+ from org.jbpm.job.Job as job
+ where job.retries=0 and job.exception is not null
+ ]]>
+ </query>
<query name="JobSession.findJobsWithOverdueLockTime">
<![CDATA[
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-16
13:28:25 UTC (rev 3882)
@@ -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.hibernate.StaleObjectStateException;
+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 {
+ 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());
+ }
+ }
+}