疯狂Activiti6.0连载(22)流程存储Web Service

原创
2017/12/03 21:10
阅读数 1.4K

本文节选自《疯狂工作流讲义(第2版)》

京东购买地址:https://item.jd.com/12246565.html

疯狂Activiti电子书:https://my.oschina.net/JavaLaw/blog/1570397

工作流Activiti教学视频:https://my.oschina.net/JavaLaw/blog/1577577

流程存储Web Service

        Activiti的所发布的Web Service,主要是调用流程引擎的各个服务组件,操作流程相关数据,这些操作包括增加、查询、删除、数据修改和文件上传,从本小节开始,将讲解这部分Web Service的使用,注意下,本章的全部例子均使用CXF的API来编写请求客户端。

上传部署文件

        Activiti发布了一个/deployment接口,调用该接口可以将流程相关的文件远程部署到流程引擎中,目前支持的格式有.bpmn、.bpmn20.xml、.bar和.zip文件。部署activiti-rest后,这个接口的访问地址为http://localhost:8080/activiti-rest/service/deployment,当/deployment接口成功接收请求后,会调用RepositoryService().createDeployment()等一系列方法添加资源文件并且进行部署,RepositoryService的使用详细请见流程存储章节。代码清单17-9为客户端代码。

        代码清单17-9:codes\17\rs-client\src\org\crazyit\activiti\rest\TestDeployment.java

        // 创建WebClient,设置URL、认证用户名和密码
        WebClient client = WebClient.create(
                "http://localhost:8080/activiti-rest/service/repository/deployments",
                "crazyit", "123456", null);
        // 设置认证格式为基础认证格式
        String authorizationHeader = "Basic "
                + org.apache.cxf.common.util.Base64Utility
                        .encode("user:password".getBytes());
        client.header("Authorization", authorizationHeader);
        // 设置内容类型
        client.type("multipart/form-data");
        // 获取上传文件
        String path = TestDeployment.class.getResource("/").toString();
        File file = new File(new URI(path + "bpmn/DeploymentUpload.bpmn20.xml"));
        // 一定需要name属性
        ContentDisposition cd = new ContentDisposition(
                "form-data; name=deployment; filename=DeploymentUpload.bpmn20.xml");
        Attachment att = new Attachment(null, new FileInputStream(file), cd);
        // 获取响应,使用POS方法
        Response response = client.post(new MultipartBody(att));
        // 获取响应内容
        InputStream ent = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(ent);
        // 输出响应字符串
        System.out.println(content);

        调用时需要注意,在构建ContentDisposition实例时,必须提供name属性,filename属性的文件名称后缀必须为.bpmn、.bpmn20.xml、.bar和.zip,否则接口会返回:“File must be of type .bpmn20.xml, .bpmn, .bar or .zip”这样的异常信息。成功调用接口后,返回以下JSON:

{
    "id": "3801",
    "name": "DeploymentUpload.bpmn20.xml",
    "deploymentTime": "2017-07-23T07:34:24.496+08:00",
    "category": null,
    "url": "http://localhost:8080/activiti-rest/service/repository/deployments/3801",
    "tenantId": ""
}

部署数据查询

        使用部署数据查询接口,可以分页查询多个部署数据,接口调用的地址为:repository/deployments,地址后可接多个参数,代码清单17-10为接口调用代码。

        代码清单17-10:codes\17\rs-client\src\org\crazyit\activiti\rest\TestDeployments.java

        // 创建WebClient,设置URL、认证用户名和密码
        WebClient client = WebClient
                .create("http://localhost:8080/activiti-rest/service/"
                        + "repository/deployments?sort=name&nameLike=%processes",
                        "crazyit", "123456", null);
        // 设置认证格式为基础认证格式
        String authorizationHeader = "Basic "
                + org.apache.cxf.common.util.Base64Utility
                        .encode("user:password".getBytes());
        client.header("Authorization", authorizationHeader);
        // 获取响应
        Response response = client.get();
        // 获取响应内容
        InputStream ent = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(ent);
        // 输出字符串
        System.out.println(content);

        代码清单17-10中的粗体字代码,加入了sort参数进行按name字段进行排序,使用nameLike参数进行名称的模糊查询,在使用模糊查询时,注意需要传入通配符。调用接口后返回以下JSON字符串:

{
    "data": [
        {
            "id": "1320",
            "name": "Demo processes",
            "deploymentTime": "2017-07-22T08:36:48.793+08:00",
            "category": null,
            "url": "http://localhost:8080/activiti-rest/service/repository/deployments/1320",
            "tenantId": ""
        }
    ],
    "total": 1,
    "start": 0,
    "sort": "name",
    "order": "asc",
    "size": 1
}

