Search in sources :

Example 1 with KieContainerInstance

use of org.kie.server.services.api.KieContainerInstance in project kiegroup-examples by tkobayas.

the class MyKieServerWarmupEventListener method internalWarmupFireOnly.

private void internalWarmupFireOnly(KieContainerInstance containerInstance) {
    LOGGER.info("  internalWarmupFireOnly");
    KieContainer kieContainer = containerInstance.getKieContainer();
    Collection<String> kieBaseNames = kieContainer.getKieBaseNames();
    // warmup all kieSessions
    kieBaseNames.stream().flatMap(kieBaseName -> kieContainer.getKieSessionNamesInKieBase(kieBaseName).stream()).forEach(kieSessionName -> {
        LOGGER.info("warmup : kieSessionName = {}", kieSessionName);
        KieSessionType type = kieContainer.getKieSessionModel(kieSessionName).getType();
        if (type == KieSessionType.STATEFUL) {
            KieSession kieSession = kieContainer.newKieSession(kieSessionName);
            kieSession.fireAllRules();
            kieSession.dispose();
        } else {
            StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession(kieSessionName);
            final List<Command> cmds = new ArrayList<Command>();
            cmds.add(CommandFactory.newFireAllRules());
            BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
            final ExecutionResults batchResult = (ExecutionResults) statelessKieSession.execute(batchExecutionCommand);
        }
    });
}
Also used : Command(org.kie.api.command.Command) Logger(org.slf4j.Logger) Collection(java.util.Collection) ExecutionResults(org.kie.api.runtime.ExecutionResults) LoggerFactory(org.slf4j.LoggerFactory) KieContainer(org.kie.api.runtime.KieContainer) KieContainerResource(org.kie.server.api.model.KieContainerResource) CommandFactory(org.kie.internal.command.CommandFactory) Constructor(java.lang.reflect.Constructor) KieContainerInstance(org.kie.server.services.api.KieContainerInstance) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSessionType(org.kie.api.builder.model.KieSessionModel.KieSessionType) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) List(java.util.List) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Optional(java.util.Optional) KieServer(org.kie.server.services.api.KieServer) KieSession(org.kie.api.runtime.KieSession) KieServerEventListener(org.kie.server.services.api.KieServerEventListener) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSessionType(org.kie.api.builder.model.KieSessionModel.KieSessionType) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) KieContainer(org.kie.api.runtime.KieContainer)

Example 2 with KieContainerInstance

use of org.kie.server.services.api.KieContainerInstance in project kiegroup-examples by tkobayas.

the class MyKieServerWarmupEventListener method afterContainerStarted.

@Override
public void afterContainerStarted(KieServer kieServer, KieContainerInstance containerInstance) {
    // To set the KIE_WARMUP_LISTENER_STRATEGY value, see CreateContainerWithWarmupTrue.java in Ex-kie-server-client-7.48.0.Final-redhat-00006
    // If you comment-out this part, you can always trigger warm-up.
    KieContainerResource resource = containerInstance.getResource();
    Optional<KieServerConfigItem> optItem = resource.getConfigItems().stream().filter(item -> item.getName().equals(KIE_WARMUP_LISTENER_STRATEGY)).findFirst();
    if (!optItem.isPresent()) {
        LOGGER.info("{} is not enabled : ", KIE_WARMUP_LISTENER_STRATEGY);
        return;
    }
    String strategy = optItem.get().getValue();
    LOGGER.info("start warmup : " + strategy);
    long start = System.currentTimeMillis();
    try {
        switch(strategy) {
            case "internal-fire-only":
                internalWarmupFireOnly(containerInstance);
                break;
            case "internal-full":
                // in order to use internalWarmupFull, you need to edit the method to insert expected fact objects
                internalWarmupFull(containerInstance);
                break;
            default:
                break;
        }
    } catch (Exception e) {
        LOGGER.warn("warmup failed. You can ignore the error and use the deployed container", e);
    }
    LOGGER.info("afterContainerStarted : elapsed time = {}ms", System.currentTimeMillis() - start);
}
Also used : Command(org.kie.api.command.Command) Logger(org.slf4j.Logger) Collection(java.util.Collection) ExecutionResults(org.kie.api.runtime.ExecutionResults) LoggerFactory(org.slf4j.LoggerFactory) KieContainer(org.kie.api.runtime.KieContainer) KieContainerResource(org.kie.server.api.model.KieContainerResource) CommandFactory(org.kie.internal.command.CommandFactory) Constructor(java.lang.reflect.Constructor) KieContainerInstance(org.kie.server.services.api.KieContainerInstance) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieSessionType(org.kie.api.builder.model.KieSessionModel.KieSessionType) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) List(java.util.List) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Optional(java.util.Optional) KieServer(org.kie.server.services.api.KieServer) KieSession(org.kie.api.runtime.KieSession) KieServerEventListener(org.kie.server.services.api.KieServerEventListener) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) KieContainerResource(org.kie.server.api.model.KieContainerResource) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with KieContainerInstance

