Search in sources :

Example 1 with KieContainerMonitorMXBean

use of org.kie.api.management.KieContainerMonitorMXBean in project drools by kiegroup.

the class MonitoringWithJPAKnowledgeServiceTest method testBasic.

@Test
public void testBasic() throws MalformedObjectNameException {
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    KieServices ks = KieServices.Factory.get();
    String containerId = "testcontainer-" + System.currentTimeMillis();
    KieContainer kc = ks.newKieClasspathContainer(containerId);
    KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(containerId), KieContainerMonitorMXBean.class);
    KieBase kb = kc.getKieBase("org.kie.monitoring.kbase1");
    // Use JPAKnowledgeService to create the KieSession
    StatefulKnowledgeSession statefulKieSession = JPAKnowledgeService.newStatefulKnowledgeSession(kb, null, env);
    long sessionIdentifier = statefulKieSession.getIdentifier();
    statefulKieSession.insert("String1");
    statefulKieSession.fireAllRules();
    KieSessionMonitoringMXBean statefulKieSessionMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(containerId, "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "persistent"), KieSessionMonitoringMXBean.class);
    assertEquals(1, statefulKieSessionMonitor.getTotalMatchesFired());
    // There should be 3 mbeans for KieContainer, KieBase and KieSession.
    assertEquals(3, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(containerId) + ",*"), null).size());
    // needs to be done separately:
    statefulKieSession.dispose();
    StatefulKnowledgeSession deserialized = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionIdentifier, kb, null, createEnvironment(context));
    deserialized.insert("String2");
    deserialized.fireAllRules();
    // the mbean does not persist state, but in this case consolidate by grouping the fire of the former session and the deserialized one
    assertEquals(2, statefulKieSessionMonitor.getTotalMatchesFired());
    kc.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieContainerMonitorMXBean(org.kie.api.management.KieContainerMonitorMXBean) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) KieSessionMonitoringMXBean(org.kie.api.management.KieSessionMonitoringMXBean) KieServices(org.kie.api.KieServices) MBeanServer(javax.management.MBeanServer) KieContainer(org.kie.api.runtime.KieContainer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 2 with KieContainerMonitorMXBean

use of org.kie.api.management.KieContainerMonitorMXBean in project drools by kiegroup.

the class MBeansMonitoringTest method testLoadKjarFromClasspath.

/**
 * Copied from KieRepositoryTest to test JMX monitoring
 */
@Test
public void testLoadKjarFromClasspath() {
    // DROOLS-1335
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { this.getClass().getResource("/kie-project-simple-1.0.0.jar") });
    Thread.currentThread().setContextClassLoader(urlClassLoader);
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    try {
        KieServices ks = KieServices.Factory.get();
        KieRepository kieRepository = ks.getRepository();
        ReleaseId releaseId = ks.newReleaseId("org.test", "kie-project-simple", "1.0.0");
        KieModule kieModule = kieRepository.getKieModule(releaseId);
        assertNotNull(kieModule);
        assertEquals(releaseId, kieModule.getReleaseId());
        ks.newKieContainer("myID", releaseId);
        KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID"), KieContainerMonitorMXBean.class);
        assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(releaseId));
        assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(releaseId));
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}
Also used : KieRepository(org.kie.api.builder.KieRepository) URLClassLoader(java.net.URLClassLoader) KieContainerMonitorMXBean(org.kie.api.management.KieContainerMonitorMXBean) URLClassLoader(java.net.URLClassLoader) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 3 with KieContainerMonitorMXBean

use of org.kie.api.management.KieContainerMonitorMXBean in project drools by kiegroup.

the class MBeansMonitoringTest method testKieClasspathMBeans.

