Skip to main content

Using Activiti with spring mvc

Follow below steps to create web application using spring mvc and activiti in background

steps :1
create maven project with following pom


 4.0.0
 com.gk
 activitiMakerChecker
 war
 0.0.1-SNAPSHOT
 activitiMakerChecker Maven Webapp
 http://maven.apache.org
 
  4.2.4.RELEASE
  1.6
  4.0.5.RELEASE
  1.2
  3.1.0
  4.0.1.Final
  3.5.6-Final
 
  
  
   org.activiti
   activiti-engine
   5.14
  
  
   org.activiti
   activiti-spring
   5.14
  
  
   org.springframework
   spring-beans
   ${org.springframework.version}
  
  
   org.springframework
   spring-context
   4.2.4.RELEASE
  
  
   org.springframework
   spring-jdbc
   4.2.4.RELEASE
  
  
   org.springframework
   spring-tx
   4.2.4.RELEASE
  
  
   org.springframework
   spring-orm
   ${org.springframework.version}
  
  
   org.slf4j
   slf4j-api
   1.7.2
   
  
   org.hibernate
   hibernate-core
   ${org.hibernate.version}
  
  
   org.hibernate
   hibernate-entitymanager
   ${org.hibernate.version}
  
  
   org.hibernate
   hibernate-annotations
   ${org.hibernate.annotation.version}
   
  
   com.oracle
   ojdbc6
   11.2.0.3
  
  
   org.springframework
   spring-webmvc
   ${spring.version}
  
  
   javax.servlet
   jstl
   ${jstl.version}
  
  
   junit
   junit
   3.8.1
   test
  
 
 
  activitiMakerChecker
 
 
Step 2 create web.xml

  Archetype Created Web Application
    
    hello
    org.springframework.web.servlet.DispatcherServlet
  
  
    hello
    /
  


hello-servlet.xml

 
 

 
  
  
  

 
 
  
 


 

  
  
  
  

 

 
  
 
 
 
 
 
 

 
  
  
 




Step 3: java classes MyProcess
 
package com.nuc.process;
 
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
 
@Controller
public class MyProcess {
 
 @Autowired
 ProcessEngine processEngine;
 @RequestMapping(value="hello")
 public String sayHello() {
  System.out.println("Controlle is here");
  if(processEngine==null)
  {
   System.out.println("Nulla");
  }
  else
  {
   System.out.println("Not null");
  }
   
   
  return "welcome";
 }
}
MyController
package com.gk.controller;
 
package com.gk.controller;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpSession;
 
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
import com.gk.dao.Dao;
import com.gk.dao.User;
import com.nuc.service.UserTaskList;
 
 
@Controller
public class MyController {
 
 @Autowired
 @Qualifier("dao")
 Dao dao;
  
 @Autowired
 private ProcessEngine processEngine;
 @RequestMapping(value=("login"),method=RequestMethod.POST)
 public String login(@RequestParam("name")String name,@RequestParam("pass")String pass,@RequestParam("gName")String gName,HttpSession session
   ) {
   
  User u1=new User();
  u1.setName(name);
  u1.setgName(gName);
  u1.setPass(pass);
//  dao.save(u1);
 // return "welcome";
  
  String vl="rtl";
  String js="/resources/js/rtl.js";
  if(vl.equalsIgnoreCase("rtl"))
  {
    js="/resources/js/rtl.js";
  }
  session.setAttribute("js", js);
 
  
 //  <link css="" href="<c:url value=" resources="" rtl.css="">" rel="stylesheet">
  //    <script src="<c:url value=${js} />"></script>
 
  User u2=dao.findById(name);
  if(u2!=null)
  {
   if(u1.getPass().equals(u2.getPass())&&u1.getgName().equals(u2.getgName()))
   {
     
    session.setAttribute("user", u1);
    return "welcome";
   }
   else
   {
    return "error";
   }
  }
  else
  {
   return "error";
  }
   
   
 }
  