use of org.kie.server.services.api.KieContainerInstance in project droolsjbpm-integration by kiegroup.

the class AbstractKieServerImplTest method testHealthCheckFailedExtension.

@Test
public void testHealthCheckFailedExtension() {
    extensions.add(new KieServerExtension() {

        @Override
        public List<Message> healthCheck(boolean report) {
            List<Message> messages = KieServerExtension.super.healthCheck(report);
            messages.add(new Message(Severity.ERROR, "TEST extension is unhealthy"));
            return messages;
        }

        @Override
        public void updateContainer(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
        }

        @Override
        public boolean isUpdateContainerAllowed(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
            return false;
        }

        @Override
        public boolean isInitialized() {
            return true;
        }

        @Override
        public boolean isActive() {
            return true;
        }

        @Override
        public void init(KieServerImpl kieServer, KieServerRegistry registry) {
        }

        @Override
        public Integer getStartOrder() {
            return 10;
        }

        @Override
        public List<Object> getServices() {
            return null;
        }

        @Override
        public String getImplementedCapability() {
            return "TEST";
        }

        @Override
        public String getExtensionName() {
            return "TEST";
        }

        @Override
        public <T> T getAppComponents(Class<T> serviceType) {
            return null;
        }

        @Override
        public List<Object> getAppComponents(SupportedTransports type) {
            return null;
        }

        @Override
        public void disposeContainer(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
        }

        @Override
        public void destroy(KieServerImpl kieServer, KieServerRegistry registry) {
        }

        @Override
        public void createContainer(String id, KieContainerInstance kieContainerInstance, Map<String, Object> parameters) {
        }
    });
    kieServer.init();
    List<Message> healthMessages = kieServer.healthCheck(false);
    assertEquals(healthMessages.size(), 1);
    Message failedContainer = healthMessages.get(0);
    assertEquals(Severity.ERROR, failedContainer.getSeverity());
    assertEquals(1, failedContainer.getMessages().size());
    assertEquals("TEST extension is unhealthy", failedContainer.getMessages().iterator().next());
}
Also used : Message(org.kie.server.api.model.Message) KieServerExtension(org.kie.server.services.api.KieServerExtension) Matchers.anyString(org.mockito.Matchers.anyString) KieContainerInstance(org.kie.server.services.api.KieContainerInstance) SupportedTransports(org.kie.server.services.api.SupportedTransports) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) KieContainerResourceList(org.kie.server.api.model.KieContainerResourceList) List(java.util.List) ArrayList(java.util.ArrayList) ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) Test(org.junit.Test)

Example 4 with KieContainerInstance

use of org.kie.server.services.api.KieContainerInstance in project droolsjbpm-integration by kiegroup.

the class KieServerImpl method healthCheck.

