03-06-2013 05:26 PM
public class ThrowsException implements ActivityBehavior, Serializable
{
@Override
public void execute( ActivityExecution execution ) throws Exception
{
System.out.println( "in ThrowsException");
PvmTransition transition = null;
try {
if( execution.getVariable("clearException") == null )
{
System.out.println( "throwing Exception");
throw new Exception( "This is fucked!" );
}
System.out.println( "going no-exception path" );
transition = execution.getActivity().findOutgoingTransition("no-exception");
}
catch (Exception e)
{
System.out.println( "found Exception, going exception path");
transition = execution.getActivity().findOutgoingTransition("exception");
}
execution.take(transition);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://activiti.org/bpmn20">
<process id="financialReportWithJavaException2" name="Monthly financial report process">
<startEvent id="theStart"></startEvent>
<scriptTask id="scripttask1" name="Script Task" activiti:async="true" activiti:exclusive="false" scriptFormat="groovy">
<script><![CDATA[System.out.println( "in Script Task" );]]></script>
</scriptTask>
<serviceTask id="servicetask1" name="Service Task" activiti:class="org.example.activiti.services.ThrowsException"></serviceTask>
<scriptTask id="scripttask2" name="Script Task" scriptFormat="groovy">
<script><![CDATA[println( "after service task successful");]]></script>
</scriptTask>
<userTask id="usertask1" name="Fix Exception" activiti:candidateGroups="engineering">
<extensionElements>
<activiti:taskListener event="create" class="org.example.activiti.listener.ClearExceptionTaskListener"></activiti:taskListener>
</extensionElements>
</userTask>
<endEvent id="endevent1" name="End"></endEvent>
<scriptTask id="scripttask3" name="Script Task" scriptFormat="groovy">
<script><![CDATA[println( "after service task threw Exception");]]></script>
</scriptTask>
<sequenceFlow id="flow1" name="" sourceRef="theStart" targetRef="scripttask1"></sequenceFlow>
<sequenceFlow id="flow2" name="" sourceRef="scripttask1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="no-exception" name="No Exception" sourceRef="servicetask1" targetRef="scripttask2"></sequenceFlow>
<sequenceFlow id="exception" name="With Exception" sourceRef="servicetask1" targetRef="scripttask3"></sequenceFlow>
<sequenceFlow id="flow6" name="" sourceRef="scripttask2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow7" name="" sourceRef="scripttask3" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow8" name="" sourceRef="usertask1" targetRef="scripttask1"></sequenceFlow>
</process>
</definitions>
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://activiti.org/bpmn20">
<process id="financialReportWithJavaException2" name="Monthly financial report process">
<startEvent id="theStart"></startEvent>
<scriptTask id="scripttask1" name="Script Task" activiti:async="true" activiti:exclusive="false" scriptFormat="groovy">
<script><![CDATA[System.out.println( "in Script Task" );]]></script>
</scriptTask>
<serviceTask id="servicetask1" name="Service Task" activiti:expression="${handler.execute(execution)}"></serviceTask>
<scriptTask id="scripttask2" name="Script Task" scriptFormat="groovy">
<script><![CDATA[println( "after service task successful");]]></script>
</scriptTask>
<userTask id="usertask1" name="Fix Exception" activiti:candidateGroups="engineering">
<extensionElements>
<activiti:taskListener event="create" class="org.example.activiti.listener.ClearExceptionTaskListener"></activiti:taskListener>
</extensionElements>
</userTask>
<endEvent id="endevent1" name="End"></endEvent>
<scriptTask id="scripttask3" name="Script Task" scriptFormat="groovy">
<script><![CDATA[println( "after service task threw Exception");]]></script>
</scriptTask>
<sequenceFlow id="flow1" name="" sourceRef="theStart" targetRef="scripttask1"></sequenceFlow>
<sequenceFlow id="flow2" name="" sourceRef="scripttask1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="no-exception" name="No Exception" sourceRef="servicetask1" targetRef="scripttask2"></sequenceFlow>
<sequenceFlow id="exception" name="With Exception" sourceRef="servicetask1" targetRef="scripttask3"></sequenceFlow>
<sequenceFlow id="flow6" name="" sourceRef="scripttask2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow7" name="" sourceRef="scripttask3" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow8" name="" sourceRef="usertask1" targetRef="scripttask1"></sequenceFlow>
</process>
</definitions>
Started process with id: 301
done
in Script Task
in ThrowsException
throwing Exception
found Exception, going exception path
after service task threw Exception
Mar 6, 2013 4:04:57 PM org.activiti.engine.impl.bpmn.deployer.BpmnDeployer deploy
INFO: Processing resource FinancialReportProcessWithJavaException2.bpmn
Mar 6, 2013 4:04:58 PM org.activiti.engine.impl.bpmn.parser.BpmnParse parseDefinitionsAttributes
INFO: XMLSchema currently not supported as typeLanguage
Mar 6, 2013 4:04:58 PM org.activiti.engine.impl.bpmn.parser.BpmnParse parseDefinitionsAttributes
INFO: XPath currently not supported as expressionLanguage
Mar 6, 2013 4:04:58 PM org.activiti.engine.impl.bpmn.parser.BpmnParse parseProcessDefinitions
INFO: Process with id='financialReportWithJavaException2' hasn't the attribute isExecutable set. Please maintain it, so you are compatible to future activiti versions.
Mar 6, 2013 4:04:58 PM org.activiti.engine.impl.bpmn.deployer.BpmnDeployer deploy
INFO: Processing resource FinancialReportProcessWithJavaException2.financialReportWithJavaException2.png
in Script Task
in ThrowsException
going no-exception path
after service task successful
Mar 6, 2013 4:04:58 PM org.activiti.engine.impl.interceptor.CommandContext close
SEVERE: Error while closing command context
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
### The error may involve org.activiti.engine.impl.persistence.entity.ExecutionEntity.deleteExecution-Inline
### The error occurred while setting parameters
### SQL: delete from ACT_RU_EXECUTION where ID_ = ?
### Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:147)
at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:158)
at org.activiti.engine.impl.db.DbSqlSession$DeleteById.execute(DbSqlSession.java:146)
at org.activiti.engine.impl.db.DbSqlSession.flushDeletes(DbSqlSession.java:483)
at org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:371)
at org.activiti.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:157)
at org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:109)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:49)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.activiti.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:108)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:145)
… 12 more
Exception in thread "pool-1-thread-2" org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
### The error may involve org.activiti.engine.impl.persistence.entity.ExecutionEntity.deleteExecution-Inline
### The error occurred while setting parameters
### SQL: delete from ACT_RU_EXECUTION where ID_ = ?
### Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:147)
at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:158)
at org.activiti.engine.impl.db.DbSqlSession$DeleteById.execute(DbSqlSession.java:146)
at org.activiti.engine.impl.db.DbSqlSession.flushDeletes(DbSqlSession.java:483)
at org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:371)
at org.activiti.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:157)
at org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:109)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:49)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.activiti.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:108)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:145)
… 12 more
in Script Task
in ThrowsException
going no-exception path
after service task successful
Mar 6, 2013 4:09:59 PM org.activiti.engine.impl.interceptor.CommandContext close
SEVERE: Error while closing command context
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
### The error may involve org.activiti.engine.impl.persistence.entity.ExecutionEntity.deleteExecution-Inline
### The error occurred while setting parameters
### SQL: delete from ACT_RU_EXECUTION where ID_ = ?
### Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:147)
at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:158)
at org.activiti.engine.impl.db.DbSqlSession$DeleteById.execute(DbSqlSession.java:146)
at org.activiti.engine.impl.db.DbSqlSession.flushDeletes(DbSqlSession.java:483)
at org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:371)
at org.activiti.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:157)
at org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:109)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:49)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.activiti.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:108)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:145)
… 12 more
Exception in thread "pool-1-thread-4" org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
### The error may involve org.activiti.engine.impl.persistence.entity.ExecutionEntity.deleteExecution-Inline
### The error occurred while setting parameters
### SQL: delete from ACT_RU_EXECUTION where ID_ = ?
### Cause: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:147)
at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(DefaultSqlSession.java:158)
at org.activiti.engine.impl.db.DbSqlSession$DeleteById.execute(DbSqlSession.java:146)
at org.activiti.engine.impl.db.DbSqlSession.flushDeletes(DbSqlSession.java:483)
at org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:371)
at org.activiti.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:157)
at org.activiti.engine.impl.interceptor.CommandContext.close(CommandContext.java:109)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:49)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
at org.activiti.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "act_ru_execution" violates foreign key constraint "act_fk_task_exe" on table "act_ru_task"
Detail: Key (id_)=(101) is still referenced from table "act_ru_task".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:381)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:108)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:145)
… 12 more
03-07-2013 09:44 AM
03-07-2013 11:06 AM
Expression-bases service-tasks only delegate to the expression, presuming the expression WILL NOT alter execution state (apart form execution variables) and will take the default outgoing flow after the expression has been called. This is a big difference with implementing an ActivityBehaviour, which is in control of the process-flow. An ActivityBehaviour is for advanced use cases, while the regular service-task expression/class mechanism is for incorporating your java-logic in the process.
Why not use a gateway after the service-task, based on an execution-vcariable that is set when the service-task encounters an exception inside it. Even better, you can use throw a BPMNError from the JavaDelegate when your logic threw an exception. This error can be caught by an error-catching event somewhere else and do the "handle exception" logic for you, using pure BPMN.
03-08-2013 04:40 AM
03-08-2013 03:35 PM
If it's a logical error that is expected in your process, than you can just model in in with a gateway after the service-task. If it's an error that can occur due to "technical issue" while executing the service-task (not one that is expected regularly, like e.g.. order not found) an error-event is a better choice.
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.