一个JBPM5.2+Spring的例子

这个例子的源码是别人的,因为我没有成功运行,所以修改了一些地方,才能运行起来,故上传做备份。

更多的源码见附件

JbpmSupport.java
package com.gds.jbpm;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;

import org.drools.SystemEventListenerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.process.audit.JPAProcessInstanceDbLog;
import org.jbpm.process.audit.JPAWorkingMemoryDbLogger;
import org.jbpm.process.workitem.wsht.SyncWSHumanTaskHandler;
import org.jbpm.task.TaskService;
import org.jbpm.task.service.TaskServiceSession;
import org.jbpm.task.service.local.LocalTaskService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("jbpmSupport")
public class JbpmSupport {

	@Resource
	@Qualifier("entityManagerFactory")
	private EntityManagerFactory entityManagerFactory;

	@Resource
	@Qualifier("ksession")
	private StatefulKnowledgeSession ksession;

	private TaskServiceSession taskServiceSession;

	/**
	 * <dt>核心就是两个session:
	 * <dd>KnowledgeSession 可以完全通过spring drools配置成功
	 * <dd>TaskServiceSession 这里通过手动编码产生local service
	 * 
	 * <dt>todo 学习点:
	 * <dd>能否完全使用spring xml配置出两个session
	 * <dd>usergroup callback的作用和用法进一步研究
	 */
	@PostConstruct
	public void init() {
		System.out.println("初始化Service类[JbpmSupport]------------------------------------------------------------------------.");
		// 为 ksession 设置log
		new JPAWorkingMemoryDbLogger(ksession);
		new JPAProcessInstanceDbLog(ksession.getEnvironment());

		// 创建 local human service 及其 handler
		org.jbpm.task.service.TaskService tService = new org.jbpm.task.service.TaskService(entityManagerFactory,
				SystemEventListenerFactory.getSystemEventListener());
		taskServiceSession = tService.createSession();
		// TODO 事务该如何设置?
		// taskServiceSession.setTransactionType("RESOURCE_LOCAL");
		SyncWSHumanTaskHandler humanTaskHandler = new SyncWSHumanTaskHandler(new LocalTaskService(taskServiceSession),
				ksession);
		humanTaskHandler.setLocal(true);
		humanTaskHandler.connect();
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task", humanTaskHandler);

		// set user group callback
		System.setProperty("jbpm.usergroup.callback", "org.jbpm.task.service.DefaultUserGroupCallbackImpl");
	}

	public StatefulKnowledgeSession getKsession() {
		return ksession;
	}

	public TaskService getTaskService() {
		return new LocalTaskService(taskServiceSession);
	}

}



OrderController.java
package com.gds.web;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.WorkflowProcessInstance;
import org.jbpm.task.AccessType;
import org.jbpm.task.Task;
import org.jbpm.task.TaskData;
import org.jbpm.task.TaskService;
import org.jbpm.task.query.TaskSummary;
import org.jbpm.task.service.ContentData;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gds.jbpm.JbpmSupport;
import com.gds.jbpm.Order;

@Controller
public class OrderController {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource
	@Qualifier("jbpmSupport")
	private JbpmSupport jbpmSupport;

	@RequestMapping("/")
	public String test() {
		log.info("this is the index");
		return "index";
	}

	@RequestMapping(value = "{user}/create", method = GET)
	public String create(@PathVariable("user") String user) {
		//log.info(user + " prepare to create");
		System.out.println("用户["+user+"]进入创建界面,准备创建流程。");
		return "create";
	}

	@RequestMapping(value = "{user}/create", method = { POST, PUT })
	public String doCreate(@PathVariable("user") String user, Model model) {
		//log.info(user + " create an order");
		System.out.println("用户["+user+"]提交了流程单据");

		StatefulKnowledgeSession ksession = jbpmSupport.getKsession();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("priority", "High");
		params.put("modelNumber", "179");
		params.put("quantity", "100");
		System.out.println("用户["+user+"]赋初始值,并准备启动流程.......");
		ksession.startProcess("com.gds.jbpm.sample", params);
		ksession.fireAllRules();
		System.out.println("用户["+user+"]创建流程,等待处理......");
		model.addAttribute("message", "process created!");
		System.out.println("---------------------------------------------------单据提交完成:"+user);
		return "create";
	}

