Search in sources :

Example 6 with ServiceResponsesList

use of org.kie.server.api.model.ServiceResponsesList in project droolsjbpm-integration by kiegroup.

the class JmsResponseHandlerIntegrationTest method testStartProcessUseOfAsyncResponseHandler.

@Test
public void testStartProcessUseOfAsyncResponseHandler() throws Exception {
    ResponseCallback callback = new BlockingResponseCallback(null);
    testStartProcessResponseHandler(new AsyncResponseHandler(callback));
    // now let's check if response has arrived
    ServiceResponsesList response = callback.get();
    assertThat(response).isNotNull();
    assertThat(response.getResponses()).isNotNull().hasSize(1);
    KieServerAssert.assertSuccess(response.getResponses().get(0));
    ServiceResponse<? extends Object> serviceResponse = response.getResponses().get(0);
    Object result = serviceResponse.getResult();
    assertThat(result).isNotNull();
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) AsyncResponseHandler(org.kie.server.client.jms.AsyncResponseHandler) BlockingResponseCallback(org.kie.server.client.jms.BlockingResponseCallback) ResponseCallback(org.kie.server.client.jms.ResponseCallback) BlockingResponseCallback(org.kie.server.client.jms.BlockingResponseCallback) Test(org.junit.Test) JbpmKieServerBaseIntegrationTest(org.kie.server.integrationtests.jbpm.JbpmKieServerBaseIntegrationTest)

Example 7 with ServiceResponsesList

use of org.kie.server.api.model.ServiceResponsesList in project droolsjbpm-integration by kiegroup.

the class AbstractKieServicesClientImpl method executeJmsCommand.

