Search in sources :

Example 1 with KieServer

use of org.kie.server.services.api.KieServer 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 2 with KieServer

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

the class KeepLatestContainerOnlyPolicyTest method setUp.

@Before
public void setUp() {
    kieServer = mock(KieServer.class);
    when(kieServer.disposeContainer(anyString())).thenReturn(new ServiceResponse<>(ServiceResponse.ResponseType.NO_RESPONSE, "Success."));
    kieContainerOne = mock(InternalKieContainer.class);
    when(kieContainerOne.getReleaseId()).thenReturn(new ReleaseId("org.kie", "container", "1.0"));
    when(kieContainerOne.getContainerReleaseId()).thenReturn(new ReleaseId("org.kie", "container", "1.0"));
    kieContainerTwo = mock(InternalKieContainer.class);
    when(kieContainerTwo.getReleaseId()).thenReturn(new ReleaseId("org.kie", "container", "2.0"));
    when(kieContainerTwo.getContainerReleaseId()).thenReturn(new ReleaseId("org.kie", "container", "2.0"));
}
Also used : InternalKieContainer(org.drools.core.impl.InternalKieContainer) ReleaseId(org.kie.server.api.model.ReleaseId) KieServer(org.kie.server.services.api.KieServer) Before(org.junit.Before)

Example 3 with KieServer

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

the class KeepLatestContainerOnlyPolicy method apply.

@Override
public void apply(KieServerRegistry kieServerRegistry, KieServer kieServer) {
    DefaultRestControllerImpl controller = new DefaultRestControllerImpl(kieServerRegistry);
    List<String> containerAliases = kieServerRegistry.getContainerAliases();
    if (containerAliases.isEmpty()) {
        logger.debug("No containers found, quiting");
        return;
    }
    for (String alias : containerAliases) {
        List<KieContainerInstanceImpl> containerInstances = kieServerRegistry.getContainersForAlias(alias);
        if (containerInstances.isEmpty() || containerInstances.size() == 1) {
            logger.debug("Containers for alias {} are already on expected level (number of containers is {})", alias, containerInstances.size());
            continue;
        }
        String latestContainerId = LatestContainerLocator.get().locateContainer(alias, containerInstances);
        final Map<String, String> report = new HashMap<>();
        containerInstances.stream().filter(kci -> !kci.getContainerId().equals(latestContainerId)).filter(kci -> !kci.getStatus().equals(KieContainerStatus.CREATING)).forEach(kci -> {
            ServiceResponse<Void> response = kieServer.disposeContainer(kci.getContainerId());
            report.put(kci.getContainerId(), response.getType().toString());
            logger.debug("Dispose of container {} completed with {} message {}", kci.getContainerId(), response.getType().toString(), response.getMsg());
            if (response.getType().equals(ServiceResponse.ResponseType.SUCCESS)) {
                controller.stopContainer(kci.getContainerId());
            }
        });
        logger.info("KeepLatestContainerOnlyPolicy applied to {} successfully (report {})", alias, report);
    }
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ServiceResponse(org.kie.server.api.model.ServiceResponse) LatestContainerLocator(org.kie.server.services.impl.locator.LatestContainerLocator) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Map(java.util.Map) Policy(org.kie.server.services.api.Policy) KieServer(org.kie.server.services.api.KieServer) DefaultRestControllerImpl(org.kie.server.services.impl.controller.DefaultRestControllerImpl) KieContainerStatus(org.kie.server.api.model.KieContainerStatus) HashMap(java.util.HashMap) DefaultRestControllerImpl(org.kie.server.services.impl.controller.DefaultRestControllerImpl) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl)

Example 4 with KieServer

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

the class KieServerRouterEventListenerRetryTest method testRouterServerStoppedRetry.

