第 23 章 红帽流程自动化管理器中的 KIE 服务器和 KIE 容器命令

Red Hat Process Automation Manager 支持针对与服务器相关的或容器相关操作(如检索服务器信息或删除容器)发送到 KIE Server 的服务器命令。支持的 KIE 服务器配置命令的完整列表位于 Red Hat Process Automation Manager 实例的 org.kie.server.api.commands 软件包中。

在 KIE Server REST API 中,您可以使用 org.kie.server.api.commands 命令作为 POST 请求到 http://SERVER:PORT/kie-server/services/rest/server/config 的请求正文。有关使用 KIE 服务器 REST API 的更多信息,请参阅 第 21 章 KIE 服务器 REST API 用于 KIE 容器和业务资产

在 KIE Server Java 客户端 API 中,您可以使用父 KieServicesClient Java 客户端中的对应方法作为 Java 应用中的嵌入式 API 请求。所有 KIE 服务器命令都由 Java 客户端 API 中提供的方法执行,因此您不需要在 Java 应用中嵌入实际的 KIE Server 命令。有关使用 KIE Server Java 客户端 API 的详情,请参考 第 22 章 KIE Server Java 客户端 API 用于 KIE 容器和业务资产

23.1. KIE 服务器和 KIE 容器命令示例

以下是您可以在 KIE Server 中使用 KIE Server REST API 或 Java 客户端 API 的 KIE Server 命令示例,用于 KIE 服务器中的与服务器相关的或容器相关操作:

  • GetServerInfoCommand
  • GetServerStateCommand
  • CreateContainerCommand
  • GetContainerInfoCommand
  • ListContainersCommand
  • CallContainerCommand
  • DisposeContainerCommand
  • GetScannerInfoCommand
  • UpdateScannerCommand
  • UpdateReleaseIdCommand

有关支持的 KIE 服务器配置和管理命令的完整列表,请查看 Red Hat Process Automation Manager 实例中的 org.kie.server.api.commands 软件包。

您可以单独运行 KIE Server 命令,也可以作为批处理 REST API 请求或批处理 Java API 请求一起运行:

批处理 REST API 请求以创建、调用和分离 KIE 容器(JSON)

{
  "commands": [
    {
      "create-container": {
        "container": {
          "status": "STARTED",
          "container-id": "command-script-container",
          "release-id": {
            "version": "1.0",
            "group-id": "com.redhat",
            "artifact-id": "Project1"
          }
        }
      }
    },
    {
      "call-container": {
        "payload": "{\n  \"commands\" : [ {\n    \"fire-all-rules\" : {\n      \"max\" : -1,\n      \"out-identifier\" : null\n    }\n  } ]\n}",
        "container-id": "command-script-container"
      }
    },
    {
      "dispose-container": {
        "container-id": "command-script-container"
      }
    }
  ]
}

批处理 Java API 请求以检索、分配和重新创建 KIE 容器

public void disposeAndCreateContainer() {
    System.out.println("== Disposing and creating containers ==");

    // Retrieve list of KIE containers
    List<KieContainerResource> kieContainers = kieServicesClient.listContainers().getResult().getContainers();
    if (kieContainers.size() == 0) {
        System.out.println("No containers available...");
        return;
    }

    // Dispose KIE container
    KieContainerResource container = kieContainers.get(0);
    String containerId = container.getContainerId();
    ServiceResponse<Void> responseDispose = kieServicesClient.disposeContainer(containerId);
    if (responseDispose.getType() == ResponseType.FAILURE) {
        System.out.println("Error disposing " + containerId + ". Message: ");
        System.out.println(responseDispose.getMsg());
        return;
    }
    System.out.println("Success Disposing container " + containerId);
    System.out.println("Trying to recreate the container...");

    // Re-create KIE container
    ServiceResponse<KieContainerResource> createResponse = kieServicesClient.createContainer(containerId, container);
    if(createResponse.getType() == ResponseType.FAILURE) {
        System.out.println("Error creating " + containerId + ". Message: ");
        System.out.println(responseDispose.getMsg());
        return;
    }
    System.out.println("Container recreated with success!");
}

本节中的每个命令都包括 KIE Server REST API 的 REST 请求正文示例(JSON),以及来自 KIE Server Java 客户端 API 的 KieServicesClient Java 客户端的嵌入式方法示例。

