公司要使用activiti做流程管理中心,之前知道有这个框架,但是一直没有机会去接触学习,公司采用了springboot框架,(我就不介绍这个框架的使用了,简单来说就是spring去掉配置文件,就是springboot,哈哈,我是这样认为的,比较容易上手)废话不多说,下面开始框架的整合;
**第一步:搭建springboot框架**
无论是idea 还是eclipse,都有插件生成springboot框架,但是个人认为,还是自己搭建比较好,可以了解框架由哪几部分组成,我是用的idea,我就不适用idea一键生成springboot框架了,自己搭建比较好。
(1)创建一个maven web项目
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>1.5.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>1.5.4.RELEASEversion>
dependency>
dependencies>
添加类 ServletInitializer.java,添加application.properties or application.ylm你喜欢哪一种配置方式都可以如图所示文件目录:
ServletInitializer.java
@ComponentScan
@ServletComponentScan
@EnableTransactionManagement
@MapperScan(basePackages = "com.river.dao") //整合mybatis 扫描路径
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
//这玩意后面会解释
public class ServletInitializer extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ServletInitializer.class);
}
public static void main(String[] args) {
SpringApplication.run(ServletInitializer.class,args);
}
}
到这里springboot框架就搭建好了,需要依赖自己往pom.xml里添加。我的pom.xml如下
<properties>
<activiti.version>5.22.0activiti.version>
properties>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>1.5.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>1.5.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
<version>1.5.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.44version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.5version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-spring-boot-starter-basicartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-spring-boot-starter-actuatorartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-restartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.apache.xmlgraphicsgroupId>
<artifactId>batik-codecartifactId>
<version>1.7version>
dependency>
<dependency>
<groupId>org.apache.xmlgraphicsgroupId>
<artifactId>batik-cssartifactId>
<version>1.7version>
dependency>
<dependency>
<groupId>org.apache.xmlgraphicsgroupId>
<artifactId>batik-svg-domartifactId>
<version>1.7version>
dependency>
<dependency>
<groupId>org.apache.xmlgraphicsgroupId>
<artifactId>batik-svggenartifactId>
<version>1.7version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-explorerartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-diagram-restartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-simple-workflowartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-springartifactId>
<version>${activiti.version}version>
dependency>
dependencies>
application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_activiti_service
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:com/river/dao/*.xml #mybatis使用
logging.file=log.log
logging.pattern.level=debug
security.basic.enabled=false
(2)开始整合activiti+modeler
在网上有一些介绍,直接在pom.xml中直接添加
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-spring-boot-starter-basicartifactId>
<version>${activiti.version}version>
dependency>
<dependency>
<groupId>org.activitigroupId>
<artifactId>activiti-spring-boot-starter-actuatorartifactId>
<version>${activiti.version}version>
dependency>
这两个依赖就可以,但事实是不可以的,我也不知道为啥,在springboot中,数据源无法自动配置到ProcessEngineConfigurationConfigurer中,导致项目启动时processEngine为null,搞了我大半天,然后自己写配置自己掌控processEngine的DataSource
1、添加配置类ActivitiDataSourceProperties.java
@Component
public class ActivitiDataSourceProperties {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
2、ActivitiDatadourceConfig.java
@Configuration
public class ActivitiDatadourceConfig extends AbstractProcessEngineAutoConfiguration {
@Resource
private ActivitiDataSourceProperties activitiDataSourceProperties;
@Bean
@Primary
public DataSource activitiDataSource() {
DruidDataSource DruiddataSource = new DruidDataSource();
DruiddataSource.setUrl(activitiDataSourceProperties.getUrl());
DruiddataSource.setDriverClassName(activitiDataSourceProperties.getDriverClassName());
DruiddataSource.setPassword(activitiDataSourceProperties.getPassword());
DruiddataSource.setUsername(activitiDataSourceProperties.getUsername());
return 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-master
分配在这些文件夹中找到上面的modeler的文件,放到相应的位置中
修改一下这个文件
虽然在pom.xml中有activiti-modeler这个依赖,但是集合springboot的时候不能使用,以为在传参数的时候,@RequestBody @RequestBody MultiValueMap
@RestController
public class ModelSaveRestResource implements ModelDataJsonConstants {
protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);
@Autowired
private RepositoryService repositoryService;
@RequestMapping(value = "/model/{modelId}/save", method = RequestMethod.PUT)
public void saveModel(@PathVariable String modelId, @RequestParam String json_xml,
@RequestParam String svg_xml,@RequestParam String name, @RequestParam String description) {
try {
ObjectMapper objectMapper = new ObjectMapper();
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();
//部署流程
Model modelData = repositoryService.getModel(modelId);
ObjectNode modelNode = (ObjectNode) objectMapper.readTree(repositoryService.getModelEditorSource(modelData.getId()));
byte[] bpmnBytes = null;
BpmnModel model2 = new BpmnJsonConverter().convertToBpmnModel(modelNode);
bpmnBytes = new BpmnXMLConverter().convertToXML(model2);
String processName = modelData.getName() + ".bpmn";
//部署流程
Deployment deployment = repositoryService.createDeployment()
.name(modelData.getName())
.addString(processName, StringUtils.toEncodedString(bpmnBytes, Charset.forName("UTF-8")))
.deploy();
} catch (Exception e) {
LOGGER.error("Error saving model", e);
throw new ActivitiException("Error saving model", e);
}
}
}
(3)到这里就差不多了,但是启动之后可以在http://localhost:8080需要你输入用户名和密码;这是解决的方法,同事也解释上面的一些配置的原因,看一下:
解决Springboot集成ActivitiModel提示输入密码的问题:
http://blog.csdn.net/sinat_38335497/article/details/74285053
http://blog.csdn.net/KingBoyWorld/article/details/77697653
创建空mode即跳转到modeler编辑页面的代码如下
@Controller
@RequestMapping("/modeler")
public class ModelerController {
private static final Logger logger = LoggerFactory.getLogger(ModelerController.class);
@Resource
private ModelerService modelerService;
/**
* 创建流程模型
* @param name
* @param key
* @param description
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createModel(@RequestParam("name") String name, @RequestParam("key") String key,
@RequestParam("description") String description,
HttpServletRequest request, HttpServletResponse response) {
logger.info("创建空modeler:name:{},key:{},description:{}", name, key, description);
try {
//创建空模型
String modelId = modelerService.crateModel(name, key, description);
if(StringUtils.isBlank(modelId)) {
throw new RuntimeException("创建modeler失败modelId:" + modelId);
}
return "redirect:../modeler.html?modelId=" + modelId;
}catch (Exception e) {
logger.error("创建模型失败",e );
throw new RuntimeException(e);
}
}
/**
* 模型列表
* @param modelAndView
* @return
*/
@RequestMapping("/model/list")
public ModelAndView modelList(ModelAndView modelAndView) {
List list = modelerService.queryModelList();
return modelAndView;
}
第一次写博客,不太会怎么整理,嘻嘻,有什么问题可以给我留言,谢谢支持,如果看不懂可以参考一下我的的github:
https://github.com/fengheatu/springboot.activiti