Search code examples
javaesperurlclassloader

Processing Events from a Class that it was previously loaded by ClassLoader with Esper


I´m trying to process events using Esper, the problem is I´m using a Class that it was previously loaded by ClassLoader. And the Statements does not seem be inserted in the stream. Any help, please?

Here the code:

public class CEPMotor {

public <loadedClass> void motor(List<loadedClass> eventsList, Class<?> loadedClass) {

    // log4j
    BasicConfigurator.configure();


    // We change the classpath, adding a new Class to the ClassLoader.
    try {           

        File root = new File("./build/classes");

        // Get the ClassLoader and its method addURL()
        URLClassLoader classLoader = ((URLClassLoader) ClassLoader
                .getSystemClassLoader());
        Method methodAdd = URLClassLoader.class.getDeclaredMethod("addURL",
                new Class[] { URL.class });
        methodAdd.setAccessible(true);

        // URL from the class we want to add
        URL url = root.toURI().toURL();

        // the method addURL is invoked passing that class´ url 
        metodoAdd.invoke(classLoader, new Object[] { url });
    } catch (Exception e) {
        e.printStackTrace();
    }

    Configuration cepConfig = new Configuration();
    cepConfig.getEngineDefaults().getLogging().setEnableExecutionDebug(true);


    cepConfig.addEventType("measure", loadedClass.getName());


    EPServiceProvider cep = EPServiceProviderManager
            .getDefaultProvider(cepConfig);


    EPRuntime cepRT = cep.getEPRuntime();


    EPAdministrator cepAdm = cep.getEPAdministrator();


    EPStatement cepStatement = cepAdm.createEPL("@Audit select * from "
            + "measure ");


    cepStatement.addListener(new CEPListener());


    for (int i = 0; i < eventsList.size(); i++) {

        cepRT.sendEvent(eventsList.get(i));
    }

}


}

The Listener

public class CEPListener implements UpdateListener {


@Override
public void update(EventBean[] newData, EventBean[] oldData) {

    System.out.println("\n<-------------- Evento recibido: "
            + newData[0].getUnderlying() + "\n");

}

}

The console output is:

941 [main] DEBUG com.espertech.esper.core.service.EPRuntimeImpl  - .sendEvent Processing event     eventos.Source@7690781[enabled=true,displayStartDate=0,displayEndDate=0,descript    ionFrench=(C&P) Sentier Brookfield - Sentier Sawmill Creek à      Brookfield,twitterMessage=,twitterMessageFrench=,message=(C&P) Brookfield      Pathway closed from Sawmill Creek Pathway to Brookfield due to construction.      From Monday, August 18 to Autumn 2015.,messageFrench=(C&P) Sentier Brookfield     fermé du sentier Sawmill Creek à Brookfield en raison de construction. Du     lundi 18 août à l'automne     2015.,id=1022,latitude=45.37488,longitude=-75.68217,description=(C&P) Brookfield    Pathway - Sawmill Creek Pathway to Brookfield]
957 [main] DEBUG com.espertech.esper.core.service.EPRuntimeImpl  - .sendEvent Processing event   eventos.Source@77eca502[enabled=true,displayStartDate=0,displayEndDate=0,descrip    tionFrench=(MTQ) Pont des Draveurs,twitterMessage=,twitterMessageFrench=,message=(MTQ) Draveurs Bridge reduced to two lanes in counter-peak direction due to construction.,messageFrench=(MTQ) Pont des Draveurs réduit à deux voies en direction contre-pointe en raison de construction.,id=1385,latitude=45.457399,longitude=-75.716153,description=(MTQ) Draveurs Bridge (LR B)]
957 [main] DEBUG com.espertech.esper.core.service.EPRuntimeImpl  - .sendEvent Processing event eventos.Source@3246fb96[enabled=true,displayStartDate=0,displayEndDate=0,descriptionFrench=(MTQ) Bretelle - St-Louis à 50 d/e,twitterMessage=,twitterMessageFrench=,message=(MTQ) Ramp closed from St-Louis to 50 westbound due to construction. Follow the signed detour.,messageFrench=(MTQ) Bretelle fermée de St-Louis à 50 direction Ouest en raison de construction. Suivre le détour signalé.,id=1304,latitude=45.461432,longitude=-75.71728,description=(MTQ) Ramp - St-Louis to 50 E/B (FC)]
 957 [main] DEBUG com.espertech.esper.core.service.EPRuntimeImpl  - .sendEvent Processing event eventos.Source@2e222612[enabled=true,displayStartDate=0,displayEndDate=0,descriptionFrench=(CCN) Colonel By à Clegg,twitterMessage=,twitterMessageFrench=,message=Colonel By off-peak lane reductions at Clegg due to construction. From Monday, April 13 to Friday, June 26.,messageFrench=Colonel By réductions de voies hors-pointe à Clegg en raison de construction. Du lundi 13 avril au vendredi 26 juin.,id=1323,latitude=45.404679,longitude=-75.680244,description=(NCC) Colonel By at Clegg]
957 [main] DEBUG com.espertech.esper.core.service.EPRuntimeImpl  - .sendEvent Processing event eventos.Source@61386958[enabled=true,displayStartDate=0,displayEndDate=0,descriptionFrench=(P) Slater - Bank à O'Connor,twitterMessage=,twitterMessageFrench=,message=Slater sidewalk north side closed from Bank to O'Connor due to construction. Until Thursday, October 1.,messageFrench=Slater trottoir côté Nord fermé de Bank à O'Connor en raison de construction. Jusqu'au jeudi 1 octobre.,id=965,latitude=45.419781,longitude=-75.698773,description=(P) Slater - Bank to O'Connor]
957 [main] DEBUG com.espertech.esper.core.service.EPRuntimeImpl  - .sendEvent Processing event eventos.Source@73ee04c8[enabled=true,displayStartDate=0,displayEndDate=0,descriptionFrench=(P) Wellington - Bank à O'Connor,twitterMessage=,twitterMessageFrench=,message=Wellington sidewalk south side closed from Bank to O'Connor due to construction. Until Saturday, October 31.,messageFrench=Wellington trottoir côté Sud fermé de Bank à O'Connor en raison de construction. Jusqu'au samedi 31 octobre.,id=967,latitude=45.422217,longitude=-75.700935,description=(P) Wellington - Bank to O'Connor] 

                                  ...

Solution

  • For the object returned by "eventsList.get(i)" its "getClass()" method should return the same reference as "loadedClass". I.e. it should be the same Class for the event type and for the events (or a subclass of course). Maybe also do this instead: cepConfig.addEventType("measure", loadedClass); since the class name is not relevant to Esper and the Class is. If the Class is only available at runtime you could use "cep.getEPAdministrator().getConfiguration().addEventType" for adding an event type at runtime.