Search in sources :

Example 1 with CommandExecutor

use of org.kie.api.runtime.CommandExecutor in project droolsjbpm-integration by kiegroup.

the class KieContainerCommandServiceImpl method callContainer.

protected ServiceResponse<ExecutionResults> callContainer(String containerId, String payload, MarshallingFormat marshallingFormat, String classType, boolean marshallResponse) {
    if (payload == null) {
        return new ServiceResponse<ExecutionResults>(ServiceResponse.ResponseType.FAILURE, "Error calling container " + containerId + ". Empty payload. ");
    }
    try {
        KieContainerInstanceImpl kci = (KieContainerInstanceImpl) context.getContainer(containerId, ContainerLocatorProvider.get().getLocator());
        // call do dispose() is executed.
        if (kci != null && kci.getKieContainer() != null) {
            String sessionId = null;
            // this is a weak way of finding the lookup, but it is the same used in kie-camel. Will keep it for now.
            Matcher m = LOOKUP.matcher(payload);
            if (m.find()) {
                sessionId = m.group(1);
            }
            // find the session
            CommandExecutor ks = null;
            if (sessionId != null) {
                ks = context.getKieSessionLookupManager().lookup(sessionId, kci, context);
            } else {
                // if no session ID is defined, then use default stateful/stateless ksession.
                ks = KieServerUtils.getDefaultKieSession(kci);
            }
            context.getKieSessionLookupManager().postLookup(sessionId, kci, ks, context);
            if (ks != null) {
                Class<? extends Command> type = BatchExecutionCommandImpl.class;
                if (classType != null && !classType.isEmpty()) {
                    type = (Class<? extends Command>) kci.getKieContainer().getClassLoader().loadClass(classType);
                }
                Command<?> cmd = kci.getMarshaller(marshallingFormat).unmarshall(payload, type);
                if (cmd == null) {
                    return new ServiceResponse<ExecutionResults>(ServiceResponse.ResponseType.FAILURE, "Body of in message not of the expected type '" + Command.class.getName() + "'");
                }
                if (!(cmd instanceof BatchExecutionCommandImpl)) {
                    cmd = new BatchExecutionCommandImpl(Arrays.asList(new ExecutableCommand<?>[] { (ExecutableCommand<?>) cmd }));
                }
                ExecutionResults results = ks.execute((BatchExecutionCommandImpl) cmd);
                if (marshallResponse) {
                    Marshaller marshaller = kci.getMarshaller(marshallingFormat);
                    String result = marshaller.marshall(results);
                    return new ServiceResponse(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " successfully called.", result);
                } else {
                    return new ServiceResponse<ExecutionResults>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " successfully called.", results);
                }
            } else {
                return new ServiceResponse<ExecutionResults>(ServiceResponse.ResponseType.FAILURE, "Session '" + sessionId + "' not found on container '" + containerId + "'.");
            }
        } else {
            return new ServiceResponse<ExecutionResults>(ServiceResponse.ResponseType.FAILURE, "Container " + containerId + " is not instantiated.");
        }
    } catch (Exception e) {
        logger.error("Error calling container '" + containerId + "'", e);
        return new ServiceResponse<ExecutionResults>(ServiceResponse.ResponseType.FAILURE, "Error calling container " + containerId + ": " + e.getClass().getName() + ": " + e.getMessage());
    }
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) Matcher(java.util.regex.Matcher) ExecutionResults(org.kie.api.runtime.ExecutionResults) CommandExecutor(org.kie.api.runtime.CommandExecutor) ServiceResponse(org.kie.server.api.model.ServiceResponse) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) UpdateReleaseIdCommand(org.kie.server.api.commands.UpdateReleaseIdCommand) CreateContainerCommand(org.kie.server.api.commands.CreateContainerCommand) ExecutableCommand(org.kie.api.command.ExecutableCommand) UpdateScannerCommand(org.kie.server.api.commands.UpdateScannerCommand) GetScannerInfoCommand(org.kie.server.api.commands.GetScannerInfoCommand) ActivateContainerCommand(org.kie.server.api.commands.ActivateContainerCommand) CallContainerCommand(org.kie.server.api.commands.CallContainerCommand) Command(org.kie.api.command.Command) GetReleaseIdCommand(org.kie.server.api.commands.GetReleaseIdCommand) ListContainersCommand(org.kie.server.api.commands.ListContainersCommand) KieServerCommand(org.kie.server.api.model.KieServerCommand) DeactivateContainerCommand(org.kie.server.api.commands.DeactivateContainerCommand) GetServerInfoCommand(org.kie.server.api.commands.GetServerInfoCommand) GetServerStateCommand(org.kie.server.api.commands.GetServerStateCommand) DisposeContainerCommand(org.kie.server.api.commands.DisposeContainerCommand) GetContainerInfoCommand(org.kie.server.api.commands.GetContainerInfoCommand)

Example 2 with CommandExecutor

use of org.kie.api.runtime.CommandExecutor in project droolsjbpm-integration by kiegroup.

