activiti Spring boot 整合源码
项目采用Springboot 2.0.3.RELEASE版本以及activiti 5.22.0版本
在acitiviti官网下载完整包https://github.com/Activiti/Activiti/releases/tag/activiti-5.22.0
下载完成后
1、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\main\webapp下的diagram-viewer、editor-app以及modeler.html文件放置在项目resources\static文件夹下。
2、将Activiti-activiti-5.22.0\modules\activiti-webapp-explorer2\src\resources下的stencilset.json放置在项目resources\static文件夹下。
3、将Activiti-activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor下的main以及model中的java文件放置到项目mian\java目录下
复制完成后,首先将ModelEditoeJsonRestResource.java、ModelSaveRestResource.java、StencilsetRestResource.java上添加 @RequestMapping(value = “/service”)
然后修改resources\static\editor-app\app-cfg.js,如下图
修改ModelSaveRestResource.java,如下(参考https://blog.csdn.net/qq_21770005/article/details/78713912)
@RestController
@RequestMapping(value = "/service")
public class ModelSaveRestResource implements ModelDataJsonConstants {
private static final Logger LOGGER = LogManager.getLogger(ModelSaveRestResource.class);
@Autowired
private RepositoryService repositoryService;
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.OK)
public void saveModel(@PathVariable String modelId, @RequestParam("name") String name,
@RequestParam("json_xml") String json_xml, @RequestParam("svg_xml") String svg_xml,
@RequestParam("description") String description) {
try {
Model model = repositoryService.getModel(modelId);
ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
modelJson.put(MODEL_NAME, name);
modelJson.put(MODEL_DESCRIPTION, description);
model.setMetaInfo(modelJson.toString());
model.setName(name);
repositoryService.saveModel(model);
repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));
InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
TranscoderInput input = new TranscoderInput(svgStream);
PNGTranscoder transcoder = new PNGTranscoder();
// Setup output
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
// Do the transformation
transcoder.transcode(input, output);
final byte[] result = outStream.toByteArray();
repositoryService.addModelEditorSourceExtra(model.getId(), result);
outStream.close();
} catch (Exception e) {
LOGGER.error("Error saving model", e);
throw new ActivitiException("Error saving model", e);
}
}
}
在pom.xml添加activiti依赖
org.activiti
activiti-spring-boot-starter-basic
${activiti.version}
org.activiti
activiti-spring-boot-starter-actuator
${activiti.version}
org.activiti
activiti-rest
${activiti.version}
org.apache.xmlgraphics
batik-codec
1.7
org.apache.xmlgraphics
batik-css
1.7
org.apache.xmlgraphics
batik-svg-dom
1.7
org.apache.xmlgraphics
batik-svggen
1.7
org.activiti
activiti-explorer
${activiti.version}
org.activiti
activiti-diagram-rest
${activiti.version}
org.activiti
activiti-simple-workflow
${activiti.version}
org.activiti
activiti-spring
${activiti.version}
在application.yml中添加activiti的数据源,如下
spring:
datasource:
activiti:
url:jdbc:mysql://127.0.0.1:3306/clouddo?nullCatalogMeansCurrent=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username: root
password: 123asd
driverClassName: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
添加activiti数据源配置类
@Configuration
public class ActitytiDataSourceConfig extends AbstractProcessEngineAutoConfiguration {
private static final Logger log = LogManager.getLogger(ActitytiDataSourceConfig.class);
@Bean(name = "activitiDataSource")
@ConfigurationProperties(prefix = "spring.datasource.activiti")
public DataSource activitiDataSource(){
log.info("activitiDataSource 初始化...");
return new DruidDataSource();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(activitiDataSource());
}
@Bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
configuration.setDataSource(activitiDataSource());
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
configuration.setJobExecutorActivate(true);
configuration.setTransactionManager(transactionManager());
return configuration;
}
}
以上,基本配置已完成。在启动项目后,将自动创建activiti所需要的25张表。
如报错无法启动,在application.yml中添加
spring:
activiti:
check-process-definitions: false
成功启动项目后,发现访问项目任何路径都需要输入用户名和密码。这是因为acitivi-rest中集成了spring-security,需要在启动类上添加
@EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, org.activiti.spring.boot.SecurityAutoConfiguration.class})
访问http://localhost:8080/modeler.html,发现页面未显示内容,这是因为目前还未创建任何model
此时创建ActivitiModelController.java,如下
@Controller
public class ActivitiModelController {
private static final Logger log = LogManager.getLogger(ActivitiModelController.class);
@Autowired
ProcessEngine processEngine;
@Autowired
ObjectMapper objectMapper;
/**
* 新建一个空模型
*/
@RequestMapping("/create")
public void newModel(HttpServletResponse response) throws IOException {
RepositoryService repositoryService = processEngine.getRepositoryService();
//初始化一个空模型
Model model = repositoryService.newModel();
//设置一些默认信息
String name = "new-process";
String description = "";
int revision = 1;
String key = "process";
ObjectNode modelNode = objectMapper.createObjectNode();
modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);
model.setName(name);
model.setKey(key);
model.setMetaInfo(modelNode.toString());
repositoryService.saveModel(model);
String id = model.getId();
//完善ModelEditorSource
ObjectNode editorNode = objectMapper.createObjectNode();
editorNode.put("id", "canvas");
editorNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace",
"http://b3mn.org/stencilset/bpmn2.0#");
editorNode.put("stencilset", stencilSetNode);
repositoryService.addModelEditorSource(id,editorNode.toString().getBytes("utf-8"));
response.sendRedirect("/modeler.html?modelId="+id);
}
/**
* 获取所有模型
*/
@RequestMapping("/modelList")
@ResponseBody
public Object modelList(){
RepositoryService repositoryService = processEngine.getRepositoryService();
return repositoryService.createModelQuery().list();
}
/**
* 发布模型为流程定义
*/
@RequestMapping("/deploy")
@ResponseBody
public Object deploy(String modelId) throws Exception {
//获取模型
RepositoryService repositoryService = processEngine.getRepositoryService();
Model modelData = repositoryService.getModel(modelId);
byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
if (bytes == null) {
return "模型数据为空,请先设计流程并成功保存,再进行发布。";
}
JsonNode modelNode = new ObjectMapper().readTree(bytes);
BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
if(model.getProcesses().size()==0){
return "数据模型不符要求,请至少设计一条主线流程。";
}
byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
//发布流程
String processName = modelData.getName() + ".bpmn20.xml";
Deployment deployment = repositoryService.createDeployment()
.name(modelData.getName())
.addString(processName, new String(bpmnBytes, "UTF-8"))
.deploy();
modelData.setDeploymentId(deployment.getId());
repositoryService.saveModel(modelData);
return "SUCCESS";
}
/**
* 启动流程
*/
@RequestMapping("/start")
@ResponseBody
public Object startProcess(String keyName) {
ProcessInstance process = processEngine.getRuntimeService().startProcessInstanceByKey(keyName);
return process.getId() + " : " + process.getProcessDefinitionId();
}
/**
* 提交任务
*/
@RequestMapping("/run")
@ResponseBody
public Object run(String processInstanceId) {
Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).singleResult();
log.info("task {} find ", task.getId());
processEngine.getTaskService().complete(task.getId());
return "SUCCESS";
}
}
通过访问http://localhost:8080/create创建一个空白的model并跳转到编辑页面
在绘制流程完成后,访问http://localhost:8080/deploy?modelId=1 对该流程进行部署
http://localhost:8080/start?keyName=hello 启动流程
http://localhost:8080/run?processInstanceId=1 提交
至此,springboot集成activiti modeler完成
需要源码,请及时联系我。
https://download.csdn.net/download/u013116345/10774314