Blogs on SpringSource and Latest in Java

April 29, 2012

Jumpstart Activiti BPM, Spring MVC and Maven

Filed under: Spring — Administrator @ 7:07 pm

This blog moved here. The source code for this can be found here.

Recently I had a opportunity to try Activiti, an opensource BPM. If you are from Java background, this is a good tool to help us learn concepts like BPM, Workflow, BPMN etc. It has very good Spring integration support. It support BPMN 2.0 standards. In this example, there are lots of JUnit testcases which runs out of the box with just maven, which can demo these concepts.

Just to give you some background, couple of years back, we had written a web application using Spring MVC for Supply Chain Automation, where the application flow was driven by the workflow rules defined outside of the application. While exploring this tool I also realized it is a perfect candidate for externalizing the user flows/rules similar to what we had written in Spring MVC. One of the other competing opensource tools is jBPM Drools.

BPM is a tool which can model a Business process to be used for 2 main purpose,

  • A Business process that can be triggered by a web application to do a order process, which can touch multiple application (A2A) to complete the order
  • Another most important feature of a Business process modeling is in interacting with an user to complete a set of approval process, in the form of usertasks

In this article I will emphasize on 2nd point, I will demonstrate a Loan Processing Application using Activiti, BPMN 2.0, Spring ROO MVC and Maven. This code base has 2 projects,

  • Activiti Junit testcases ported to Maven: This helps you to learn the features of Activity. To quickstart go to this folder and run mvn test, if you see all the tests pass, import to STSIDE and run in debug mode and learn how it works. The prerequisites for this are maven and STS IDE, it does not need Activiti installation, it will work with Activiti in memory engine.
  • Loan Processing Application using Activiti, Spring ROO MVC, Maven: This example is built on top of Roo. In this example I demonstrate a process where loan is submitted by a user, the manager can claim the loan approval task and approve the loan. Activiti and most of other BPM tool does not need you to write external web application to control the flow, the tool themselves support basic UI flow. I will also demo how we can build basic UI for submitting a document and someone verify it and approving it. The prerequisites for this is maven, STS IDE, Activiti, ant, Spring Roo

Details:

Let us download Activiti and unzip the installation. Let us run ant demo.start from <activiti install>/setup folder.

In approach 1, open a browser and type http://localhost:8080/activiti-explorer, login as kermit/kermit.
Deploy the bpmn.xml file located @ <svn location>/src/main/resources/org/activiti/spring/test/usertask1/LoanProcess1.bpmn20.xml . You will get user form in Activiti explorer where you can submit and approve the loan.

In approach 2, Deploy the bpmn.xml file located @ <svn location>/src/main/resources/org/activiti/spring/test/usertask/LoanProcess.bpmn20.xml . I have built a Roo based Spring MVC application where I did the “Push In” to generate the controller code LoanRequestController and added the below code,


private String startProcess(LoanRequest loanRequest, HttpServletRequest request) {
if (request.isUserInRole(submitterRole)) {
logger.debug("in the startProcess ");
// Get Activiti services
// Create Activiti process engine
ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration().buildProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
taskService = processEngine.getTaskService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("loanProcess");
logger.debug("startProcess processInstance Id=" + processInstance.getId());
claimAndComplete(processInstance.getId());
}
return "";
}
private void approveProcess(LoanRequest loanRequest, HttpServletRequest request) {
// TODO Auto-generated method stub
logger.debug("in the approveProcess ");
if (!loanRequest.getProcessId().isEmpty() && request.isUserInRole(approverRole)) {
claimAndComplete(loanRequest.getProcessId());
}
}

Run the application by calling mvn -Dmaven.tomcat.port=8181 clean tomcat:run @ /loanrequest folder. To test, login as kermit/kermit and create a loan request and submit. Now login as gonzo/gonzo and notice in activiti explorer the new process was started with new task “Verify loan request”. Now you go back to you application and login in as gonzo/gonzo and open the loan request and update it. It will get approved..

To learn more, run mvn test and understand how the process flow works. The spring configuration is as below,

<bean id="dataSource-activiti" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost/activiti" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource-activiti" />
<property name="databaseSchemaUpdate" value="true" />
<property name="transactionManager" ref="transactionManager" />
<property name="jpaHandleTransaction" value="false" />
<property name="jpaCloseEntityManager" value="false" />
<property name="jobExecutorActivate" value="false" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource-activiti" />
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