GetServerInfoCommand

返回有关此 KIE 服务器实例的信息。

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "get-server-info" : { }
  } ]
}

Java 客户端方法示例

KieServerInfo serverInfo = kieServicesClient.getServerInfo();

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Kie Server info",
      "result": {
        "kie-server-info": {
          "id": "default-kieserver",
          "version": "7.11.0.Final-redhat-00001",
          "name": "default-kieserver",
          "location": "http://localhost:8080/kie-server/services/rest/server",
          "capabilities": [
            "KieServer",
            "BRM",
            "BPM",
            "CaseMgmt",
            "BPM-UI",
            "BRP",
            "DMN",
            "Swagger"
          ],
          "messages": [
            {
              "severity": "INFO",
              "timestamp": {
                "java.util.Date": 1538502533321
              },
              "content": [
                "Server KieServerInfo{serverId='default-kieserver', version='7.11.0.Final-redhat-00001', name='default-kieserver', location='http://localhost:8080/kie-server/services/rest/server', capabilities=[KieServer, BRM, BPM, CaseMgmt, BPM-UI, BRP, DMN, Swagger], messages=null}started successfully at Tue Oct 02 13:48:53 EDT 2018"
              ]
            }
          ]
        }
      }
    }
  ]
}

GetServerStateCommand

返回有关此 KIE 服务器实例的当前状态和配置的信息。

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "get-server-state" : { }
  } ]
}

Java 客户端方法示例

KieServerStateInfo serverStateInfo = kieServicesClient.getServerState();

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Successfully loaded server state for server id default-kieserver",
      "result": {
        "kie-server-state-info": {
          "controller": [
            "http://localhost:8080/business-central/rest/controller"
          ],
          "config": {
            "config-items": [
              {
                "itemName": "org.kie.server.location",
                "itemValue": "http://localhost:8080/kie-server/services/rest/server",
                "itemType": "java.lang.String"
              },
              {
                "itemName": "org.kie.server.controller.user",
                "itemValue": "controllerUser",
                "itemType": "java.lang.String"
              },
              {
                "itemName": "org.kie.server.controller",
                "itemValue": "http://localhost:8080/business-central/rest/controller",
                "itemType": "java.lang.String"
              }
            ]
          },
          "containers": [
            {
              "container-id": "employee-rostering",
              "release-id": {
                "group-id": "employeerostering",
                "artifact-id": "employeerostering",
                "version": "1.0.0-SNAPSHOT"
              },
              "resolved-release-id": null,
              "status": "STARTED",
              "scanner": {
                "status": "STOPPED",
                "poll-interval": null
              },
              "config-items": [
                {
                  "itemName": "KBase",
                  "itemValue": "",
                  "itemType": "BPM"
                },
                {
                  "itemName": "KSession",
                  "itemValue": "",
                  "itemType": "BPM"
                },
                {
                  "itemName": "MergeMode",
                  "itemValue": "MERGE_COLLECTIONS",
                  "itemType": "BPM"
                },
                {
                  "itemName": "RuntimeStrategy",
                  "itemValue": "SINGLETON",
                  "itemType": "BPM"
                }
              ],
              "messages": [],
              "container-alias": "employeerostering"
            }
          ]
        }
      }
    }
  ]
}

CreateContainerCommand

在 KIE 服务器中创建 KIE 容器。

表 23.1. 命令属性

名称描述要求

container

包含 container-idrelease-id 数据(组 ID、工件 ID、版本)、状态,以及新 KIE 容器的任何其他组件

必填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "create-container" : {
      "container" : {
        "status" : null,
        "messages" : [ ],
        "container-id" : "command-script-container",
        "release-id" : {
          "version" : "1.0",
          "group-id" : "com.redhat",
          "artifact-id" : "Project1"
        },
        "config-items" : [ ]
      }
    }
  } ]
}

Java 客户端方法示例

