Java tutorial
/* * GLIMPSE: A generic and flexible monitoring infrastructure. * For further information: http://labsewiki.isti.cnr.it/labse/tools/glimpse/public/main * * Copyright (C) 2011 Software Engineering Laboratory - ISTI CNR - Pisa - Italy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package it.cnr.isti.labsedc.glimpse.impl; import it.cnr.isti.labsedc.glimpse.buffer.EventsBuffer; import it.cnr.isti.labsedc.glimpse.cep.ComplexEventProcessor; import it.cnr.isti.labsedc.glimpse.event.GlimpseBaseEvent; import it.cnr.isti.labsedc.glimpse.event.GlimpseBaseEventBPMN; import it.cnr.isti.labsedc.glimpse.event.GlimpseBaseEventChoreos; import it.cnr.isti.labsedc.glimpse.exceptions.UnknownMethodCallRuleException; import it.cnr.isti.labsedc.glimpse.rules.DroolsRulesManager; import it.cnr.isti.labsedc.glimpse.rules.RulesManager; import it.cnr.isti.labsedc.glimpse.utils.DebugMessages; import java.util.Properties; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; import javax.jms.Session; import javax.jms.Topic; import javax.jms.TopicConnection; import javax.jms.TopicConnectionFactory; import javax.jms.TopicPublisher; import javax.jms.TopicSession; import javax.jms.TopicSubscriber; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.commons.net.ntp.TimeStamp; import org.drools.KnowledgeBase; import org.drools.KnowledgeBaseConfiguration; import org.drools.KnowledgeBaseFactory; import org.drools.builder.KnowledgeBuilder; import org.drools.builder.KnowledgeBuilderError; import org.drools.builder.KnowledgeBuilderErrors; import org.drools.builder.KnowledgeBuilderFactory; import org.drools.builder.ResourceType; import org.drools.conf.EventProcessingOption; import org.drools.io.Resource; import org.drools.io.ResourceFactory; import org.drools.runtime.StatefulKnowledgeSession; import org.drools.runtime.rule.WorkingMemoryEntryPoint; public class ComplexEventProcessorImpl extends ComplexEventProcessor implements MessageListener { private String topic; private TopicConnection connection; private Topic connectionTopic; private TopicSession publishSession; private TopicSession subscribeSession; @SuppressWarnings("unused") private TopicPublisher tPub; private TopicSubscriber tSub; private KnowledgeBase knowledgeBase; private StatefulKnowledgeSession ksession; private WorkingMemoryEntryPoint eventStream; private KnowledgeBuilder knowledgeBuilder; public ComplexEventProcessorImpl(Properties settings, EventsBuffer<GlimpseBaseEvent<?>> buffer, TopicConnectionFactory connectionFact, InitialContext initConn) { this.topic = settings.getProperty("probeTopic"); init(connectionFact, initConn); } public void init(TopicConnectionFactory connectionFact, InitialContext initConn) { try { DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Creating connection object "); connection = connectionFact.createTopicConnection(); DebugMessages.ok(); DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Creating public session object "); publishSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); DebugMessages.ok(); DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Creating subscribe object "); subscribeSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); DebugMessages.ok(); DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Setting up reading topic "); connectionTopic = (Topic) initConn.lookup(topic); tSub = subscribeSession.createSubscriber(connectionTopic, null, true); DebugMessages.ok(); DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Setting up destination topic "); connectionTopic = publishSession.createTopic(this.topic); tPub = publishSession.createPublisher(connectionTopic); DebugMessages.ok(); DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Reading knowledge base "); knowledgeBase = createKnowledgeBase(); ksession = knowledgeBase.newStatefulKnowledgeSession(); ksession.setGlobal("EVENTS EntryPoint", eventStream); eventStream = ksession.getWorkingMemoryEntryPoint("DEFAULT"); cepRuleManager = new DroolsRulesManager(knowledgeBuilder, knowledgeBase, ksession); DebugMessages.ok(); } catch (JMSException e) { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage()); } catch (NamingException e) { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage()); } } public void run() { DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "Starting connection "); try { if (connection == null) { System.out.println( "Unable to connect to ActiveMQ instance or connection parameters wrong.\nPlease check and restart GLIMPSE."); System.exit(0); } connection.start(); DebugMessages.ok(); tSub.setMessageListener(this); DebugMessages.line(); while (this.getState() == State.RUNNABLE) { Thread.sleep(20); ksession.fireAllRules(); } } catch (JMSException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public void onMessage(Message arg0) { ObjectMessage msg = (ObjectMessage) arg0; try { GlimpseBaseEvent<?> receivedEvent = (GlimpseBaseEvent<?>) msg.getObject(); if (eventStream != null && receivedEvent != null) { try { eventStream.insert(receivedEvent); if (receivedEvent instanceof GlimpseBaseEventChoreos<?>) { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "receives:\n" + "eventData: " + receivedEvent.getEventData() + "\n" + "eventName: " + receivedEvent.getEventName() + "\n" + "timestamp: " + receivedEvent.getTimeStamp() + "\n" + "machineIP: " + ((GlimpseBaseEventChoreos<?>) receivedEvent).getMachineIP() + "\n" + "choreographySource: " + ((GlimpseBaseEventChoreos<?>) receivedEvent).getChoreographySource()); } else if (receivedEvent instanceof GlimpseBaseEventBPMN<?>) { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "receives:\n" + "eventData: " + receivedEvent.getEventData() + "\n" + "eventName: " + receivedEvent.getEventName() + "\n" + "timestamp: " + receivedEvent.getTimeStamp() + "\n" + "sessionID: " + ((GlimpseBaseEventBPMN<?>) receivedEvent).getSessionID() + "\n" + "assigneeID: " + ((GlimpseBaseEventBPMN<?>) receivedEvent).getAssigneeID() + "\n" + "roleID: " + ((GlimpseBaseEventBPMN<?>) receivedEvent).getRoleID() + "\n" + "taskID: " + ((GlimpseBaseEventBPMN<?>) receivedEvent).getTaskID() + "\n" + "subProcessID: " + ((GlimpseBaseEventBPMN<?>) receivedEvent).getSubProcessID() + "\n" + "desideredCompletionTime: " + ((GlimpseBaseEventBPMN<?>) receivedEvent).getDesideredCompletionTime()); } else { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), "receives:\n" + "eventData: " + receivedEvent.getEventData() + "\n" + "eventName: " + receivedEvent.getEventName() + "\n" + "timestamp: " + receivedEvent.getTimeStamp()); } DebugMessages.line(); } catch (org.drools.RuntimeDroolsException droolsCrashException) { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), droolsCrashException.getMessage()); new UnknownMethodCallRuleException(); } } } catch (JMSException e) { e.printStackTrace(); } catch (ClassCastException ex) { DebugMessages.println(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), ex.getMessage()); } } private KnowledgeBase createKnowledgeBase() { try { KnowledgeBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); config.setOption(EventProcessingOption.STREAM); /* Using knowledge builder to create a knowledgePackage using provided resources (drl file) * after the creation the knowledgePackage contained into the knowledge builder will be putted * into the knowledgeBase using the method addKnowledgePackages(knowledgeBuilder.getKnowledgePackages()) */ knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); //Resource drlToLoad = ResourceFactory.newFileResource("startupRule.drl"); String firstRuleToLoad = "import it.cnr.isti.labsedc.glimpse.event.GlimpseBaseEventAbstract; " + "declare GlimpseBaseEventAbstract " + "@role( event ) " + "@timestamp( timeStamp ) " + "end"; byte[] firstRuleToLoadByteArray = firstRuleToLoad.getBytes(); Resource drlToLoad = ResourceFactory.newByteArrayResource(firstRuleToLoadByteArray); knowledgeBuilder.add(drlToLoad, ResourceType.DRL); KnowledgeBuilderErrors errors = knowledgeBuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config); knowledgeBase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages()); return knowledgeBase; } catch (NullPointerException e) { System.out.println(e.getMessage()); System.out.println(e.getCause()); DebugMessages.print(TimeStamp.getCurrentTime(), this.getClass().getSimpleName(), e.getMessage()); return null; } } @Override public void setMetric() { } public RulesManager getRuleManager() { return this.cepRuleManager; } }