javaclassloaderespercontextclassloader

Using Esper within OSGi framework


I want to user Esper within an OSGi framework however I'm not sure how the classloading mechanism within OSGi will allow what I need to achieve or even how Esper can be used within OSGi because of it.

I'm using Esper 5.3 and upon inspection of the bundle manifest it does not use the Dynamic-Import OSGi header. At the heart of Esper are the EPStatements which I understand to be SQL queries which operate on POJOs.

What I am struggling to understand is how the Esper bundle, which does not use Dynamic Imports and has no knowledge of what POJOs it may be directed to operate on in the future, gains access to them.

For example if I try and create the EPStatement:

select * com.example.Pojo

If the class com.example.Pojo is in another bundle to the Esper core, then the only way Pojo can be loaded is by using its ClassLoader. This could be done by:

Thread.currentThread().setContextClassLoader(com.example.Pojo.class.getClassLoader);

But doesn't this just load Pojo into the class space of the bundle containing Pojo itself? How can the Esper bundle gain access to it as it will need to when creating the above example EPStatement.


Solution

  • You could use "addEventType(String eventTypeName, Class eventClass)" from "epAdministrator.getConfiguration()" to register a type. This way you have control over where the class comes from.

    When trying to find a class by name then Esper always uses "Thread.currentThread().getContextClassLoader();". Your code could use "setContextClassLoader" to set an own classloader. And the code should probably set it back.

    You can simply change the Manifest that comes with Esper jar and include whatever you like.

    You could use "friend" bundles.