Quick look at the pom file will have below 3 additional dependencies,

<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.9</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>5.9</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.2.132</version>
</dependency>

April 23, 2012

Jumpstart Grails with real life example: Agile Tracking Tool

Filed under: Spring — Administrator @ 2:29 pm

I moved this article here.

Recently I wanted to tryout Grails. I was trying out lot of different sample/examples/opensource projects. With the new Grails 2.0.3 upgrades all these applications are not working out of the box including Grails sample. Finally I narrowed down to on opensource project - Agile Tracking Tool. Within an hr or so I was able to make this work on Grails 2.0.3. It clearly demonstrates aspects like,

  • Spring Security
  • Graphs and Charts
  • Ajax using prototype framework

I am also contributing to this code base, by making it working with Latest Grails and I am currently fixing Authentication/Authorization with roles like, admin, scrummaster, developer etc..

To quickly jump start setting up the environment,

  • Download the latest grails @ http://www.grails.org/download/file?mirror=212
  • This assumes that you have Java 6 installed on your machine, and add <grails installation folder>/bin folder to the path
  • Get the latest code @ https://agiletrackingtool.googlecode.com/svn/trunk/ using tortoise svn
  • open the command prompt and go to the folder you just now got the code and run grails run-app
  • Open the browser and type in http://localhost:8080/AgileTrackingTool/
  • Login in as scrummaster/scrummaster and load the sample data by clicking the link “Load example project data”, also notice you dont have admin link
  • Once you load the data, you can see some burndown charts etc..
  • Login in as admin/admin, you get the admin link

Next step is to import this project in STS IDE as a Grails project and analysing how charts work, how ajax work etc…

April 18, 2012

Jumpstart with Groovy to do a simple XMLSlurper for xml parsing using GMaven

Filed under: Spring — Administrator @ 9:35 pm

As I am from Java world, recently I had an opportunity to play with Groovy and I realized that the simplest way to jump start learning Groovy is to use a maven archetype from GMaven.

I have put a working code of the example I am going to talk below. Download the code from here. To test, unzip the code and run mvn test.

I assume you have, maven 3.x installed on your machine. If you want to write this code from scratch, as a first step, you can call this command,

mvn archetype:generate -DarchetypeGroupId=org.codehaus.gmaven.archetypes -DarchetypeArtifactId=gmaven-archetype-basic -DarchetypeVersion=<version></version>

It will create a maven groovy project. You can also import the same in a eclipse, it will not recognize this as a Groovy project. If you import this in STS IDE, with little configuration to STS it will recognize this as a Groovy project.

Once it is done, under src/test/groovy you can starting unit tests to test your code, and start running,

mvn test

If you want to test your code directly, you can use the below command,

mvn groovy:execute

GMaven also has commands like

mvn groovy:shell

mvn groovy:console

Try executing and see what happens.

In this article I will demonstrate how to write a simple XMLSlurper, let us start writing a test as below,


package com.mycompany
import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory
/**
* Tests for the {@link Example} class.
*/
class XmlManagerTest extends GroovyTestCase
{
def processXml( String xpathQuery ) {
def xpath = XPathFactory.newInstance().newXPath()
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def inputStream = new FileInputStream( "pom-test.xml")
def records = builder.parse(inputStream).documentElement
def nodes = xpath.evaluate( xpathQuery, records, XPathConstants.NODESET )
assert nodes.size < 0
nodes.collect { node -< println(node) }
}
void testJavaXpath(){
processXml( "/project/dependencies/dependency/groupId['org.springframework']" )
}
}

In another code snippet, I have used XMLSlurper as below,

public void analyseFile(def xmlFile){
def project = new XmlSlurper().parse(xmlFile)
def orgSpringframeworkDeps = project.dependencies.dependency.findAll { dependency -> dependency.groupId == 'org.springframework' }
assert 0 < orgSpringframeworkDeps.size()
orgSpringframeworkDeps.each { orgSpringframeworkDep ->
println “***********” + orgSpringframeworkDep[’artifactId’] + “***********” ;
}
}

Next Page »

Powered by WordPress