Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.
---------실행결과-------------
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:1:888473870:888473870:1:DEFAULT:Server [name=winServer, processors=4, memory=4096, diskSpace=2048, cpuUsage=25, getName()=winServer, getProcessors()=4, getMemory()=4096, getDiskSpace()=2048, getCpuUsage()=25, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=888473870, toString()=com.gooper.drool_test.ServerInfo@34f5090e]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:2:416841088:416841088:2:DEFAULT:Server [name=ubuntuServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=70, getName()=ubuntuServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=70, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=416841088, toString()=com.gooper.drool_test.ServerInfo@18d87d80]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [23] [INFO] Object Inserted : [fact 0:3:1412794598:1412794598:3:DEFAULT:Server [name=debianServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=10, getName()=debianServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=10, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=1412794598, toString()=com.gooper.drool_test.ServerInfo@543588e6]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
session.getFactCount() = 3
[2016-07-21 17:38:27] [CustomAgendaEventListener] [133] [INFO] Match Created : [fact 0:2:416841088:416841088:2:DEFAULT:Server [name=ubuntuServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=70, getName()=ubuntuServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=70, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=416841088, toString()=com.gooper.drool_test.ServerInfo@18d87d80]]
[2016-07-21 17:38:27] [CustomAgendaEventListener] [133] [INFO] Match Created : [fact 0:1:888473870:888473870:1:DEFAULT:Server [name=winServer, processors=4, memory=4096, diskSpace=2048, cpuUsage=25, getName()=winServer, getProcessors()=4, getMemory()=4096, getDiskSpace()=2048, getCpuUsage()=25, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=888473870, toString()=com.gooper.drool_test.ServerInfo@34f5090e]]
[2016-07-21 17:38:27] [CustomAgendaEventListener] [113] [INFO] Before Match Fired : [fact 0:2:416841088:416841088:2:DEFAULT:Server [name=ubuntuServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=70, getName()=ubuntuServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=70, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=416841088, toString()=com.gooper.drool_test.ServerInfo@18d87d80]]
[2016-07-21 17:38:27] [check minimum server configuration2] [20] [INFO] Server "ubuntuServer" was rejected...
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [18] [INFO] Object Deleted : [fact 0:2:416841088:416841088:2:DEFAULT:Server [name=ubuntuServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=70, getName()=ubuntuServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=70, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=416841088, toString()=com.gooper.drool_test.ServerInfo@18d87d80]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
[2016-07-21 17:38:27] [CustomAgendaEventListener] [76] [DEBUG] Rule fired: check minimum server configuration2
[2016-07-21 17:38:27] [CustomAgendaEventListener] [113] [INFO] Before Match Fired : [fact 0:1:888473870:888473870:1:DEFAULT:Server [name=winServer, processors=4, memory=4096, diskSpace=2048, cpuUsage=25, getName()=winServer, getProcessors()=4, getMemory()=4096, getDiskSpace()=2048, getCpuUsage()=25, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=888473870, toString()=com.gooper.drool_test.ServerInfo@34f5090e]]
[2016-07-21 17:38:27] [check minimum server configuration2] [20] [INFO] Server "winServer" was rejected...
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [18] [INFO] Object Deleted : [fact 0:1:888473870:888473870:1:DEFAULT:Server [name=winServer, processors=4, memory=4096, diskSpace=2048, cpuUsage=25, getName()=winServer, getProcessors()=4, getMemory()=4096, getDiskSpace()=2048, getCpuUsage()=25, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=888473870, toString()=com.gooper.drool_test.ServerInfo@34f5090e]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
[2016-07-21 17:38:27] [CustomAgendaEventListener] [76] [DEBUG] Rule fired: check minimum server configuration2
matched count of Fact = 2
listener.getCurrentServers().size() : 1
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [29] [INFO] Object Updated : [fact 0:2:416841088:416841088:5:DEFAULT:Server [name=ubuntuServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=10, getName()=ubuntuServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=10, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=416841088, toString()=com.gooper.drool_test.ServerInfo@18d87d80]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
listener.getCurrentServers().size() : 1
[2016-07-21 17:38:27] [CustomWorkingMemoryEventListener] [29] [INFO] Object Updated : [fact 0:2:416841088:416841088:6:DEFAULT:Server [name=ubuntuServer, processors=4, memory=2048, diskSpace=1024, cpuUsage=5, getName()=ubuntuServer, getProcessors()=4, getMemory()=2048, getDiskSpace()=1024, getCpuUsage()=5, getClass()=class com.gooper.drool_test.ServerInfo, hashCode()=416841088, toString()=com.gooper.drool_test.ServerInfo@18d87d80]] Knowledge Runtime: org.drools.core.impl.StatefulKnowledgeSessionImpl@614ca7df
listener.getCurrentServers().size() : 1
listener.getCurrentServers().size() : 1
session.getObject(ubuntuServerFactHandle) : null
ubuntuServerFactHandle : 0:2:416841088:416841088:6:DEFAULT
------------------------------DroolsTest2.java----------------
package com.gooper.drool_test;
import org.kie.api.KieBase;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.api.runtime.rule.LiveQuery;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.builder.KnowledgeBuilderError;
import com.gooper.drool_test.CustomWorkingMemoryEventListener;
import com.gooper.drool_test.CustomAgendaEventListener;
/* 실시간 쿼리 변환 모니터링
* 팩트내의 필드값이 변경될때 실시간으로 변화를 추적함
*/
public class DroolsTest2 {
public static final void main(String[] args) {
try {
// 지식 빌더 생성
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
// 지식 빌더에 룰정의파일 설정
kbuilder.add(ResourceFactory. newClassPathResource("server_rule.drl"), ResourceType. DRL );
if (kbuilder.hasErrors()) {
if(kbuilder.getErrors().size() > 0) {
for(KnowledgeBuilderError kerror : kbuilder.getErrors()) {
System.out.println("error :" + kerror);
}
}
}
// 새로운 지식 베이스 생성
KieBase kiebase = kbuilder.newKnowledgeBase();
// 새로운 지식 베이스 세션 생성
KieSession session = kiebase.newKieSession();
// logger등록(팩트에 의해 생성된 엑티베이션(Activation)관련 이벤트만 로깅)
session.addEventListener(new CustomAgendaEventListener());
// logger등록(팩트의 추가/수정/제거 이벤트에 대한 정보)
session.addEventListener(new CustomWorkingMemoryEventListener());
// 지식 베이스의 getFactType 메서드를 이용하여 동적으로 팩트를 인스턴스화 시킨다.
// (팩트가 선언되어 있는 룰의 패키지 이름과 팩트의 이름을 인자로 받는다.)
//winServer 생성
ServerInfo winServer = new ServerInfo("winServer", 4, 4096, 2048, 25);
//winServer등록
session.insert(winServer);
//ubuntuServer생성
ServerInfo ubuntuServer = new ServerInfo("ubuntuServer", 4, 2048, 1024, 70);
//ubuntuServer등록
FactHandle ubuntuServerFactHandle = session.insert(ubuntuServer);
//debianServer생성
ServerInfo debianServer = new ServerInfo("debianServer", 4, 2048, 1024, 10);
//debianServer등록
session.insert(debianServer);
System.out.println(" session.getFactCount() = " + session.getFactCount());
int matchedCnt = session.fireAllRules();
System.out.println(" matched count of Fact = " + matchedCnt);
//System.out.println(" session.getProcessInstances() : " + session.getProcessInstances());
// 쿼리를 모니터링하는데 사용하는 리스너
CustomViewChangedEventListener listener = new CustomViewChangedEventListener();
// 모니터링 대상 쿼리이름, 쿼리에 필요한 파라미터, 쿼리를 모니터링하는데 사용하는 리스너
LiveQuery query = session.openLiveQuery("serverCpuUsage", new Object[]{20}, listener);
// query results에 단지 1개의 서버 오브젝트만 있음
System.out.println("listener.getCurrentServers().size() : " + listener.getCurrentServers().size());
ubuntuServer.setCpuUsage(10);
session.update(ubuntuServerFactHandle, ubuntuServer);
// query resutls에 2개의 서버 오브젝트가 있음
System.out.println("listener.getCurrentServers().size() : " + listener.getCurrentServers().size());
ubuntuServer.setCpuUsage(5);
session.update(ubuntuServerFactHandle, ubuntuServer);
// 2 server objects still in the query results
System.out.println("listener.getCurrentServers().size() : " + listener.getCurrentServers().size());
// but one of them was updated
System.out.println("listener.getCurrentServers().size() : " + listener.getCurrentServers().size());
//FactHandle로 내용확인하기
System.out.println(" session.getObject(ubuntuServerFactHandle) : " + session.getObject(ubuntuServerFactHandle));
// System.out.println(" session.getProcessInstances() : " + session.getProcessInstances());
query.close();
session.dispose();
// match된 fact를 확인함
System.out.println("ubuntuServerFactHandle : "+ubuntuServerFactHandle.toExternalForm());
} catch (Throwable t) {
t.printStackTrace();
}
}
}