All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.limetri.ygg.eventengine.SimpleEventEngine Maven / Gradle / Ivy

There is a newer version: 0.28
Show newest version
/**
 * Copyright (C) 2008-2013 LimeTri. All rights reserved.
 *
 * This library 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.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it
 * is applied to this software, see the FLOSS License Exception
 * .
 *
 * This library 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 library. If not, see .
 */
package eu.limetri.ygg.eventengine;

import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import eu.limetri.ygg.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Service(value = EventProcessingEngine.BEAN_NAME)
//@Scope("prototype")
public class SimpleEventEngine implements EventProcessingEngine, CrudListener{

    private static final Logger log = LoggerFactory.getLogger(SimpleEventEngine.class);

    Subscriber subscriber;

    private EPServiceProvider epService;
    private EPStatement epStatement;

    @PostConstruct
    public void init() {
        epService = EPServiceProviderManager.getDefaultProvider();
        epService.getEPAdministrator().getConfiguration().addEventType("ValuesEvent", ValuesInfo.class);
        log.debug("Initialized EPE ..");
    }

    @Override
    public void notifyOfTrigger(Trigger trigger) {

    }

    @Override
    public void feedTriggers(TriggerList triggerList) {
        log.debug("TriggerListEPE:{}", triggerList);
        List conditions = new ArrayList<>();
        for (Trigger t : triggerList.getTriggers()) {
            conditions.add(t.getTriggerCondition());
        }
        log.debug("conditions:{}", conditions);

        List epStatements = new ArrayList<>();
        subscriber = new Subscriber();
        for (String theCondition : conditions) {

            epStatement = epService.getEPAdministrator().createEPL(subscriber.getStatement(theCondition));
            epStatement.setSubscriber(subscriber);
            epStatements.add(epStatement);
            log.debug("statement:{}", epStatement.getText());
        }
    }

    @Override
    public  void handleEvent(T event) {
        log.debug("Event:{}", event);
        epService.getEPRuntime().sendEvent(event);
    }

    @Override
    public void handleEvent(Serializable event, List bpid) {
        throw new UnsupportedOperationException("Not supported");
    }

    @Override
    public void onCreate(Trigger object) {
       //TODO: Investigate creating EPSstatements from here, and not by the use of feedTriggers
    }

    @Override
    public void onUpdate(Trigger object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void onDelete(Trigger object) {
        log.debug("Destroying EPSstatement:{}",epStatement.getName());
        epService.getEPAdministrator().getStatement(epStatement.getName()).destroy();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy