Getting Activiti to “Talk” to an External Database Koorosh Vakhshoori Software Architect/Senior developer at Synopsys Inc.
Getting Activiti to “Talk” to an External Database Koorosh Vakhshoori Software Architect/Senior developer at Synopsys Inc.
Synopsys Inc. Provider of best-in-class solutions enabling designers to accelerate the creation and verification of complex integrated circuits (IC) and system-on-chip (SoC) designs from concept to silicon.
Use Cases (Insert/Update) Reporting Logging Dashboard
Use Cases (Retrieve) Populating some form field Signal for action or NO action Photo from Flickr by carterse
Simple Article Review Workflow
Do Not Reinvent the Wheel! Alfresco framework Other open source components Photo from Flickr by Balaji.B
Components Alfresco framework alfresco-global.properties Spring framework Dependency injection JdbcTemplate Activiti Listener Service task
Components Alfresco framework alfresco-global.properties Spring framework Dependency injection JdbcTemplate Activiti Listener Service task
alfresco-global.properties ### database connection properties ### external_db.driver=org.gjt.mm.mysql.Driver external_db.username=activiti_app external_db.password=activiti_password external_db.name=ext_db external_db.url=jdbc:mysql://localhost:3306/ext_db?useUnicode=yes &characterEncoding=UTF-8
Dependency Injection - 1 <bean id=“extDataSource“ class="org.apache.commons.dbcp.BasicDataSource“ destroy-method="close"> <property name="driverClassName“ value="${external_db.driver}"/>
Dependency injection - 2 <bean id="AbstractUserTaskDelegate“ parent="baseJavaDelegate" abstract="true“ depends-on="activitiBeanRegistry"/> <bean id="LogActionClass" parent="AbstractUserTaskDelegate" class="com.example.activiti.workflow.LogActionListener">
Listener Code package com.example.workflow.listener; … public class LogTaskCommentListener extends BaseJavaDelegate implements TaskListener { public void notify(DelegateTask task) { … String query = "INSERT INTO article_actions (user, article_id, action) VALUES (?, ?, ?)"; jdbcTemplate.update(query, new Object[] { userName, articleId, action }); } public void setExtDBSource(DataSource extDBSource) { this.jdbcTemplate = new JdbcTemplate(extDBSource); }
Listener Code package com.example.workflow.listener; … public class LogTaskCommentListener extends BaseJavaDelegate implements TaskListener { public void notify(DelegateTask task) { … String query = "INSERT INTO article_actions (user, article_id, action) VALUES (?, ?, ?)"; jdbcTemplate.update(query, new Object[] { userName, articleId, action }); } public void setExtDBSource(DataSource extDBSource) { this.jdbcTemplate = new JdbcTemplate(extDBSource); }
Watch Out Database configuration Connection time out Connection pool size Consider corner cases Database not reachable
Unit Testing Challenges Review Alfresco source code AbstractActivitiComponentTest.java
Unit Testing Components Junit framework Mock framework (mockito) Spring framework SpringJUnit4ClassRunner JdbcTemplate H2 database engine
Unit Testing Components Junit framework Mock framework (mockito) Spring framework SpringJUnit4ClassRunner JdbcTemplate H2 database engine
Test Execution Flow Initialize H2 schema Record behavior (mock) Run test scenario Validate database table Drop table and cleanup
Test Execution Flow Initialize H2 schema Record behavior (mock) Run test scenario Validate database table Drop table and cleanup
Resources Blog Post on How to Implement o-activiti-talking-to-external.html