protected ServiceResponsesList executeJmsCommand(CommandScript command, String classType, String targetCapability, String containerId) {
    ConnectionFactory factory = config.getConnectionFactory();
    Queue sendQueue = config.getRequestQueue();
    Queue responseQueue = config.getResponseQueue();
    Connection connection = null;
    Session session = null;
    ServiceResponsesList cmdResponse = null;
    String corrId = UUID.randomUUID().toString();
    String selector = "JMSCorrelationID = '" + corrId + "'";
    try {
        // setup
        MessageProducer producer;
        try {
            if (config.getPassword() != null) {
                connection = factory.createConnection(config.getUserName(), config.getPassword());
            } else {
                connection = factory.createConnection();
            }
            session = connection.createSession(config.isJmsTransactional(), Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(sendQueue);
            connection.start();
        } catch (JMSException jmse) {
            throw new KieServicesException("Unable to setup a JMS connection.", jmse);
        }
        // Create msg
        TextMessage textMsg;
        try {
            // serialize request
            String xmlStr = marshaller.marshall(command);
            logger.debug("Message content to be sent '{}'", xmlStr);
            textMsg = session.createTextMessage(xmlStr);
            // set properties
            // 1. corr id
            textMsg.setJMSCorrelationID(corrId);
            // 2. serialization info
            textMsg.setIntProperty(JMSConstants.SERIALIZATION_FORMAT_PROPERTY_NAME, config.getMarshallingFormat().getId());
            textMsg.setIntProperty(JMSConstants.INTERACTION_PATTERN_PROPERTY_NAME, responseHandler.getInteractionPattern());
            if (classType != null) {
                textMsg.setStringProperty(JMSConstants.CLASS_TYPE_PROPERTY_NAME, classType);
            }
            if (targetCapability != null) {
                textMsg.setStringProperty(JMSConstants.TARGET_CAPABILITY_PROPERTY_NAME, targetCapability);
            }
            textMsg.setStringProperty(JMSConstants.USER_PROPERTY_NAME, config.getUserName());
            textMsg.setStringProperty(JMSConstants.PASSWRD_PROPERTY_NAME, config.getPassword());
            if (containerId != null) {
                textMsg.setStringProperty(JMSConstants.CONTAINER_ID_PROPERTY_NAME, containerId);
            }
            if (owner.getConversationId() != null) {
                textMsg.setStringProperty(JMSConstants.CONVERSATION_ID_PROPERTY_NAME, owner.getConversationId());
            }
            if (config.getHeaders() != null) {
                for (Map.Entry<String, String> header : config.getHeaders().entrySet()) {
                    logger.debug("Adding additional property {} value {}", header.getKey(), header.getValue());
                    textMsg.setStringProperty(header.getKey(), header.getValue());
                }
            }
            // send
            producer.send(textMsg);
        } catch (JMSException jmse) {
            throw new KieServicesException("Unable to send a JMS message.", jmse);
        } finally {
            if (producer != null) {
                try {
                    producer.close();
                } catch (JMSException jmse) {
                    logger.warn("Unable to close producer!", jmse);
                }
            }
        }
        // receive
        cmdResponse = responseHandler.handleResponse(selector, connection, session, responseQueue, config, marshaller, owner);
        return cmdResponse;
    } finally {
        responseHandler.dispose(connection, session);
    }
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) HashMap(java.util.HashMap) Map(java.util.Map) KieServicesException(org.kie.server.api.exception.KieServicesException) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 8 with ServiceResponsesList

use of org.kie.server.api.model.ServiceResponsesList in project droolsjbpm-integration by kiegroup.

the class OptaplannerCommandServiceImpl method executeScript.

@Override
public ServiceResponsesList executeScript(CommandScript commands, MarshallingFormat marshallingFormat, String classType) {
    List<ServiceResponse<?>> responses = new ArrayList<>();
    for (KieServerCommand command : commands.getCommands()) {
        try {
            ServiceResponse<?> response;
            logger.debug("About to execute command: {}", command);
            if (command instanceof CreateSolverCommand) {
                CreateSolverCommand createSolverCommand = (CreateSolverCommand) command;
                String containerId = context.getContainerId(createSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                SolverInstance instance = new SolverInstance();
                instance.setContainerId(containerId);
                instance.setSolverId(createSolverCommand.getSolverId());
                instance.setSolverConfigFile(createSolverCommand.getSolverConfigFile());
                response = solverService.createSolver(containerId, createSolverCommand.getSolverId(), instance);
            } else if (command instanceof GetSolversCommand) {
                GetSolversCommand getSolversCommand = (GetSolversCommand) command;
                String containerId = context.getContainerId(getSolversCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolvers(containerId);
            } else if (command instanceof GetSolverCommand) {
                GetSolverCommand getSolverCommand = (GetSolverCommand) command;
                String containerId = context.getContainerId(getSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolver(containerId, getSolverCommand.getSolverId());
            } else if (command instanceof GetSolverWithBestSolutionCommand) {
                GetSolverWithBestSolutionCommand getSolverWithBestSolutionCommand = (GetSolverWithBestSolutionCommand) command;
                String containerId = context.getContainerId(getSolverWithBestSolutionCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolverWithBestSolution(containerId, getSolverWithBestSolutionCommand.getSolverId());
            } else if (command instanceof SolvePlanningProblemCommand) {
                SolvePlanningProblemCommand solvePlanningProblemCommand = (SolvePlanningProblemCommand) command;
                String containerId = context.getContainerId(solvePlanningProblemCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                KieContainerInstanceImpl kc = context.getContainer(containerId);
                Marshaller marshaller = kc.getMarshaller(marshallingFormat);
                Object planningProblem = marshaller.unmarshall(solvePlanningProblemCommand.getPlanningProblem(), Object.class);
                response = solverService.solvePlanningProblem(containerId, solvePlanningProblemCommand.getSolverId(), planningProblem);
            } else if (command instanceof TerminateSolverEarlyCommand) {
                TerminateSolverEarlyCommand terminateSolverEarlyCommand = (TerminateSolverEarlyCommand) command;
                String containerId = context.getContainerId(terminateSolverEarlyCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.terminateSolverEarly(containerId, terminateSolverEarlyCommand.getSolverId());
            } else if (command instanceof AddProblemFactChangeCommand) {
                AddProblemFactChangeCommand addProblemFactChangeCommand = (AddProblemFactChangeCommand) command;
                String containerId = context.getContainerId(addProblemFactChangeCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.addProblemFactChanges(containerId, addProblemFactChangeCommand.getSolverId(), addProblemFactChangeCommand.getProblemFactChange());
            } else if (command instanceof AddProblemFactChangesCommand) {
                AddProblemFactChangesCommand addProblemFactChangesCommand = (AddProblemFactChangesCommand) command;
                String containerId = context.getContainerId(addProblemFactChangesCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.addProblemFactChanges(containerId, addProblemFactChangesCommand.getSolverId(), addProblemFactChangesCommand.getProblemFactChanges());
            } else if (command instanceof IsEveryProblemFactChangeProcessedCommand) {
                IsEveryProblemFactChangeProcessedCommand isEveryProblemFactChangeProcessedCommand = (IsEveryProblemFactChangeProcessedCommand) command;
                String containerId = context.getContainerId(isEveryProblemFactChangeProcessedCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                ServiceResponse<Boolean> everyProblemFactChangeProcessedResponse = solverService.isEveryProblemFactChangeProcessed(containerId, isEveryProblemFactChangeProcessedCommand.getSolverId());
                if (marshallingFormat.equals(MarshallingFormat.JAXB)) {
                    Object wrappedResult = ModelWrapper.wrap(everyProblemFactChangeProcessedResponse.getResult());
                    response = new ServiceResponse<>(everyProblemFactChangeProcessedResponse.getType(), everyProblemFactChangeProcessedResponse.getMsg(), wrappedResult);
                } else {
                    response = everyProblemFactChangeProcessedResponse;
                }
            } else if (command instanceof DisposeSolverCommand) {
                DisposeSolverCommand disposeSolverCommand = (DisposeSolverCommand) command;
                String containerId = context.getContainerId(disposeSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.disposeSolver(containerId, disposeSolverCommand.getSolverId());
            } else {
                throw new IllegalStateException("Unsupported command: " + command);
            }
            logger.debug("Service returned response {}", response);
            // return successful result
            responses.add(response);
        } catch (Throwable e) {
            logger.error("Error while processing {} command", command, e);
            // return failure result
            responses.add(new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, e.getMessage()));
        }
    }
    logger.debug("About to return responses '{}'", responses);
    return new ServiceResponsesList(responses);
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) KieServerCommand(org.kie.server.api.model.KieServerCommand) ArrayList(java.util.ArrayList) TerminateSolverEarlyCommand(org.kie.server.api.commands.optaplanner.TerminateSolverEarlyCommand) SolverInstance(org.kie.server.api.model.instance.SolverInstance) ServiceResponse(org.kie.server.api.model.ServiceResponse) AddProblemFactChangesCommand(org.kie.server.api.commands.optaplanner.AddProblemFactChangesCommand) IsEveryProblemFactChangeProcessedCommand(org.kie.server.api.commands.optaplanner.IsEveryProblemFactChangeProcessedCommand) GetSolverCommand(org.kie.server.api.commands.optaplanner.GetSolverCommand) Marshaller(org.kie.server.api.marshalling.Marshaller) GetSolversCommand(org.kie.server.api.commands.optaplanner.GetSolversCommand) SolvePlanningProblemCommand(org.kie.server.api.commands.optaplanner.SolvePlanningProblemCommand) GetSolverWithBestSolutionCommand(org.kie.server.api.commands.optaplanner.GetSolverWithBestSolutionCommand) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) CreateSolverCommand(org.kie.server.api.commands.optaplanner.CreateSolverCommand) AddProblemFactChangeCommand(org.kie.server.api.commands.optaplanner.AddProblemFactChangeCommand) DisposeSolverCommand(org.kie.server.api.commands.optaplanner.DisposeSolverCommand)

Example 9 with ServiceResponsesList

use of org.kie.server.api.model.ServiceResponsesList in project droolsjbpm-integration by kiegroup.

the class WebSocketKieServerClient method getContainerInfo.

@Override
public ServiceResponse<KieContainerResource> getContainerInfo(String id) {
    CommandScript script = new CommandScript(Collections.singletonList((KieServerCommand) new GetContainerInfoCommand(id)));
    ServiceResponse<KieContainerResource> response = (ServiceResponse<KieContainerResource>) sendCommand(script, new WebSocketServiceResponse(true, (message) -> {
        ServiceResponsesList list = WebSocketUtils.unmarshal(message, ServiceResponsesList.class);
        return list.getResponses().get(0);
    })).getResponses().get(0);
    return response;
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) WebSocketServiceResponse(org.kie.server.controller.websocket.common.handlers.WebSocketServiceResponse) ServiceResponse(org.kie.server.api.model.ServiceResponse) KieServerCommand(org.kie.server.api.model.KieServerCommand) WebSocketServiceResponse(org.kie.server.controller.websocket.common.handlers.WebSocketServiceResponse) CommandScript(org.kie.server.api.commands.CommandScript) GetContainerInfoCommand(org.kie.server.api.commands.GetContainerInfoCommand) KieContainerResource(org.kie.server.api.model.KieContainerResource)

Example 10 with ServiceResponsesList

use of org.kie.server.api.model.ServiceResponsesList in project droolsjbpm-integration by kiegroup.

the class WebSocketKieServerClient method deactivateContainer.

@Override
public ServiceResponse<KieContainerResource> deactivateContainer(String id) {
    CommandScript script = new CommandScript(Collections.singletonList((KieServerCommand) new DeactivateContainerCommand(id)));
    ServiceResponse<KieContainerResource> response = (ServiceResponse<KieContainerResource>) sendCommandToAllSessions(script, new WebSocketServiceResponse(true, (message) -> {
        ServiceResponsesList list = WebSocketUtils.unmarshal(message, ServiceResponsesList.class);
        return list.getResponses().get(0);
    })).getResponses().get(0);
    return response;
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) WebSocketServiceResponse(org.kie.server.controller.websocket.common.handlers.WebSocketServiceResponse) ServiceResponse(org.kie.server.api.model.ServiceResponse) KieServerCommand(org.kie.server.api.model.KieServerCommand) WebSocketServiceResponse(org.kie.server.controller.websocket.common.handlers.WebSocketServiceResponse) CommandScript(org.kie.server.api.commands.CommandScript) DeactivateContainerCommand(org.kie.server.api.commands.DeactivateContainerCommand) KieContainerResource(org.kie.server.api.model.KieContainerResource)

Aggregations

ServiceResponsesList (org.kie.server.api.model.ServiceResponsesList)30 ServiceResponse (org.kie.server.api.model.ServiceResponse)23 KieServerCommand (org.kie.server.api.model.KieServerCommand)18 CommandScript (org.kie.server.api.commands.CommandScript)17 WebSocketServiceResponse (org.kie.server.controller.websocket.common.handlers.WebSocketServiceResponse)13 ArrayList (java.util.ArrayList)9 DescriptorCommand (org.kie.server.api.commands.DescriptorCommand)5 KieContainerResource (org.kie.server.api.model.KieContainerResource)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 JMSException (javax.jms.JMSException)4 CreateContainerCommand (org.kie.server.api.commands.CreateContainerCommand)4 DisposeContainerCommand (org.kie.server.api.commands.DisposeContainerCommand)4 Wrapped (org.kie.server.api.model.Wrapped)4 TextMessage (javax.jms.TextMessage)3 Test (org.junit.Test)3 UpdateReleaseIdCommand (org.kie.server.api.commands.UpdateReleaseIdCommand)3 UpdateScannerCommand (org.kie.server.api.commands.UpdateScannerCommand)3 KieServicesException (org.kie.server.api.exception.KieServicesException)3 ReleaseId (org.kie.server.api.model.ReleaseId)3 IOException (java.io.IOException)2