@Test(timeout = 10000)
public void testRouterServerStoppedRetry() throws Exception {
    final CountDownLatch failureLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    int routerPort = findFreePort();
    client = new KieServerRouterEventListener("test-server", "http://localhost:8080/kie-server", "http://localhost:" + routerPort, 2);
    client.setObserver(new KieServerRouterEventListener.RouterConnectionObserver() {

        @Override
        public void onSuccess(String url) {
            successLatch.countDown();
        }

        @Override
        public void onFailure(String url) {
            failureLatch.countDown();
        }
    });
    wireMockServer = createMockServer(routerPort, "/mgmt/remove");
    client.beforeServerStopped(new KieServer() {

        @Override
        public ServiceResponse<KieServerInfo> getInfo() {
            return null;
        }

        @Override
        public ServiceResponse<KieContainerResource> createContainer(String containerId, KieContainerResource container) {
            return null;
        }

        @Override
        public ServiceResponse<KieContainerResourceList> listContainers(KieContainerResourceFilter containerFilter) {
            KieContainerResource containerResource = new KieContainerResource(containerInstance.getContainerId(), containerInstance.getResource().getReleaseId(), containerInstance.getStatus());
            List<KieContainerResource> containers = new ArrayList<KieContainerResource>();
            containers.add(containerResource);
            KieContainerResourceList list = new KieContainerResourceList(containers);
            return new ServiceResponse<KieContainerResourceList>(ServiceResponse.ResponseType.SUCCESS, "", list);
        }

        @Override
        public ServiceResponse<KieContainerResource> getContainerInfo(String id) {
            return null;
        }

        @Override
        public ServiceResponse<Void> disposeContainer(String containerId) {
            return null;
        }

        @Override
        public ServiceResponse<KieScannerResource> getScannerInfo(String id) {
            return null;
        }

        @Override
        public ServiceResponse<KieScannerResource> updateScanner(String id, KieScannerResource resource) {
            return null;
        }

        @Override
        public ServiceResponse<ReleaseId> getContainerReleaseId(String id) {
            return null;
        }

        @Override
        public ServiceResponse<ReleaseId> updateContainerReleaseId(String id, ReleaseId releaseId) {
            return null;
        }

        @Override
        public ServiceResponse<ReleaseId> updateContainerReleaseId(String id, ReleaseId releaseId, boolean reset) {
            return null;
        }

        @Override
        public ServiceResponse<KieServerStateInfo> getServerState() {
            return null;
        }

        @Override
        public void addServerMessage(Message message) {
        }

        @Override
        public void addContainerMessage(String containerId, Message message) {
        }
    });
    failureLatch.await();
    wireMockServer.start();
    boolean met = successLatch.await(4L, TimeUnit.SECONDS);
    assertFalse("On success should not be invoked", met);
    wireMockServer.verify(0, postRequestedFor(urlEqualTo("/mgmt/remove")));
}
Also used : KieContainerResourceList(org.kie.server.api.model.KieContainerResourceList) Message(org.kie.server.api.model.Message) ReleaseId(org.kie.server.api.model.ReleaseId) CountDownLatch(java.util.concurrent.CountDownLatch) KieServer(org.kie.server.services.api.KieServer) ServiceResponse(org.kie.server.api.model.ServiceResponse) KieScannerResource(org.kie.server.api.model.KieScannerResource) KieContainerResourceFilter(org.kie.server.api.model.KieContainerResourceFilter) KieContainerResourceList(org.kie.server.api.model.KieContainerResourceList) ArrayList(java.util.ArrayList) List(java.util.List) KieContainerResource(org.kie.server.api.model.KieContainerResource) Test(org.junit.Test)

Example 5 with KieServer

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

the class MyKieServerWarmupEventListenerVariant method afterContainerStarted.

@Override
public void afterContainerStarted(KieServer kieServer, KieContainerInstance containerInstance) {
    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();
    switch(strategy) {
        case "rest-full":
            restWarmupFull(containerInstance);
            break;
        case "rest-fire-only":
            restWarmupFireOnly(containerInstance);
            break;
        case "internal-full":
            internalWarmupFull(containerInstance);
            break;
        case "internal-fire-only":
            internalWarmupFireOnly(containerInstance);
            break;
        default:
            break;
    }
    LOGGER.info("afterContainerStarted : elapsed time = {}ms", System.currentTimeMillis() - start);
}
Also used : KieServicesConfiguration(org.kie.server.client.KieServicesConfiguration) ExecutionResults(org.kie.api.runtime.ExecutionResults) LoggerFactory(org.slf4j.LoggerFactory) KieCommands(org.kie.api.command.KieCommands) CommandFactory(org.kie.internal.command.CommandFactory) ServiceResponse(org.kie.server.api.model.ServiceResponse) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KieServicesClient(org.kie.server.client.KieServicesClient) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat) KieServices(org.kie.api.KieServices) Command(org.kie.api.command.Command) RuleServicesClient(org.kie.server.client.RuleServicesClient) Logger(org.slf4j.Logger) KieContainer(org.kie.api.runtime.KieContainer) KieContainerResource(org.kie.server.api.model.KieContainerResource) KieContainerInstance(org.kie.server.services.api.KieContainerInstance) InvocationTargetException(java.lang.reflect.InvocationTargetException) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) List(java.util.List) KieServicesFactory(org.kie.server.client.KieServicesFactory) Optional(java.util.Optional) KieServer(org.kie.server.services.api.KieServer) KieServerEventListener(org.kie.server.services.api.KieServerEventListener) KieServerConfigItem(org.kie.server.api.model.KieServerConfigItem) KieContainerResource(org.kie.server.api.model.KieContainerResource)

Aggregations

KieServer (org.kie.server.services.api.KieServer)5 List (java.util.List)4 ArrayList (java.util.ArrayList)3 KieContainerResource (org.kie.server.api.model.KieContainerResource)3 ServiceResponse (org.kie.server.api.model.ServiceResponse)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Optional (java.util.Optional)2 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)2 Command (org.kie.api.command.Command)2 ExecutionResults (org.kie.api.runtime.ExecutionResults)2 KieContainer (org.kie.api.runtime.KieContainer)2 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)2 CommandFactory (org.kie.internal.command.CommandFactory)2 KieServerConfigItem (org.kie.server.api.model.KieServerConfigItem)2 ReleaseId (org.kie.server.api.model.ReleaseId)2 KieContainerInstance (org.kie.server.services.api.KieContainerInstance)2 KieServerEventListener (org.kie.server.services.api.KieServerEventListener)2