22.3. 带有 KIE 服务器 Java 客户端 API 的请求示例


以下是 KIE 服务器 Java 客户端 API 请求示例,用于与 KIE 服务器基本交互。有关可用 KIE Server Java 客户端的完整列表,请从红帽客户门户网站下载 Red Hat Process Automation Manager 7.11.0 源分发,并进入 ~/rhpam-7.11.0-sources/src/droolsjbpm-integration-$VERSION/kie-server-parent/kie-server-remote/kie-server-client/src/main/java/java/org/kie/server/clienthttps://access.redhat.com/jbossnetwork/restricted/listSoftware.html

列出 KIE 服务器功能

您可以使用 org.kie.server.api.model.KieServerInfo 对象来识别服务器功能。KieServicesClient 客户端需要服务器功能信息才能正确生成服务客户端。您可以在 KieServicesConfiguration 中全局指定功能;否则会自动从 KIE 服务器检索它们。

返回 KIE 服务器功能的请求示例

public void listCapabilities() {

  KieServerInfo serverInfo = kieServicesClient.getServerInfo().getResult();
  System.out.print("Server capabilities:");

  for (String capability : serverInfo.getCapabilities()) {
    System.out.print(" " + capability);
  }

  System.out.println();
}

列出 KIE 服务器中的 KIE 容器

KIE 容器由 org.kie.server.api.model.KieContainerResource 对象表示。资源列表由 org.kie.server.api.model.KieContainerResourceList 对象表示。

从 KIE 服务器返回 KIE 容器的请求示例

public void listContainers() {
    KieContainerResourceList containersList = kieServicesClient.listContainers().getResult();
    List<KieContainerResource> kieContainers = containersList.getContainers();
    System.out.println("Available containers: ");
    for (KieContainerResource container : kieContainers) {
        System.out.println("\t" + container.getContainerId() + " (" + container.getReleaseId() + ")");
    }
}

您可以使用 org.kie.server.api.model.KieContainerResourceFilter 类的实例过滤 KIE 容器结果,该实例传递给 org.kie.server.client.KieServicesClient.listContainers () 方法。

按发行版本 ID 和状态返回 KIE 容器的请求示例

public void listContainersWithFilter() {

    // Filter containers by releaseId "org.example:container:1.0.0.Final" and status FAILED
    KieContainerResourceFilter filter = new KieContainerResourceFilter.Builder()
            .releaseId("org.example", "container", "1.0.0.Final")
            .status(KieContainerStatus.FAILED)
            .build();

    // Using previously created KieServicesClient
    KieContainerResourceList containersList = kieServicesClient.listContainers(filter).getResult();
    List<KieContainerResource> kieContainers = containersList.getContainers();

    System.out.println("Available containers: ");

    for (KieContainerResource container : kieContainers) {
        System.out.println("\t" + container.getContainerId() + " (" + container.getReleaseId() + ")");
    }
}

在 KIE 服务器中创建并分离 KIE 容器

您可以使用 KieServicesClient 客户端中的 createContainerdisposeContainer 方法来忽略并创建 KIE 容器。在本例中,当您分离容器时,ServiceResponse 会返回 Void 响应。在创建容器时,ServiceResponse 会返回 KieContainerResource 对象。

取消和重新创建 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 服务器中执行运行时命令

Red Hat Process Automation Manager 支持您可以发送到 KIE 服务器的运行时命令,以获取与资产相关的操作,如在 KIE 会话中插入或重新遍历对象或触发所有规则。支持的运行时命令的完整列表位于 Red Hat Process Automation Manager 实例的 org.drools.core.command.runtime 软件包中。

您可以使用 org.kie.api.command.KieCommands 类插入命令,并使用 org.kie.api.KieServices.get ().getCommands () 来实例化 KieCommands 类。如果要添加多个命令,请使用 BatchExecutionCommand 打包程序。

插入对象并触发所有规则的请求示例

import org.kie.api.command.Command;
import org.kie.api.command.KieCommands;
import org.kie.server.api.model.ServiceResponse;
import org.kie.server.client.RuleServicesClient;
import org.kie.server.client.KieServicesClient;
import org.kie.api.KieServices;

import java.util.Arrays;

...

public void executeCommands() {

  String containerId = "hello";
  System.out.println("== Sending commands to the server ==");
  RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
  KieCommands commandsFactory = KieServices.Factory.get().getCommands();

  Command<?> insert = commandsFactory.newInsert("Some String OBJ");
  Command<?> fireAllRules = commandsFactory.newFireAllRules();
  Command<?> batchCommand = commandsFactory.newBatchExecution(Arrays.asList(insert, fireAllRules));

  ServiceResponse<String> executeResponse = rulesClient.executeCommands(containerId, batchCommand);

  if(executeResponse.getType() == ResponseType.SUCCESS) {
    System.out.println("Commands executed with success! Response: ");
    System.out.println(executeResponse.getResult());
  } else {
    System.out.println("Error executing rules. Message: ");
    System.out.println(executeResponse.getMsg());
  }
}

注意

在集群环境中的客户端和特定 KIE 服务器容器之间的对话是由唯一的 对话ID 进行保护的。对话ID 使用 X-KIE-ConversationId REST 标头进行传输。如果更新容器,请取消设置之前的 dialogID。使用 KieServiesClient.completeConversation () 取消 Java API 的 dialog ID

列出 KIE 容器中可用的进程

您可以使用 QueryServicesClient 客户端列出可用的进程定义。QueryServicesClient 方法使用分页,因此除了您进行的查询外,还必须提供当前页面和每个页面的结果数。在本例中,查询从页面 0 开始,并列出前 1000 个结果。

列出 KIE 服务器中处理进程的请求示例

public void listProcesses() {
    System.out.println("== Listing Business Processes ==");
    QueryServicesClient queryClient = kieServicesClient.getServicesClient(QueryServicesClient.class);
    List<ProcessDefinition> findProcessesByContainerId = queryClient.findProcessesByContainerId("rewards", 0, 1000);
    for (ProcessDefinition def : findProcessesByContainerId) {
        System.out.println(def.getName() + " - " + def.getId() + " v" + def.getVersion());
    }
}

在 KIE 容器中启动批准过程

您可以使用 ProcessServicesClient 客户端启动启动过程。使用 addExtraClasses () 方法,确保您需要的任何自定义类添加到 KieServicesConfiguration 对象中。

启动业务进程的请求示例

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.client.KieServicesClient;
import org.kie.server.client.KieServicesConfiguration;
import org.kie.server.client.KieServicesFactory;
import org.kie.server.client.ProcessServicesClient;
...

public static void startProcess() {

  //Client configuration setup
  KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(SERVER_URL, LOGIN, PASSWORD);

  //Add custom classes, such as Obj.class, to the configuration
  Set<Class<?>> extraClassList = new HashSet<Class<?>>();
  extraClassList.add(Obj.class);
  config.addExtraClasses(extraClassList);
  config.setMarshallingFormat(MarshallingFormat.JSON);

  // ProcessServicesClient setup
  KieServicesClient client = KieServicesFactory.newKieServicesClient(config);
  ProcessServicesClient processServicesClient = client.getServicesClient(ProcessServicesClient.class);

  // Create an instance of the custom class
  Obj obj = new Obj();
  obj.setOk("ok");

  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("test", obj);

  // Start the process with custom class
  processServicesClient.startProcess(CONTAINER, processId, variables);
}

运行自定义查询

您可以使用 QueryServicesClient 客户端的 QueryDefinition 对象在 KIE 服务器中注册和执行自定义查询。

在 KIE 服务器中注册和执行自定义查询的请求示例

// Client setup
KieServicesConfiguration conf = KieServicesFactory.newRestConfiguration(SERVER_URL, LOGIN, PASSWORD);
KieServicesClient client = KieServicesFactory.newKieServicesClient(conf);

// Get the QueryServicesClient
QueryServicesClient queryClient = client.getServicesClient(QueryServicesClient.class);

// Build the query
QueryDefinition queryDefinition = QueryDefinition.builder().name(QUERY_NAME)
        .expression("select * from Task t")
        .source("java:jboss/datasources/ExampleDS")
        .target("TASK").build();

// Specify that two queries cannot have the same name
queryClient.unregisterQuery(QUERY_NAME);

// Register the query
queryClient.registerQuery(queryDefinition);

// Execute the query with parameters: query name, mapping type (to map the fields to an object), page number, page size, and return type
List<TaskInstance> query = queryClient.query(QUERY_NAME, QueryServicesClient.QUERY_MAP_TASK, 0, 100, TaskInstance.class);

// Read the result
for (TaskInstance taskInstance : query) {
    System.out.println(taskInstance);
}

在本例中,目标 指示查询服务应用默认过滤器。或者,您可以手动设置过滤器参数。Target 类支持以下值:

public enum Target {
    PROCESS,
    TASK,
    BA_TASK,
    PO_TASK,
    JOBS,
    CUSTOM;
}
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.