the class RulesExecutionService method call.

public ExecutionResults call(KieContainerInstance kci, BatchExecutionCommand executionCommand) {
    BatchExecutionCommandImpl command = (BatchExecutionCommandImpl) executionCommand;
    if (kci != null && kci.getKieContainer() != null) {
        // find the session
        CommandExecutor ks = null;
        if (command.getLookup() != null) {
            ks = context.getKieSessionLookupManager().lookup(command.getLookup(), kci, context);
        } else {
            // if no session ID is defined, then use default stateful/stateless ksession.
            ks = KieServerUtils.getDefaultKieSession((KieContainerInstanceImpl) kci);
        }
        context.getKieSessionLookupManager().postLookup(command.getLookup(), kci, ks, context);
        if (ks != null) {
            applyListeners(ks);
            ExecutionResults results = ks.execute(command);
            return results;
        } else {
            throw new IllegalStateException("Session '" + command.getLookup() + "' not found on container '" + kci.getContainerId() + "'.");
        }
    }
    throw new IllegalStateException("Unable to execute command " + command);
}
Also used : BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) ExecutionResults(org.kie.api.runtime.ExecutionResults) CommandExecutor(org.kie.api.runtime.CommandExecutor) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl)

Example 3 with CommandExecutor

use of org.kie.api.runtime.CommandExecutor in project droolsjbpm-integration by kiegroup.

the class KieExecuteProducer method process.

public void process(Exchange exchange) throws Exception {
    KieEmbeddedEndpoint ke = (KieEmbeddedEndpoint) getEndpoint();
    Command<?> cmd = exchange.getIn().getBody(ExecutableCommand.class);
    if (cmd == null) {
        throw new RuntimeCamelException("Body of in message not of the expected type 'org.kie.api.command.Command' for uri" + ke.getEndpointUri());
    }
    if (!(cmd instanceof BatchExecutionCommandImpl)) {
        cmd = new BatchExecutionCommandImpl(Collections.singletonList(cmd));
    }
    CommandExecutor exec;
    ExecutionNodePipelineContextImpl droolsContext = exchange.getProperty("kie-context", ExecutionNodePipelineContextImpl.class);
    if (droolsContext != null) {
        exec = droolsContext.getCommandExecutor();
    } else {
        exec = ke.getExecutor();
        if (exec == null) {
            String lookup = exchange.getIn().getHeader(KieComponent.KIE_LOOKUP, String.class);
            if (StringUtils.isEmpty(lookup) && (cmd instanceof BatchExecutionCommandImpl)) {
                lookup = ((BatchExecutionCommandImpl) cmd).getLookup();
            }
            if (!StringUtils.isEmpty(lookup)) {
                exec = ke.getComponent().getCamelContext().getRegistry().lookup(lookup, CommandExecutor.class);
                if (exec == null) {
                    throw new RuntimeException("ExecutionNode is unable to find ksession=" + lookup + " for uri" + ke.getEndpointUri());
                }
            } else {
                throw new RuntimeException("No ExecutionNode, unable to find ksession=" + lookup + " for uri" + ke.getEndpointUri());
            }
        }
    }
    if (exec == null) {
        throw new RuntimeException("No defined ksession for uri " + ke.getEndpointUri());
    }
    ExecutionResults results = exec.execute((BatchExecutionCommandImpl) cmd);
    exchange.getIn().setBody(results);
}
Also used : BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) ExecutionResults(org.kie.api.runtime.ExecutionResults) CommandExecutor(org.kie.api.runtime.CommandExecutor) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Aggregations

BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)3 CommandExecutor (org.kie.api.runtime.CommandExecutor)3 ExecutionResults (org.kie.api.runtime.ExecutionResults)3 Matcher (java.util.regex.Matcher)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 Command (org.kie.api.command.Command)1 ExecutableCommand (org.kie.api.command.ExecutableCommand)1 ActivateContainerCommand (org.kie.server.api.commands.ActivateContainerCommand)1 CallContainerCommand (org.kie.server.api.commands.CallContainerCommand)1 CreateContainerCommand (org.kie.server.api.commands.CreateContainerCommand)1 DeactivateContainerCommand (org.kie.server.api.commands.DeactivateContainerCommand)1 DisposeContainerCommand (org.kie.server.api.commands.DisposeContainerCommand)1 GetContainerInfoCommand (org.kie.server.api.commands.GetContainerInfoCommand)1 GetReleaseIdCommand (org.kie.server.api.commands.GetReleaseIdCommand)1 GetScannerInfoCommand (org.kie.server.api.commands.GetScannerInfoCommand)1 GetServerInfoCommand (org.kie.server.api.commands.GetServerInfoCommand)1 GetServerStateCommand (org.kie.server.api.commands.GetServerStateCommand)1 ListContainersCommand (org.kie.server.api.commands.ListContainersCommand)1 UpdateReleaseIdCommand (org.kie.server.api.commands.UpdateReleaseIdCommand)1 UpdateScannerCommand (org.kie.server.api.commands.UpdateScannerCommand)1