Using EclipseLink from OCEP

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):

- javax.persistence_2.0.1.v201006031150.jar

- org.eclipse.persistence.antlr_2.1.2.v20101206-r8635.jar

- org.eclipse.persistence.asm_2.1.2.v20101206-r8635.jar

- org.eclipse.persistence.core_2.1.2.v20101206-r8635.jar

- org.eclipse.persistence.jpa.osgi_2.1.2.v20101206-r8635.jar

- org.eclipse.persistence.jpa_2.1.2.v20101206-r8635.jar

- org.eclipse.persistence.oracle_2.1.2.v20101206-r8635.jar

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 the

HelloWorldEvent.java
file that I used.

Now you need to define a persistence unit that knows about HelloWorldEvent. Here is a sample

persistence.xml
file 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.
JPA-PersistenceUnits: helloworld

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 the

HelloWorldBean.java
file 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.

<data-source>
<name>derbyDS</name>
<connection-pool-params>
<initial-capacity>5</initial-capacity>
<max-capacity>5</max-capacity>
</connection-pool-params>
<driver-params>
<url>jdbc:derby:dbtest1;create=true</url>
<driver-name>org.apache.derby.jdbc.EmbeddedDriver </driver-params>
</data-source>

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.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

bocadmin_ww

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today