简体   繁体   English

处理以前由ClassLoader使用Esper加载的类中的事件

[英]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. 我正在尝试使用Esper处理事件,问题是我正在使用以前由ClassLoader加载的Class。 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] 

                                  ...

For the object returned by "eventsList.get(i)" its "getClass()" method should return the same reference as "loadedClass". 对于由“ eventsList.get(i)”返回的对象,其“ getClass()”方法应返回与“ loadedClass”相同的引用。 Ie it should be the same Class for the event type and for the events (or a subclass of course). 即,对于事件类型和事件(或当然是子类),它应该是相同的Class。 Maybe also do this instead: cepConfig.addEventType("measure", loadedClass); 也许也可以这样做: cepConfig.addEventType("measure", loadedClass); since the class name is not relevant to Esper and the Class is. 因为类名称与Esper不相关,而类则与此相关。 If the Class is only available at runtime you could use "cep.getEPAdministrator().getConfiguration().addEventType" for adding an event type at runtime. 如果该类仅在运行时可用,则可以使用“ cep.getEPAdministrator()。getConfiguration()。addEventType”在运行时添加事件类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM