Using EclipseLink from OCEP
By Manju James on Jan 27, 2011
There are certain use cases where events flowing through an EPN should be saved into a persistent store. Using EclipseLink's JPA implementation is a quick way to achieve this. To test this out with OCEP server, I modified our helloworld sample to use EclipseLink to persist the events as they come through. This blog entry contains instructions on how to do this and some sample code (Thanks to Shaun Smith from persistence team for his help). Please refer to EclipseLink page to find out more about EclipseLink.
To start off, you need to download eclipselink's osgi bundles. Copy the following files from the downloaded zip into modules/ext directory of the OCEP domain ($ocep_dir/samples/domains/helloworld_domain/defaultserver/modules/ext):
HelloWorldEvent is the Entity class and hence we need to add JPA annotations to it. I also added a long attribute named 'id' as the automatically generated id. Here is thefile that I used.
Now you need to define a persistence unit that knows about HelloWorldEvent. Here is a samplefile with persistence unit named 'helloworld' in it. This file must be inside META-INF directory of the application. In addition, manifest file of the application should be updated to import javax.persistence package and to include the entry below. Note that the persistence unit specified in the manifest must be the one defined in the persistence.xml file.
The last step is to modify HelloWorldBean class to persist the events as it receives them. To verify that the events are actually getting persisted, I also added code to query and count the events saved so far. Here is thefile that I used.
Note that I am using @Resource annotation to inject the datasource into HelloWorldBean. This datasource resource must be defined in OCEP server's config file. I used a derby datasource, defined as below in the server config file. Alternatively, you could specify the jdbc driver class, url etc. as javax.persistence.jdbc properties in the persistence unit.
That should be it! Now you should be able to run the application and see the count of events stored going up. Just one thing to note - due to some issues with OSGi support in EclipseLink, the application cannot be updated dynamically. If you do update the application at runtime, you will need to restart the server for the persistence code to work correctly.