 @RequestMapping(value="startProcess",method=RequestMethod.GET)
 public String startProcess(HttpSession session) {
   
  System.out.println("starting new process");
   
  Map<string object="">variables=new HashMap<string object="">();
  variables.put("name", "true");
  processEngine.getRepositoryService().createDeployment().addClasspathResource("myProcess.bpmn20.xml").deploy();
  processEngine.getRuntimeService().startProcessInstanceByKey("myProcess",variables);
  User user=(User)session.getAttribute("user");
 
  //processEngine.getRuntimeService().setVariablesLocal("vars", variables);
 // processEngine.getRuntimeService().setVariables("var", variables);
  session.setAttribute("processEngine", processEngine);
  return "myTask";
   
 }
 @RequestMapping(value="myTasks",method=RequestMethod.GET)
 public String myTasks(HttpSession session) {
  
 //System.out.println("starting new process");
   
  User user=(User)session.getAttribute("user");
  session.setAttribute("processEngine", processEngine);
   
  return "myTask";
   
 }
  
 @RequestMapping(value="claim",method=RequestMethod.GET)
 public String claim(@RequestParam("id")String id,HttpSession session) {
   
  User user=(User)session.getAttribute("user");
  processEngine.getTaskService().claim(id,user.getName());
   
  return "myTask";
 }
  
 @RequestMapping(value="complete",method=RequestMethod.GET)
 public String complete(@RequestParam("id")String id,HttpSession session) {
   
  User user=(User)session.getAttribute("user");
  processEngine.getTaskService().complete(id);
   
  return "myTask";
 }
 
}

below are jsps:
index.jsp:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    
<body>
<h2>Hello World! gk1</h2>
<center>
<br>
<form method="post" action="login">

<input type="text" name="name">
<br><br>
<input type="password" name="pass">
<br><br>
<select name="gName">
 <option>m</option>
 <option>c</option>
</select>
<br><br><br>
<input type="submit">
</form>
</center>
</body>
</html>

welcome.jsp:
<%@page import="com.gk.dao.User"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<%

String myvar=(String)session.getAttribute("js");
%>

 <link href="<c:url value="/resources/css/rtl.css" />" rel="stylesheet">
 <script src=<c:url value="<%=myvar %>" />></script>
</head>
<body>
Welcome <%
User user=(User)session.getAttribute("user");
if(user!=null)
{
 out.print(user.getName());
}

%>

<%= session.getAttribute("js") %>
<c:catch>


</c:catch>
<a href="startProcess">Start Process</a>
<a href="myTasks">MyTasks</a>
</body>
</html>

mytask.jsp:
<%@page import="org.activiti.engine.ProcessEngine"%>
<%@page import="org.activiti.engine.task.Task"%>
<%@page import="java.util.List"%>
<%@page import="com.gk.dao.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

your tasks


<%
 User user=(User)session.getAttribute("user");
 ProcessEngine processEngine=(ProcessEngine)session.getAttribute("processEngine");
 List<Task>tasks=processEngine.getTaskService().createTaskQuery().taskCandidateGroup(user.getgName()).list();
 out.print("<br>");
 out.print("Tasks in your group");
 for(Task t:tasks)
 {
  out.print("<br>Id:"+t.getId()+"Name:"+t.getName()+"<a href='claim?id="+t.getId()+"'>Claim it</a>");
 }
 out.print("<br>");
 out.print("Your tasks");
 List<Task>myTasks=processEngine.getTaskService().createTaskQuery().taskAssignee(user.getName()).list();
 for(Task t:myTasks)
 {
  out.print("<br>Id:"+t.getId()+"Name:"+t.getName()+"<a href='complete?id="+t.getId()+"'>Complete it</a>");
 }
 
%>
</body>
</html>


Comments

Popular posts from this blog

Communication among micro-services

Below are good articles to read on communication among micro-services. 1.  docs@microsoft.com 2.  https://dzone.com/articles/microservices-why-asynchronous-communications 3.  microservice-design-patterns 4.  https://vertx.io/docs/vertx-amqp-bridge/java/ 5.  developers.redhat.com/blog

Reactive programming

As per wikipedia reactive programming is declarative programming paradigm concerned with data streams and the propagation of change. Reactive programming is about having an architecture which has below four key components: Message driven  Scalable Resilient Responsive There lot of libraries/frameworks available which enable doing reactive programming across programming languages for ex in java we have RxJava, Project Reactor. Also spring 5 on wards spring framework also supports reactive programming.