Activiti第四篇工作流之流程引擎配置

本文详细介绍了如何配置Activiti流程引擎,包括从默认资源、自定义资源文件、文件输入流创建配置,以及数据源配置。此外,还展示了如何通过编码和配置文件设置DBCP和C3P0数据源。同时,讲解了邮件服务器配置以及自定义ProcessEngineConfiguration和命令拦截器的实现,包括创建InterceptorA和InterceptorB,并在自定义配置类中添加到拦截器链中。
摘要由CSDN通过智能技术生成

ProcessEngineConfiguration对象代表一个Activiti流程引擎的全部配置,该类提供了一系列的创建ProcessEngineConfiguration实例的静态方法,这些方法用于读取和解析相应的配置文件,并返回ProcessEngineConfiguration的实例,除了这些静态方法外,该类还为其他可配置的引擎属性提供相应的setter和getter方法。
读取自定义的配置文件
log4j.xml

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

测试类

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 使用createProcessEngineConfigurationFromResourceDefault方法
 * 创建ProcessEngineConfiguration实例
 * @author Administrator
 *
 */
public class CreateDefault {

	public static void main(String[] args) {
		//使用Activiti默认的方式创建ProcessEngineConfiguration
		ProcessEngineConfiguration config = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();	
	}

}

日志

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

测试代码

 ProcessEngineConfiguration config = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();
        ProcessEngineConfiguration congfig1=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");

第二种

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 使用CreateProcessEngineConfigurationFromResource方法创建ProcessEngineConfiguration实例
 * 
 * @author Administrator
 * 
 */
public class CreateFromResource_1 {

	public static void main(String[] args) {
		// 指定配置文件创建ProcessEngineConfiguration
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("my-activiti1.xml");

	}

}

第三种

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 使用CreateProcessEngineConfigurationFromResource方法创建ProcessEngineConfiguration实例
 * 
 * @author Administrator
 * 
 */
public class CreateFromResource_2 {

	public static void main(String[] args) {
		// 指定配置文件创建ProcessEngineConfiguration
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"my-activiti2.xml", "test2");
		System.out.println(config.getProcessEngineName());
	}
}

读取文件输入流的配置

package org.crazyit.activiti;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;

/**
 * 使用createProcessEngineConfigurationFromInputStream方法创建ProcessEngineConfiguration
 * 
 * @author yangenxiong
 * 
 */
public class CreateInputStream {

	public static void main(String[] args) throws Exception {
		File file = new File("resource/input-stream.xml");
		// 得到文件输入流
		InputStream fis = new FileInputStream(file);
		// 使用createProcessEngineConfigurationFromInputStream方法创建ProcessEngineConfiguration
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromInputStream(fis);
	}

}

调用createStandaloneInMemProcessEngineConfiguration方法

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 使用createStandaloneInMemProcessEngineConfiguration创建ProcessEngineConfiguration
 * 
 * @author yangenxiong
 * 
 */
public class CreateStandaloneInMem {

	public static void main(String[] args) {
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createStandaloneInMemProcessEngineConfiguration();
		// 值为create-drop
		System.out.println(config.getDatabaseSchemaUpdate());
		// 值为jdbc:h2:mem:activiti
		System.out.println(config.getJdbcUrl());
	}

}

数据源的配置
Activiti在启动时,会读取数据源配置,以便对数据库进行相应的操作。
JDBC的配置
原始基于Java的实现

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;
import org.apache.commons.dbcp2.BasicDataSource;

/**
 * 使用编码方式设置DBCP数据源
 * 
 * @author yangenxiong
 * 
 */
public class DBCPCoding {

	public static void main(String[] args) throws Exception {
		// 创建DBCP数据源
		BasicDataSource ds = new BasicDataSource();
		// 设置JDBC连接的各个属性
		ds.setUsername("root");
		ds.setPassword("root");
		ds.setUrl("jdbc:mysql://localhost:3306/act");
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		// 验证是否连接成功
		ds.getConnection().getMetaData();
		// 读取Activiti配置文件
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("dbcp-coding.xml");
		// 为ProcessEngineConfiguration设置dataSource属性
		config.setDataSource(ds);
		System.out.println(config.getDataSource());
	}

}

dbcp-config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 使用DBCP数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/act" />
		<property name="username" value="root" />
		<property name="password" value="123456" />
	</bean>

	<bean id="processEngineConfiguration"
		class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>

日志log4j.properties配置文件

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

创建dbcp-coding.xml配置文件

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

测试

package org.crazyit.activiti;

import javax.sql.DataSource;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 使用配置方法设置DBCP数据源
 * 
 * @author yangenxiong
 * 
 */
public class DBCPConfig {

	public static void main(String[] args) throws Exception {
		// 读取 dbcp-config.xml配置
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("dbcp-config.xml");
		// 能正常输出,即完成配置
		DataSource ds = config.getDataSource();
		// 查询数据库元信息,如果能查询则表示连接成功
		ds.getConnection().getMetaData();
		// 结果为 org.apache.commons.dbcp.BasicDataSource
		System.out.println(ds.getClass().getName());
	}

}

