Search in sources :

Example 1 with KieSessionMonitoringMXBean

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

the class MBeansMonitoringTest method testAggregatedAndDispose.

@Test
public void testAggregatedAndDispose() throws Exception {
    String drl = "package org.drools.compiler.integrationtests\n" + "rule ND\n" + "when\n" + "    String() \n" + "    not ( Double() ) \n" + "then\n" + "    // do nothing. \n" + "end\n" + "rule ND2\n" + "salience 1\n" + "when\n" + "    $s : String() \n" + "    not ( Double() ) \n" + "then\n" + "    try { Thread.sleep(10); } catch (Exception e) { }\n" + "    insert( new Double(0) );\n" + "    retract( $s );\n" + "end\n";
    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()));
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(KBASE2).setDefault(false);
    KieSessionModel ksessionModel2 = kieBaseModel2.newKieSessionModel(KSESSION2).setDefault(true).setType(KieSessionModel.KieSessionType.STATELESS);
    ReleaseId releaseId1 = ks.newReleaseId("org.kie.test", "mbeans", "1.0.0");
    createKJar(ks, kproj, releaseId1, null, drl);
    String containerId = "myContainerId";
    KieContainer kc = ks.newKieContainer(containerId, releaseId1);
    KieBase kiebase = kc.getKieBase(KBASE1);
    KieSessionConfiguration sessionConf = ks.newKieSessionConfiguration();
    sessionConf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = kc.newKieSession(KSESSION1, sessionConf);
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    KieSessionMonitoringMXBean aggrMonitor = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(containerId, KBASE1, KieSessionType.STATEFUL, KSESSION1), KieSessionMonitoringMXBean.class);
    long tft = 0;
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 0, 0, 0);
    checkTotalFactCount(aggrMonitor, 0);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft);
    checkTotalSessions(aggrMonitor, 1);
    assertNull(aggrMonitor.getStatsForRule("ND"));
    assertNull(aggrMonitor.getStatsForRule("ND2"));
    assertFalse(aggrMonitor.getStatsByRule().containsKey("ND"));
    assertFalse(aggrMonitor.getStatsByRule().containsKey("ND2"));
    ksession.insert("Ciao");
    ksession.fireAllRules();
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 2, 1, 1);
    checkTotalFactCount(aggrMonitor, 1);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft + 1);
    checkTotalSessions(aggrMonitor, 1);
    checkStatsForRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsForRule(aggrMonitor, "ND2", 1, 0, 1);
    checkStatsByRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsByRule(aggrMonitor, "ND2", 1, 0, 1);
    ksession.fireAllRules();
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 2, 1, 1);
    checkTotalFactCount(aggrMonitor, 1);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft);
    checkTotalSessions(aggrMonitor, 1);
    checkStatsForRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsForRule(aggrMonitor, "ND2", 1, 0, 1);
    checkStatsByRule(aggrMonitor, "ND", 1, 1, 0);
    checkStatsByRule(aggrMonitor, "ND2", 1, 0, 1);
    LOG.debug("---");
    KieSession ksession2 = kc.newKieSession(KSESSION1, sessionConf);
    ksession2.insert("Ciao");
    ksession2.fireAllRules();
    print(aggrMonitor);
    checkAgendaTotals(aggrMonitor, 4, 2, 2);
    checkTotalFactCount(aggrMonitor, 2);
    tft = checkTotalFiringTimeGEQ(aggrMonitor, tft + 1);
    checkTotalSessions(aggrMonitor, 2);
    checkStatsForRule(aggrMonitor, "ND", 2, 2, 0);
    checkStatsForRule(aggrMonitor, "ND2", 2, 0, 2);
    checkStatsByRule(aggrMonitor, "ND", 2, 2, 0);
    checkStatsByRule(aggrMonitor, "ND2", 2, 0, 2);
    ksession.dispose();
    checkTotalSessions(aggrMonitor, 1);
    checkTotalFactCount(aggrMonitor, 1);
    ksession2.dispose();
    checkTotalSessions(aggrMonitor, 0);
    checkTotalFactCount(aggrMonitor, 0);
    LOG.debug("--- NOW for the STATELESS ---");
    tft = 0;
    StatelessKieSession stateless = kc.newStatelessKieSession(KSESSION2, sessionConf);
    StatelessKieSessionMonitoringMXBean aggrMonitor2 = JMX.newMXBeanProxy(mbserver, DroolsManagementAgent.createObjectNameBy(containerId, KBASE2, KieSessionType.STATELESS, KSESSION2), StatelessKieSessionMonitoringMXBean.class);
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 0, 0, 0);
    checkRuleRuntimeTotals(aggrMonitor2, 0, 0);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft);
    checkTotalSessions(aggrMonitor2, 0);
    assertNull(aggrMonitor2.getStatsForRule("ND"));
    assertNull(aggrMonitor2.getStatsForRule("ND2"));
    assertFalse(aggrMonitor2.getStatsByRule().containsKey("ND"));
    assertFalse(aggrMonitor2.getStatsByRule().containsKey("ND2"));
    stateless.execute("Ciao");
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 2, 1, 1);
    checkRuleRuntimeTotals(aggrMonitor2, 2, 1);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
    checkTotalSessions(aggrMonitor2, 1);
    checkStatsForRule(aggrMonitor2, "ND", 1, 1, 0);
    checkStatsForRule(aggrMonitor2, "ND2", 1, 0, 1);
    checkStatsByRule(aggrMonitor2, "ND", 1, 1, 0);
    checkStatsByRule(aggrMonitor2, "ND2", 1, 0, 1);
    stateless.execute("Ciao");
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 4, 2, 2);
    checkRuleRuntimeTotals(aggrMonitor2, 4, 2);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
    checkTotalSessions(aggrMonitor2, 2);
    checkStatsForRule(aggrMonitor2, "ND", 2, 2, 0);
    checkStatsForRule(aggrMonitor2, "ND2", 2, 0, 2);
    checkStatsByRule(aggrMonitor2, "ND", 2, 2, 0);
    checkStatsByRule(aggrMonitor2, "ND2", 2, 0, 2);
    StatelessKieSession stateless2 = kc.newStatelessKieSession(KSESSION2, sessionConf);
    LOG.debug(stateless + " " + stateless2);
    checkTotalSessions(aggrMonitor2, 2);
    stateless2.execute("Ciao");
    print(aggrMonitor2);
    checkAgendaTotals(aggrMonitor2, 6, 3, 3);
    checkRuleRuntimeTotals(aggrMonitor2, 6, 3);
    tft = checkTotalFiringTimeGEQ(aggrMonitor2, tft + 1);
    checkTotalSessions(aggrMonitor2, 3);
    checkStatsForRule(aggrMonitor2, "ND", 3, 3, 0);
    checkStatsForRule(aggrMonitor2, "ND2", 3, 0, 3);
    checkStatsByRule(aggrMonitor2, "ND", 3, 3, 0);
    checkStatsByRule(aggrMonitor2, "ND2", 3, 0, 3);
    KieContainer kc2 = ks.newKieContainer("kc2", releaseId1);
    assertEquals(5, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(containerId) + ",*"), null).size());
    kc.dispose();
    assertEquals(0, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote(containerId) + ",*"), null).size());
    assertEquals(1, mbserver.queryNames(new ObjectName("org.kie:kcontainerId=" + ObjectName.quote("kc2") + ",*"), null).size());
}
Also used : GenericKieSessionMonitoringMXBean(org.kie.api.management.GenericKieSessionMonitoringMXBean) KieSessionMonitoringMXBean(org.kie.api.management.KieSessionMonitoringMXBean) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) ObjectName(javax.management.ObjectName) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 2 with KieSessionMonitoringMXBean

