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());
}
}
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);
}
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);
}
Aggregations