CP30数据源的配置
日志log4j.properties

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

创建c3p0-config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 使用C3P0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
		<property name="user" value="root" />
		<property name="password" value="root" />
	</bean>

	<bean id="processEngineConfiguration"
		class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>

创建c3p0-coding.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 不初始化任何属性 -->
	<bean id="processEngineConfiguration"
		class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
	</bean>

</beans>

测试
基于Java的测试

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 使用编码方式设置DBCP数据源
 * 
 * @author yangenxiong
 * 
 */
public class C3P0Coding {

	public static void main(String[] args) throws Exception {
		// 创建C3P0数据源
		ComboPooledDataSource ds = new ComboPooledDataSource();
		// 设置JDBC连接的各个属性
		ds.setUser("root");
		ds.setPassword("123456");
		ds.setJdbcUrl("jdbc:mysql://localhost:3306/act");
		ds.setDriverClass("com.mysql.jdbc.Driver");
		// 验证是否连接成功
		ds.getConnection().getMetaData();

		// 读取Activiti配置文件
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("config/c3p0-coding.xml");
		// 为ProcessEngineConfiguration设置dataSource属性
		config.setDataSource(ds);
		System.out.println(config.getDataSource());
	}

}

测试基于XML配置文件

package org.crazyit.activiti;

import javax.sql.DataSource;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 使用配置方法设置DBCP数据源
 * 
 * @author yangenxiong
 * 
 */
public class C3P0Config {

	public static void main(String[] args) throws Exception {
		// 读取c3p0-config.xml配置
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("config/c3p0-config.xml");
		// 能正常输出,即完成配置
		DataSource ds = config.getDataSource();
		// 查询数据库元信息,如果能查询则表示连接成功
		ds.getConnection().getMetaData();
		// 结果为 com.mchange.v2.c3p0.ComboPooledDataSource
		System.out.println(config.getDataSource().getClass().getName());
	}

}

邮件服务器的配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">


	<bean id="processEngineConfiguration"
		class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUsername" value="root" />
		<property name="jdbcPassword" value="123456" />
		<property name="mailServerHost" value="smtp.163.com"></property>
		<property name="mailServerPort" value="25"></property>
		<property name="mailServerDefaultFrom" value="yangenxiong@163.com"></property>
		<property name="mailServerUsername" value="yangenxiong@163.com"></property>
		<property name="mailServerPassword" value="123456"></property>
	</bean>

</beans>

测试

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;


public class Mail {

	public static void main(String[] args) {
		ProcessEngineConfiguration config = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("mail.xml");
		System.out.println(config.getMailServerHost());
	}

}

自定义ProcessEngineConfiguration自定义属性
日志log4j.properties配置文件

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

创建my-config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 配置自定义属性 -->
	<bean id="processEngineConfiguration" class="org.crazyit.activiti.MyConfiguration">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUsername" value="root" />
		<property name="jdbcPassword" value="root" />
		<property name="databaseSchemaUpdate" value="true"></property>
		<property name="userName" value="crazyit"></property>
	</bean>

</beans>

配置类

package org.crazyit.activiti;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.interceptor.CommandContextInterceptor;
import org.activiti.engine.impl.interceptor.CommandInterceptor;

/**
 * 自定义配置类
 * @author yangenxiong
 *继承ProcessEngineConfigurationImpl,需要实现createTransactionInterceptor方法。
 * 可以为自己的ProcessEngineConfiguration类添加属性,并且添加相应的setter方法。
 */
public class MyConfiguration extends ProcessEngineConfigurationImpl {
	
	public MyConfiguration() {
		// 做自定义设置
	}
	
	//测试属性,需要在processEngineConfiguration注入
	private String userName;
	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserName() {
		return this.userName;
	}
	public CommandInterceptor createTransactionInterceptor() {
		return null;
	}
}

测试

package org.crazyit.activiti;

import org.activiti.engine.ProcessEngineConfiguration;

/**
 * 自定义配置测试
 * @author yangenxiong
 *
 */
public class ConfigTest {

	public static void main(String[] args) {
		//创建ProcessEngineConfiguration,并强制转换为MyConfiguration
		MyConfiguration config = (MyConfiguration)ProcessEngineConfiguration.
				createProcessEngineConfigurationFromResource("my-config.xml");
		config.buildProcessEngine();
		//打印出结果为crazyit
		System.out.println(config.getUserName());
	}

}

Activiti的命令拦截器
Activiti提供了命令拦截器功能,外界对Activiti流程中各个实例进行的操作,实际可以看作对数据进行的相应操作,在此过程中,Activiti使用了设计模式中的命令模式,每一个操作数据库的过程,均可被看作一个命令,然后交由命令执行者去完成。除此之外,为了能让使用者可以对这些命令进行相应的拦截(进行个性化处理),Activiti还使用了设计模式中的责任链模式,从而使用者可以添加相应的拦截器(责任链模式中的处理者)。