ServiceResponse<KieContainerResource> response = kieServicesClient.createContainer("command-script-container", resource);

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Container command-script-container successfully deployed with module com.redhat:Project1:1.0.",
      "result": {
        "kie-container": {
          "container-id": "command-script-container",
          "release-id": {
            "version" : "1.0",
            "group-id" : "com.redhat",
            "artifact-id" : "Project1"
          },
          "resolved-release-id": {
            "version" : "1.0",
            "group-id" : "com.redhat",
            "artifact-id" : "Project1"
          },
          "status": "STARTED",
          "scanner": {
            "status": "DISPOSED",
            "poll-interval": null
          },
          "config-items": [],
          "messages": [
            {
              "severity": "INFO",
              "timestamp": {
                "java.util.Date": 1538762455510
              },
              "content": [
                "Container command-script-container successfully created with module com.redhat:Project1:1.0."
              ]
            }
          ],
          "container-alias": null
        }
      }
    }
  ]
}

GetContainerInfoCommand

返回 KIE 服务器中指定的 KIE 容器的信息。

表 23.2. 命令属性

名称描述要求

container-id

KIE 容器的 ID

必填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "get-container-info" : {
      "container-id" : "command-script-container"
    }
  } ]
}

Java 客户端方法示例

ServiceResponse<KieContainerResource> response = kieServicesClient.getContainerInfo("command-script-container");

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Info for container command-script-container",
      "result": {
        "kie-container": {
          "container-id": "command-script-container",
          "release-id": {
            "group-id": "com.redhat",
            "artifact-id": "Project1",
            "version": "1.0"
          },
          "resolved-release-id": {
            "group-id": "com.redhat",
            "artifact-id": "Project1",
            "version": "1.0"
          },
          "status": "STARTED",
          "scanner": {
            "status": "DISPOSED",
            "poll-interval": null
          },
          "config-items": [

          ],
          "container-alias": null
        }
      }
    }
  ]
}

ListContainersCommand

返回此 KIE 服务器实例中创建的 KIE 容器列表。

表 23.3. 命令属性

名称描述要求

kie-container-filter

包含 release-id-filtercontainer-status-filter 以及您要过滤结果的任何其他 KIE 容器属性的可选映射

选填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "list-containers" : {
      "kie-container-filter" : {
        "release-id-filter" : { },
        "container-status-filter" : {
          "accepted-status" : ["FAILED"]
        }
      }
    }
  } ]
}

Java 客户端方法示例

KieContainerResourceFilter filter = new KieContainerResourceFilter.Builder()
        .status(KieContainerStatus.FAILED)
        .build();

KieContainerResourceList containersList = kieServicesClient.listContainers(filter);

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "List of created containers",
      "result": {
        "kie-containers": {
          "kie-container": [
            {
              "container-id": "command-script-container",
              "release-id": {
                "group-id": "com.redhat",
                "artifact-id": "Project1",
                "version": "1.0"
              },
              "resolved-release-id": {
                "group-id": "com.redhat",
                "artifact-id": "Project1",
                "version": "1.0"
              },
              "status": "STARTED",
              "scanner": {
                "status": "STARTED",
                "poll-interval": 5000
              },
              "config-items": [
                {
                  "itemName": "RuntimeStrategy",
                  "itemValue": "SINGLETON",
                  "itemType": "java.lang.String"
                },
                {
                  "itemName": "MergeMode",
                  "itemValue": "MERGE_COLLECTIONS",
                  "itemType": "java.lang.String"
                },
                {
                  "itemName": "KBase",
                  "itemValue": "",
                  "itemType": "java.lang.String"
                },
                {
                  "itemName": "KSession",
                  "itemValue": "",
                  "itemType": "java.lang.String"
                }
              ],
              "messages": [
                {
                  "severity": "INFO",
                  "timestamp": {
                    "java.util.Date": 1538504619749
                  },
                  "content": [
                    "Container command-script-container successfully created with module com.redhat:Project1:1.0."
                  ]
                }
              ],
              "container-alias": null
            }
          ]
        }
      }
    }
  ]
}

CallContainerCommand

调用 KIE 容器并执行一个或多个运行时命令。有关 Red Hat Process Automation Manager 运行时命令的详情,请参考 第 24 章 Red Hat Process Automation Manager 中的运行时命令

表 23.4. 命令属性

名称描述要求

container-id

要调用的 KIE 容器的 ID

必填

payload

要在 KIE 容器上执行的 BatchExecutionCommand 打包程序中的一个或多个命令

必填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "call-container" : {
      "payload" : "{\n  \"lookup\" : \"defaultKieSession\",\n  \"commands\" : [ {\n    \"fire-all-rules\" : {\n      \"max\" : -1,\n      \"out-identifier\" : null\n    }\n  } ]\n}",
      "container-id" : "command-script-container"
    }
  } ]
}