	@RequestMapping("{user}/list")
	public String list(@PathVariable("user") String user, Model model) {
		//log.info(user + " list his tasks");
		System.out.println("用户["+user+"]读取任务列表.");

		TaskService taskService = jbpmSupport.getTaskService();
		List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner(user, "en-UK");

		log.info("\n***Task size::" + tasks.size() + "***\n");
		for (TaskSummary taskSummary : tasks) {
			log.info(taskSummary.getId() + " :: " + taskSummary.getActualOwner());
		}

		model.addAttribute("tasks", tasks);
		model.addAttribute("tasksCount", tasks.size());
		model.addAttribute("user", user);
		System.out.println("---------------------------------------------------列表读取完成:"+user);
		return "list";
	}

	@RequestMapping(value = "{user}/work/{task}", method = GET)
	public String work(@PathVariable("user") String user, @PathVariable("task") long taskId, Model model) {
		//log.info(user + " prepare to work on task " + taskId);
		System.out.println("用户["+user+"]读取任务以便处理,任务ID="+taskId);

		StatefulKnowledgeSession ksession = jbpmSupport.getKsession();
		TaskService taskService = jbpmSupport.getTaskService();

		Task task = taskService.getTask(taskId);
		TaskData taskData = task.getTaskData();
		WorkflowProcessInstance process = (WorkflowProcessInstance) ksession.getProcessInstance(taskData
				.getProcessInstanceId());

		model.addAttribute("taskData", taskData);
		model.addAttribute("order", new Order(user, taskId, process));
		System.out.println("---------------------------------------------------任务读取完成:"+user);
		return "work";
	}