命令模式
在GoF的设计模式中,命令模式属于行为型模式,它把一个请求或者操作封装到命令对象中,这些请求或者操作的内容包括接收者信息,然后将该命令对象交由执行者执行,执行者不需要关心命令的接收人或者命令的具体内容,因为这些信息均被封装到命令对象中。命令模式中涉及的角色及其作用如下。
➢ 命令接口(Command):声明执行操作的接口。
➢ 接口实现(ConcreteCommand):命令接口实现,需要保存接收者的相应操作,并执行相应的操作。
➢ 命令执行者(Invoker):要求命令执行此次请求。
➢ 命令接收人(Receiver):由命令接口的实现类来维护Receiver实例,并在命令执行时处理相应的任务。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
责任模式
与命令模式一样,责任链模式也是GoF的设计模式之一,同样也是行为型模式。该设计模式让多个对象都有机会处理请求,从而避免了请求发送者和请求接收者之间的耦合。这些请求接收者将组成一条链,并沿着这条链传递请求,直到有一个对象处理这个请求为止,这就形成了一条责任链。责任链模式有以下参与者。
➢ 请求处理者接口(Handler):定义一个处理请求的接口,可以实现后继链。
➢ 请求处理者实现(ConcreteHandler):请求处理接口的实现,如果它可以处理请求就处理,否则就将该请求转发给它的后继者。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

编写自定义拦截器
创建my-config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 配置自定义属性 -->
	<bean id="processEngineConfiguration" class="org.crazyit.activiti.TestConfiguration">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUsername" value="root" />
		<property name="jdbcPassword" value="root" />
		<property name="databaseSchemaUpdate" value="true"></property>
	</bean>
</beans>

日志log4j

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n


log4j.logger.org.apache=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.org.htmlparser=INFO
log4j.logger.com.angus=INFO

流程图

<?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://www.activiti.org/test">
	<process id="vacationProcess" name="vacation">
		<startEvent id="startevent1" name="Start"></startEvent>
		<userTask id="usertask1" name="Write Vacation"></userTask>
		<endEvent id="endevent1" name="End"></endEvent>
		<sequenceFlow id="flow1" name="" sourceRef="startevent1"
			targetRef="usertask1"></sequenceFlow>
		<sequenceFlow id="flow2" name="" sourceRef="usertask1"
			targetRef="endevent1"></sequenceFlow>
	</process>
</definitions>

在这里插入图片描述
拦截器实现A

package org.crazyit.activiti;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandConfig;
import org.activiti.engine.impl.interceptor.CommandInterceptor;

/**
 * 拦截器实现A
 * 
 */
public class InterceptorA implements CommandInterceptor {

	private CommandInterceptor next;
	
	@Override
	public <T> T execute(CommandConfig config, Command<T> command) {
		// 输出字符串和命令
		System.out.println("this is interceptor A:"
				+ command.getClass().getName());
		// 然后让责任链中的下一请求处理者处理命令
		return getNext().execute(config, command);
	}
	
	public CommandInterceptor getNext() {
		return this.next;
	}
	
	public void setNext(CommandInterceptor next) {
		this.next = next;
	}
}

创建InterceptorB拦截器B

package org.crazyit.activiti;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandConfig;
import org.activiti.engine.impl.interceptor.CommandInterceptor;

public class InterceptorB implements CommandInterceptor {
	
	private CommandInterceptor next;

	@Override
	public <T> T execute(CommandConfig config, Command<T> command) {
		// 输出字符串和命令
		System.out.println("this is interceptor B "
				+ command.getClass().getName());
		// 然后让责任链中的下一请求处理者处理命令
		return next.execute(config, command);
	}

	@Override
	public CommandInterceptor getNext() {
		// TODO Auto-generated method stub
		return this.next;
	}

	@Override
	public void setNext(CommandInterceptor next) {
		this.next = next;
	}

}

自定义配置类

package org.crazyit.activiti;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.interceptor.CommandInterceptor;

/**
 * 自定义配置类
 */
public class TestConfiguration extends ProcessEngineConfigurationImpl {
	
	public CommandInterceptor createTransactionInterceptor() {
		// 不实现事务拦截器
		return null;
	}
	
	/**
	 * 重写初始化命令拦截器方法
	 */
	public void initCommandInterceptors() {
		// 为父类的命令集合添加拦截器
		customPreCommandInterceptors = new ArrayList<CommandInterceptor>();
		// 依次将A和B两个拦截器加入集合(责任链)
		customPreCommandInterceptors.add(new InterceptorA());
		customPreCommandInterceptors.add(new InterceptorB());
		// 再调用父类的实始化方法
		super.initCommandInterceptors();
	}
}

测试

package org.crazyit.activiti;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;

public class MyConfig {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ProcessEngines.getDefaultProcessEngine();
		// 创建Activiti配置对象
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("my-config.xml");		
		// 初始化流程引擎
		ProcessEngine engine = config.buildProcessEngine();
		// 部署一个最简单的流程
		engine.getRepositoryService().createDeployment()
				.addClasspathResource("bpmn/config.bpmn20.xml").deploy();
		// 构建流程参数
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("day", 10);
		// 开始流程
		engine.getRuntimeService().startProcessInstanceByKey("vacationProcess",
				vars);
		System.exit(0);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值