public List<Message> healthCheck(boolean report) throws IllegalStateException {
    List<Message> healthMessages = new ArrayList<>();
    long start = System.currentTimeMillis();
    if (!isKieServerReady()) {
        healthMessages.add(new Message(Severity.ERROR, String.format("KIE Server '%s' is not ready to serve requests", KieServerEnvironment.getServerId())));
    }
    if (report) {
        List<String> mainInfo = new ArrayList<>();
        mainInfo.add(String.format("KIE Server '%s' is ready to serve requests %s", KieServerEnvironment.getServerId(), isKieServerReady()));
        mainInfo.add("Server is up for " + calculateUptime());
        Message header = new Message(Severity.INFO, mainInfo);
        healthMessages.add(header);
    }
    // first check of KIE Server's containers if any of them is in failed state
    for (KieContainerInstance container : getContainers()) {
        // RHPAM-1793 - relax health check in case of management is enabled.
        if (container.getStatus().equals(KieContainerStatus.FAILED) && !managementDisabled) {
            healthMessages.add(new Message(Severity.WARN, String.format("KIE Container '%s' is in FAILED state", container.getContainerId())));
        } else if (container.getStatus().equals(KieContainerStatus.FAILED) && managementDisabled) {
            healthMessages.add(new Message(Severity.ERROR, String.format("KIE Container '%s' is in FAILED state", container.getContainerId())));
        }
    }
    // next check all extensions for their health
    for (KieServerExtension extension : getServerExtensions()) {
        List<Message> extensionMessages = extension.healthCheck(report);
        healthMessages.addAll(extensionMessages);
    }
    if (report) {
        Message footer = new Message(Severity.INFO, "Health check done in " + (System.currentTimeMillis() - start) + " ms");
        healthMessages.add(footer);
    }
    return healthMessages;
}
Also used : Message(org.kie.server.api.model.Message) KieServerExtension(org.kie.server.services.api.KieServerExtension) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) KieContainerInstance(org.kie.server.services.api.KieContainerInstance)

Example 5 with KieContainerInstance

use of org.kie.server.services.api.KieContainerInstance in project droolsjbpm-integration by kiegroup.

the class MarshallerHelper method unmarshal.

public <T> T unmarshal(String containerId, String data, String marshallingFormat, Class<T> unmarshalType, ContainerLocator locator) {
    if (data == null || data.isEmpty()) {
        return null;
    }
    MarshallingFormat format = getFormat(marshallingFormat);
    KieContainerInstance containerInstance = registry.getContainer(containerId, locator);
    if (containerInstance == null || format == null) {
        throw new IllegalArgumentException("No container found for id " + containerId + " or unknown marshalling format " + marshallingFormat);
    }
    Marshaller marshaller = containerInstance.getMarshaller(format);
    if (marshaller == null) {
        throw new IllegalArgumentException("No marshaller found for format " + format);
    }
    Object instance = marshaller.unmarshall(data, unmarshalType, MarshallingFormat.buildParameters(marshallingFormat));
    if (instance instanceof Wrapped) {
        return (T) ((Wrapped) instance).unwrap();
    }
    return (T) instance;
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat) Wrapped(org.kie.server.api.model.Wrapped) KieContainerInstance(org.kie.server.services.api.KieContainerInstance)

Aggregations

KieContainerInstance (org.kie.server.services.api.KieContainerInstance)9 ArrayList (java.util.ArrayList)6 MarshallingFormat (org.kie.server.api.marshalling.MarshallingFormat)5 List (java.util.List)4 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 Command (org.kie.api.command.Command)4 ExecutionResults (org.kie.api.runtime.ExecutionResults)4 Marshaller (org.kie.server.api.marshalling.Marshaller)4 Constructor (java.lang.reflect.Constructor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Optional (java.util.Optional)3 KieContainer (org.kie.api.runtime.KieContainer)3 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3 CommandFactory (org.kie.internal.command.CommandFactory)3 KieContainerResource (org.kie.server.api.model.KieContainerResource)3 KieServerConfigItem (org.kie.server.api.model.KieServerConfigItem)3 KieServer (org.kie.server.services.api.KieServer)3 KieServerEventListener (org.kie.server.services.api.KieServerEventListener)3 KieServerExtension (org.kie.server.services.api.KieServerExtension)3 Collection (java.util.Collection)2