activiti學習筆記(四)managementService

weixin_34075551發表於2018-10-07

managementService

  • Job任務管理
  • 資料庫相關通用操作
  • 執行流程引擎命令(Command)

Job任務查詢

4302734-ac5701c96331ca35.png
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;
   }
});

相關文章