@Test
public void testKieClasspathMBeans() throws Exception {
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    KieServices ks = KieServices.Factory.get();
    KieContainer kc = ks.getKieClasspathContainer("myID");
    KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID"), KieContainerMonitorMXBean.class);
    KieBase kb = kc.getKieBase("org.kie.monitoring.kbase1");
    KieSession statefulKieSession = kc.newKieSession("org.kie.monitoring.kbase1.ksession1");
    StatelessKieSession statelessKieSession = kc.newStatelessKieSession("org.kie.monitoring.kbase1.ksession2");
    KieSessionMonitoringMXBean statefulKieSessionMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), KieSessionMonitoringMXBean.class);
    StatelessKieSessionMonitoringMXBean statelessKieSessionMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), StatelessKieSessionMonitoringMXBean.class);
    assertEquals("myID", c1Monitor.getContainerId());
    assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertEquals("org.kie.monitoring.kbase1.ksession1", statefulKieSessionMonitor.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1", statefulKieSessionMonitor.getKieBaseId());
    assertEquals("org.kie.monitoring.kbase1.ksession1", statelessKieSessionMonitor.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1", statelessKieSessionMonitor.getKieBaseId());
    KieContainer kc2 = ks.newKieClasspathContainer("myID2");
    KieContainerMonitorMXBean c2Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID2"), KieContainerMonitorMXBean.class);
    KieBase kb2 = kc2.getKieBase("org.kie.monitoring.kbase1");
    KieSession statefulKieSession2 = kc2.newKieSession("org.kie.monitoring.kbase1.ksession1");
    StatelessKieSession statelessKieSession2 = kc2.newStatelessKieSession("org.kie.monitoring.kbase1.ksession2");
    KieSessionMonitoringMXBean statefulKieSessionMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID2", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), KieSessionMonitoringMXBean.class);
    StatelessKieSessionMonitoringMXBean statelessKieSessionMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("myID2", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"), StatelessKieSessionMonitoringMXBean.class);
    assertEquals("myID2", c2Monitor.getContainerId());
    assertTrue(c2Monitor.getConfiguredReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertTrue(c2Monitor.getResolvedReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertEquals("org.kie.monitoring.kbase1.ksession1", statefulKieSessionMonitor2.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1", statefulKieSessionMonitor2.getKieBaseId());
    assertEquals("org.kie.monitoring.kbase1.ksession1", statelessKieSessionMonitor2.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1", statelessKieSessionMonitor2.getKieBaseId());
    kc.dispose();
    kc2.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieContainerMonitorMXBean(org.kie.api.management.KieContainerMonitorMXBean) GenericKieSessionMonitoringMXBean(org.kie.api.management.GenericKieSessionMonitoringMXBean) KieSessionMonitoringMXBean(org.kie.api.management.KieSessionMonitoringMXBean) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) MBeanServer(javax.management.MBeanServer) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with KieContainerMonitorMXBean

use of org.kie.api.management.KieContainerMonitorMXBean in project drools by kiegroup.

the class MBeansMonitoringTest method testContainerMBeans.

@Test
public void testContainerMBeans() throws Exception {
    String drl = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "    @expires(10s)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(KBASE1).setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel(KSESSION1).setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie.test", "mbeans", "1.0.0");
    createKJar(ks, kproj, releaseId1, null, drl);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieBase kiebase = kc.getKieBase(KBASE1);
    kc.newKieSession(KSESSION1);
    kiebase.newKieSession();
    String kc1ID = ((InternalKieContainer) kc).getContainerId();
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    LOG.debug("{}", mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(kc1ID) + ",*"), null));
    ReleaseId verRelease = ks.newReleaseId("org.kie.test", "mbeans", "RELEASE");
    KieContainer kc2 = ks.newKieContainer("Matteo", verRelease);
    kc2.newKieSession(KSESSION1);
    KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(kc1ID), KieContainerMonitorMXBean.class);
    assertEquals(releaseId1.toExternalForm(), c1Monitor.getConfiguredReleaseIdStr());
    assertEquals(releaseId1.toExternalForm(), c1Monitor.getResolvedReleaseIdStr());
    assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(releaseId1));
    assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(releaseId1));
    assertEquals(releaseId1.getVersion(), c1Monitor.getConfiguredReleaseId().getVersion());
    assertEquals(releaseId1.getVersion(), c1Monitor.getResolvedReleaseId().getVersion());
    KieContainerMonitorMXBean c2Monitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy("Matteo"), KieContainerMonitorMXBean.class);
    assertEquals(verRelease.toExternalForm(), c2Monitor.getConfiguredReleaseIdStr());
    assertEquals(releaseId1.toExternalForm(), c2Monitor.getResolvedReleaseIdStr());
    assertTrue(c2Monitor.getConfiguredReleaseId().sameGAVof(verRelease));
    assertTrue(c2Monitor.getResolvedReleaseId().sameGAVof(releaseId1));
    assertEquals(verRelease.getVersion(), c2Monitor.getConfiguredReleaseId().getVersion());
    assertEquals(releaseId1.getVersion(), c2Monitor.getResolvedReleaseId().getVersion());
    // MBean are supported only via KieContainer public API.
    assertEquals(3, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(kc1ID) + ",*"), null).size());
    kc.dispose();
    assertEquals(0, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(kc1ID) + ",*"), null).size());
    assertEquals(3, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote("Matteo") + ",*"), null).size());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieContainerMonitorMXBean(org.kie.api.management.KieContainerMonitorMXBean) KieServices(org.kie.api.KieServices) InternalKieContainer(org.drools.core.impl.InternalKieContainer) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

MBeanServer (javax.management.MBeanServer)4 Test (org.junit.Test)4 KieServices (org.kie.api.KieServices)4 KieContainerMonitorMXBean (org.kie.api.management.KieContainerMonitorMXBean)4 KieBase (org.kie.api.KieBase)3 KieContainer (org.kie.api.runtime.KieContainer)3 ObjectName (javax.management.ObjectName)2 InternalKieContainer (org.drools.core.impl.InternalKieContainer)2 ReleaseId (org.kie.api.builder.ReleaseId)2 KieSessionMonitoringMXBean (org.kie.api.management.KieSessionMonitoringMXBean)2 URLClassLoader (java.net.URLClassLoader)1 KieModule (org.kie.api.builder.KieModule)1 KieRepository (org.kie.api.builder.KieRepository)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 KieModuleModel (org.kie.api.builder.model.KieModuleModel)1 KieSessionModel (org.kie.api.builder.model.KieSessionModel)1 GenericKieSessionMonitoringMXBean (org.kie.api.management.GenericKieSessionMonitoringMXBean)1 StatelessKieSessionMonitoringMXBean (org.kie.api.management.StatelessKieSessionMonitoringMXBean)1 KieSession (org.kie.api.runtime.KieSession)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1