Java 客户端方法示例

List<Command<?>> commands = new ArrayList<Command<?>>();
      BatchExecutionCommand batchExecution1 = commandsFactory.newBatchExecution(commands, "defaultKieSession");
      commands.add(commandsFactory.newFireAllRules());

      ServiceResponse<ExecutionResults> response1 = ruleClient.executeCommandsWithResults("command-script-container", batchExecution1);

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Container command-script-container successfully called.",
      "result": "{\n  \"results\" : [ ],\n  \"facts\" : [ ]\n}"
    }
  ]
}

DisposeContainerCommand

在 KIE 服务器中分离指定的 KIE 容器。

表 23.5. 命令属性

名称描述要求

container-id

要分离的 KIE 容器的 ID

必填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "dispose-container" : {
      "container-id" : "command-script-container"
    }
  } ]
}

Java 客户端方法示例

ServiceResponse<Void> response = kieServicesClient.disposeContainer("command-script-container");

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Container command-script-container successfully disposed.",
      "result": null
    }
  ]
}

GetScannerInfoCommand

如果适用,返回有关在指定 KIE 容器中用于自动更新的 KIE 扫描程序的信息。

表 23.6. 命令属性

名称描述要求

container-id

使用 KIE 扫描程序的 KIE 容器的 ID

必填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "get-scanner-info" : {
      "container-id" : "command-script-container"
    }
  } ]
}

Java 客户端方法示例

ServiceResponse<KieScannerResource> response = kieServicesClient.getScannerInfo("command-script-container");

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Scanner info successfully retrieved",
      "result": {
        "kie-scanner": {
          "status": "DISPOSED",
          "poll-interval": null
        }
      }
    }
  ]
}

UpdateScannerCommand

启动或停止 KIE 扫描程序来控制为更新的 KIE 容器部署的轮询。

注意

避免将 KIE 扫描程序与业务流程一起使用。将 KIE 扫描程序与进程搭配使用可能会导致无法预见的更新,然后在更改与正在运行的进程实例不兼容时导致长时间运行的进程出现错误。

表 23.7. 命令属性

名称描述要求

container-id

使用 KIE 扫描程序的 KIE 容器的 ID

必填

status

要在 KIE 扫描程序上设置的状态(STARTEDSTOPPED)

必填

poll-interval

允许以毫秒为单位的轮询持续时间

仅在启动扫描程序时才需要

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "update-scanner" : {
      "scanner" : {
        "status" : "STARTED",
        "poll-interval" : 10000
      },
      "container-id" : "command-script-container"
    }
  } ]
}

Java 客户端方法示例

KieScannerResource scannerResource = new KieScannerResource();
scannerResource.setPollInterval(10000);
scannerResource.setStatus(KieScannerStatus. STARTED);

ServiceResponse<KieScannerResource> response = kieServicesClient.updateScanner("command-script-container", scannerResource);

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Kie scanner successfully created.",
      "result": {
        "kie-scanner": {
          "status": "STARTED",
          "poll-interval": 10000
        }
      }
    }
  ]
}

UpdateReleaseIdCommand

更新指定 KIE 容器的发行版本 ID 数据(组 ID、工件 ID、版本)。

表 23.8. 命令属性

名称描述要求

container-id

要更新的 KIE 容器的 ID

必填

releaseId

更新了要应用到 KIE 容器的 GAV (组 ID、工件 ID、版本)数据

必填

REST 请求正文(JSON)示例

{
  "commands" : [ {
    "update-release-id" : {
      "releaseId" : {
        "version" : "1.1",
        "group-id" : "com.redhat",
        "artifact-id" : "Project1"
      },
      "container-id" : "command-script-container"
    }
  } ]
}

Java 客户端方法示例

ServiceResponse<ReleaseId> response = kieServicesClient.updateReleaseId("command-script-container", "com.redhat:Project1:1.1");

服务器响应示例(JSON)

{
  "response": [
    {
      "type": "SUCCESS",
      "msg": "Release id successfully updated",
      "result": {
        "release-id": {
          "group-id": "com.redhat",
          "artifact-id": "Project1",
          "version": "1.1"
        }
      }
    }
  ]
}