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

时间:2015-06-16 10:23:30

标签: java esper urlclassloader

我正在尝试使用Esper处理事件,问题是我使用的是以前由ClassLoader加载的Class。和 声明似乎没有插入流中。有什么帮助吗?

这里是代码:

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));
    }

}


}

听众

public class CEPListener implements UpdateListener {


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

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

}

}

控制台输出是:

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] 

                                  ...

1 个答案:

答案 0 :(得分:2)

对于“eventsList.get(i)”返回的对象,其“getClass()”方法应返回与“loadedClass”相同的引用。即它应该是事件类型和事件(或者当然是子类)的相同类。也许也这样做:cepConfig.addEventType("measure", loadedClass);因为类名与Esper和Class无关。如果Class仅在运行时可用,则可以使用“cep.getEPAdministrator()。getConfiguration()。addEventType”在运行时添加事件类型。