	@RequestMapping(value = "{user}/work/{task}", method = { POST, PUT })
	public String doWork(@ModelAttribute Order order) {
		System.out.println("处理任务信息:"+order.toString());
		String user = order.getUser();
		long taskId = order.getTaskId();
		log.info(user + " complete work on task " + taskId);

		TaskService taskService = jbpmSupport.getTaskService();
		System.out.println("用户["+user+"]启动人工任务, taskId="+taskId);
		taskService.start(taskId, user);

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("priority", order.getPriority());
		data.put("modelNumber", order.getModelNumber());
		data.put("quantity", order.getQuantity());

		ContentData contentData = null;
		if (data != null) {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream outs;
			try {
				outs = new ObjectOutputStream(bos);
				outs.writeObject(data);
				outs.close();
				contentData = new ContentData();
				contentData.setContent(bos.toByteArray());
				contentData.setAccessType(AccessType.Inline);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		taskService.complete(taskId, user, contentData);
		System.out.println("用户["+user+"]完成人工任务");
		System.out.println("---------------------------------------------------处理任务完成:"+user);

		return "redirect:/" + user + "/list";
	}
}



application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:drools="http://drools.org/schema/drools-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
	http://drools.org/schema/drools-spring 
	http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.2.0.xsd"
	default-autowire="byName">

	<context:annotation-config />
	<context:component-scan base-package="com.gds.jbpm" />

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://127.0.0.1:3306/jbpmtest?characterEncoding=UTF-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceUnitName" value="org.jbpm.persistence.jpa" />
	</bean>
	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<drools:grid-node id="node1" />
	<drools:kstore id="kstore1" />
	<drools:kbase id="kbase1" node="node1">
		<drools:resources>
			<drools:resource id="resource1" type="BPMN2"
				source="classpath:Sample.bpmn" />
		</drools:resources>
	</drools:kbase>

	<drools:ksession id="ksession" type="stateful" kbase="kbase1"
		node="node1">
		<drools:configuration>
			<drools:jpa-persistence>
				<drools:transaction-manager ref="txManager" />
				<drools:entity-manager-factory ref="entityManagerFactory" />
			</drools:jpa-persistence>
		</drools:configuration>
	</drools:ksession>

</beans>

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com</groupId>
	<artifactId>JBPM5Spring</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>JBPM5Spring Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<junit.version>4.8.2</junit.version>
		<spring.version>3.0.5.RELEASE</spring.version>
		<drools.version>5.3.1.Final</drools.version>
		<jbpm.version>5.2.0.Final</jbpm.version>
		<jbpm.stable.version>5.2.0.Final</jbpm.stable.version>

		<slf4j.version>1.6.1</slf4j.version>
		<mysql.version>5.1.10</mysql.version>
		<jstl.version>1.1.2</jstl.version>
		<servlet.version>2.5</servlet.version>
		<javaee-api.version>6.0</javaee-api.version>
		<jsp-api.version>2.1</jsp-api.version>
		<jboss.netty.version>3.2.0.Final</jboss.netty.version>
		<codehaus.btm.version>2.1.1</codehaus.btm.version>
		<thoughtworks.xstream.version>1.3.1</thoughtworks.xstream.version>
		<aspectj.version>1.6.2</aspectj.version>
		<cglib.version>2.2</cglib.version>
		<jsp.version>2.0</jsp.version>
		<log4j.version>1.2.16</log4j.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.lazyluke</groupId>
			<artifactId>log4jdbc-remix</artifactId>
			<version>0.2.7</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>${cglib.version}</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${jstl.version}</version>
		</dependency>



		<dependency>
			<groupId>org.jboss.netty</groupId>
			<artifactId>netty</artifactId>
			<version>${jboss.netty.version}</version>
		</dependency>
		<!-- JBPM5.2 -->
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-spring</artifactId>
			<version>${drools.version}</version>
		</dependency>
		<dependency>
			<groupId>org.drools</groupId>
			<artifactId>drools-persistence-jpa</artifactId>
			<version>${drools.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jbpm</groupId>
			<artifactId>jbpm-human-task</artifactId>
			<version>${jbpm.stable.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jbpm</groupId>
			<artifactId>jbpm-flow</artifactId>
			<version>${jbpm.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jbpm</groupId>
			<artifactId>jbpm-bpmn2</artifactId>
			<version>${jbpm.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jbpm</groupId>
			<artifactId>jbpm-persistence-jpa</artifactId>
			<version>${jbpm.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jbpm</groupId>
			<artifactId>jbpm-flow-builder</artifactId>
			<version>${jbpm.version}</version>
		</dependency>
		<dependency>
			<groupId>org.jbpm</groupId>
			<artifactId>jbpm-bam</artifactId>
			<version>${jbpm.version}</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.btm</groupId>
			<artifactId>btm</artifactId>
			<version>${codehaus.btm.version}</version>
		</dependency>
		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>${thoughtworks.xstream.version}</version>
			<type>jar</type>
		</dependency>

		<!-- Spring 3 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<optional>true</optional>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>


		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		<dependency>
			<groupId>jaxen</groupId>
			<artifactId>jaxen</artifactId>
			<version>1.1.3</version>
			<exclusions>
				<exclusion>
					<artifactId>maven-cobertura-plugin</artifactId>
					<groupId>maven-plugins</groupId>
				</exclusion>
				<exclusion>
					<artifactId>maven-findbugs-plugin</artifactId>
					<groupId>maven-plugins</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.6</version>
		</dependency>
	</dependencies>



	<build>
		<finalName>JBPM5Spring</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<archive>
						<manifest>
							<!--<addClasspath>true</addClasspath> -->
						</manifest>
						<manifestEntries>
							<Built-By>org-builder</Built-By>
							<Build-Jdk>${java.version}</Build-Jdk>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.20</version>
				<configuration>
					<contextPath>/JBPM5Spring</contextPath>
					<!--<webDefaultXml>webdefault.xml</webDefaultXml> -->
					<scanIntervalSeconds>0</scanIntervalSeconds>
					<scanTargetPatterns>
						<scanTargetPattern>
							<directory>src/main/webapp/WEB-INF</directory>
							<excludes>
								<exclude>**/*.jsp</exclude>
							</excludes>
							<includes>
								<include>**/*.properties</include>
								<include>**/*.xml</include>
							</includes>
						</scanTargetPattern>
					</scanTargetPatterns>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<archive>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
						</manifest>
						<manifestEntries>
							<Implementation-Build>${buildNumber}</Implementation-Build>
						</manifestEntries>
					</archive>
					<dependentWarExcludes>
						**/jdbc.properties,**/web.xml,WEB-INF/classes/META-INF/**
					</dependentWarExcludes>
				</configuration>
			</plugin>

		</plugins>
	</build>

</project>

你可能感兴趣的:(spring)