部署资源查询

        一个部署过程可以部署多个资源文件,查询这些资源可以使用repository/deployments/{ deploymentId }/resources接口,传入部署数据的ID,代码清单17-15为测试代码。

        代码清单17-11:codes\17\rs-client\src\org\crazyit\activiti\rest\QueryResources.java

        // 创建WebClient,设置URL、认证用户名和密码
        WebClient client = WebClient
                .create("http://localhost:8080/activiti-rest/service/"
                        + "repository/deployments/1001/resources",
                        "crazyit", "123456", null);
        // 设置认证格式为基础认证格式
        String authorizationHeader = "Basic "
                + org.apache.cxf.common.util.Base64Utility
                        .encode("user:password".getBytes());
        client.header("Authorization", authorizationHeader);
        // 获取响应
        Response response = client.get();
        // 获取响应内容
        InputStream ent = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(ent);
        // 输出字符串
        System.out.println(content);

        调用接口成功后,返回以下JSON:

[
    {
        "id": "bpmn/TaskExternalForm.bpmn",
        "url": "http://localhost:8080/activiti-rest/service/repository/deployments/1001/resources/bpmn/TaskExternalForm.bpmn",
        "contentUrl": "http://localhost:8080/activiti-rest/service/repository/deployments/1001/resourcedata/bpmn/TaskExternalForm.bpmn",
        "mediaType": "text/xml",
        "type": "processDefinition"
    },
    {
        "id": "forms/TaskExternalForm.form",
        "url": "http://localhost:8080/activiti-rest/service/repository/deployments/1001/resources/forms/TaskExternalForm.form",
        "contentUrl": "http://localhost:8080/activiti-rest/service/repository/deployments/1001/resourcedata/forms/TaskExternalForm.form",
        "mediaType": null,
        "type": "resource"
    }
]

查询单个部署资源

        查询单个部署资源,使用repository/deployments/{deploymentId}/resources/{resourceId}接口,调用时要传入部署数据的id以及资源id,注意资源id并不是资源表的ID_字段,而是由“部署资源查询”接口返回的id,对应的是数据库中的NAME_字段,代码清单17-12调用该接口。

        代码清单17-12:codes\17\rs-client\src\org\crazyit\activiti\rest\QueryResource.java

        // 创建WebClient,设置URL、认证用户名和密码
        WebClient client = WebClient
                .create("http://localhost:8080/activiti-rest/service/"
                        + "repository/deployments/1001/resources/bpmn/TaskExternalForm.bpmn",
                        "crazyit", "123456", null);
        // 设置认证格式为基础认证格式
        String authorizationHeader = "Basic "
                + org.apache.cxf.common.util.Base64Utility
                        .encode("user:password".getBytes());
        client.header("Authorization", authorizationHeader);
        // 获取响应
        Response response = client.get();
        // 获取响应内容
        InputStream ent = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(ent);
        // 输出字符串
        System.out.println(content);

        调用接口后,返回的JSON如下:

{
    "id": "bpmn/TaskExternalForm.bpmn",
    "url": "http://localhost:8080/activiti-rest/service/repository/deployments/1001/resources/bpmn/TaskExternalForm.bpmn",
    "contentUrl": "http://localhost:8080/activiti-rest/service/repository/deployments/1001/resourcedata/bpmn/TaskExternalForm.bpmn",
    "mediaType": "text/xml",
    "type": "processDefinition"
}

删除部署

        上传部署文件的方法,使用的是HTTP的POST方法,而前面的几个部署数据查询方法,使用的则是GET方法,删除部署,需要使用DELETE方法。删除部署的URL为repository/deployments/{ deploymentId },调用该接口后,如果删除成功并不会返回结果,如果删除失败,则会返回异常信息,代码清单17-13为测试代码。

        代码清单17-13:codes\17\rs-client\src\org\crazyit\activiti\rest\DeleteDeployment.java

        // 创建WebClient,设置URL、认证用户名和密码
        WebClient client = WebClient
                .create("http://localhost:8080/activiti-rest/service/"
                        + "repository/deployments/3807",
                        "crazyit", "123456", null);
        // 设置认证格式为基础认证格式
        String authorizationHeader = "Basic "
                + org.apache.cxf.common.util.Base64Utility
                        .encode("user:password".getBytes());
        client.header("Authorization", authorizationHeader);
        // 获取响应
        Response response = client.delete();
        // 获取响应内容
        InputStream ent = (InputStream) response.getEntity();
        String content = IOUtils.readStringFromStream(ent);
        // 输出字符串(本例并不会有响应内容)
        System.out.println(content);

        在使用删除部署接口时,需要注意以下细节:

  • 成功删除后,不会返回JSON数据。
  • 如果被删除的部署数据id不存在,接口则会接收到异常信息:Could not find a deployment with id。
  • 如果被删除的部署已经产生的外键关联(例如启动了流程实例),同样会删除失败。

        除了流程存储模块发布的接口,其他模块也提供了大量操作流程引擎的接口,所谓一通百通,其他接口的调用,在此不再赘述。

本文节选自《疯狂工作流讲义(第2版)》

京东购买地址:https://item.jd.com/12246565.html

疯狂Activiti电子书:https://my.oschina.net/JavaLaw/blog/1570397

工作流Activiti教学视频:https://my.oschina.net/JavaLaw/blog/1577577

本书代码目录:https://gitee.com/yangenxiong/CrazyActiviti

展开阅读全文
加载中
点击加入讨论🔥(2) 发布并加入讨论🔥
2 评论
3 收藏
0
分享
返回顶部
顶部