use of org.kie.api.management.KieSessionMonitoringMXBean 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 3 with KieSessionMonitoringMXBean

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

the class MBeansMonitoringTest method print.

private void print(GenericKieSessionMonitoringMXBean mb) {
    LOG.debug("total match created  : {}", mb.getTotalMatchesCreated());
    LOG.debug("total match cancelled: {}", mb.getTotalMatchesCancelled());
    LOG.debug("total match fired    : {}", mb.getTotalMatchesFired());
    if (mb instanceof StatelessKieSessionMonitoringMXBean) {
        StatelessKieSessionMonitoringMXBean c = (StatelessKieSessionMonitoringMXBean) mb;
        LOG.debug("inserted and deleted : +{} -{}", c.getTotalObjectsInserted(), c.getTotalObjectsDeleted());
    } else if (mb instanceof KieSessionMonitoringMXBean) {
        KieSessionMonitoringMXBean c = (KieSessionMonitoringMXBean) mb;
        LOG.debug("total tact count     : {}", c.getTotalFactCount());
    }
    LOG.debug("{} ms .", mb.getTotalFiringTime());
}
Also used : GenericKieSessionMonitoringMXBean(org.kie.api.management.GenericKieSessionMonitoringMXBean) KieSessionMonitoringMXBean(org.kie.api.management.KieSessionMonitoringMXBean) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean) StatelessKieSessionMonitoringMXBean(org.kie.api.management.StatelessKieSessionMonitoringMXBean)

Example 4 with KieSessionMonitoringMXBean

use of org.kie.api.management.KieSessionMonitoringMXBean 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)

Aggregations

KieSessionMonitoringMXBean (org.kie.api.management.KieSessionMonitoringMXBean)4 MBeanServer (javax.management.MBeanServer)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 KieServices (org.kie.api.KieServices)3 GenericKieSessionMonitoringMXBean (org.kie.api.management.GenericKieSessionMonitoringMXBean)3 StatelessKieSessionMonitoringMXBean (org.kie.api.management.StatelessKieSessionMonitoringMXBean)3 KieContainer (org.kie.api.runtime.KieContainer)3 ObjectName (javax.management.ObjectName)2 InternalKieContainer (org.drools.core.impl.InternalKieContainer)2 KieContainerMonitorMXBean (org.kie.api.management.KieContainerMonitorMXBean)2 KieSession (org.kie.api.runtime.KieSession)2 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)2 ReleaseId (org.kie.api.builder.ReleaseId)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 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1