managementService
- Job任务管理
- 数据库相关通用操作
- 执行流程引擎命令(Command)
Job任务查询
从上述表可以看出主要还是Job相关的查询。
数据库相关操作
- 查询表结构元数据(TableMetaData)
- 通用表查询(TablePageQuery)
- 执行自定义的sql查询(executeCustomSql)
示例
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = cfg.buildProcessEngine();
ManagementService managementService = processEngine.getManagementService();
// 获取指定表的数据
TablePage tablePage = managementService.createTablePageQuery()
.tableName(managementService.getTableName(ProcessDefinitionEntity.class))
.listPage(0,100);
List<Map<String,Object>> rowsList = tablePage.getRows();
for (Map<String,Object> row : rowsList){
logger.info("row={}",row);
}
上述示例中通过调用tableName方法来指定要查询的表,除了直接传入表名如"act_ru_task"这种形式外,还可以使用上述managementService.getTableName(ProcessDefinitionEntity.class)
方法通过实体类来获取表名。
接下来再看下如何自定义查询方法:
首先修改配置文件如下:
public interface MyCustomMapper {
@Select("select * from act_ru_task")
public List<Map<String,Object>> findAll();
}
接口的方法定义中添加select注解来实现自定义sql语句。
然后配置下流程配置文件:
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="abc123" />
<property name="databaseSchemaUpdate" value="true" />
<property name="customMybatisMappers">
<set>
<value>com.activiti.MyCustomMapper</value>
</set>
</property>
</bean>
最后后台代码调用这个自定义接口:
List<Map<String, Object>> mapList = managementService
.executeCustomSql(
new AbstractCustomSqlExecution<MyCustomMapper,List<Map<String,Object>>>(MyCustomMapper.class){
public List<Map<String, Object>> execute(MyCustomMapper myCustomMapper) {
return myCustomMapper.findAll();
}
});
for(Map<String,Object> map: mapList){
logger.info("map={}",map);
}
结果正常输出act_ru_task表的数据。
下面接着看managementService执行自定义命令:
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = cfg.buildProcessEngine();
ManagementService managementService = processEngine.getManagementService();
managementService.executeCommand(new Command() {
public Object execute(CommandContext commandContext) {
// 自定义命令实现
return null;
}
});