Thursday, February 16, 2012

Oracle NoSQL Database and Oracle SOA Suite 11g


In this blog post, I will show you how to use the new Oracle NoSQL database with Oracle SOA Suite 11g.  Specifically, we will cover:
  • How to download and install Oracle NoSQL database
  • Creating a Spring Component in Oracle SOA Suite for interacting with the database
  • Creating a BPEL process that can handle record creation, updates, and deletes. 

Background

There are many motivations for using a NoSQL database with Oracle SOA Suite.  The top ones for me are:
  • Very fast performance and highly scalable
  • Distributed data storage helps ensure fault tolerance
  • Simple programming model using key/value pairs
  • Availability of community versions eliminate licensing fees
  • Small memory and run time requirements 
I have used the CouchDB and Riak NoSQL databases in the past but really think the Oracle NoSQL database offers some compelling features.  For the following examples, I am going to use these specific versions of the software:
  • Oracle NoSQL 11gR2.1.2.123 Enterprise Edition
  • Oracle SOA Suite 11g 11.1.1.4.0
  • Oracle Weblogic Server 10.3.4.0
  • Oracle JDeveloper 11g 11.1.1.4.0
 
Installing Oracle NoSQL

The software and documentation are available here. There are a wide variety of installation choices that you can make, but the simplest installation is to copy the software to a server, and then launch it using the run-kvlite scripts.  Here's an example of the software running on a Windows environment.


You can verify your installation by starting the web-based administration tool using the admin port displayed when the software starts.  In the example shown above, the URL would http://localhost:5001

In order to use Oracle NoSQL with Oracle SOA Suite, you need to copy the kvclient-1.2.123.jar from the Oracle NoSQL installation directory to the domain directory used by the Weblogic server running Oracle SOA Suite.  In my case, the proper directory name is C:\Oracle\Middleware1034\user_projects\domains\soa\lib.  Afterwords, you will need to restart the Weblogic server in order for the new JAR file to be added to the class path.


Oracle SOA Suite Integration Example

The easiest way to use Oracle NoSQL with SOA Suite is to create a Spring Context Component that handles all interaction with the database.  You can then wire up the Spring Component with a Mediator or BPEL process and use typical invoke activities to perform various database actions.  

If you have never created a Spring Component in SOA Suite before, you might want to consult the excellent blog post here. In my case, I created a Spring Component named KVStore as shown below.



Then I created two new Java classes named KVStoreInterface.java and MyKVStore.java using the standard JDeveloper wizards.  You can get the full source code for the two Java classes here.








I added the following code to the KVStoreInterface.java file.  These methods will be used to provide basic database functions to the Mediator or BPEL process. 

public interface KVStoreInterface {
    String get(String key);
    Long put(String key, String value);
    Boolean delete(String key);
    Long getVersion(String key);   
}  


Next, I added references to these two Java classes to the Spring Component's XML file as shown below.

<?xml version="1.0" encoding="windows-1252" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:sca="http://xmlns.oracle.com/weblogic/weblogic-sca"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.5.xsd http://xmlns.oracle.com/weblogic/weblogic-sca META-INF/weblogic-sca.xsd">
 
  <!--Spring Bean definitions go here-->
  <bean class="nosqlproject.MyKVStore" id="impl" />
  <sca:service name="kvstore" target="impl" type="nosqlproject.KVStoreInterface" />
 
</beans>

I also added the kvclient-1.2.123.jar from the Oracle NoSQL database installation directory to the SOA composite application using the Project Properties menu.



BPEL process

Let's create a BPEL process that will be used to store Product details in the Oracle NoSQL database using the Spring component that was just created.

First, I created an XSD schema named Product.xsd that stores the definition of a product.  The ID element will be used to uniquely identify a product in the database.

<xsd:complexType name="ProductType">
    <xsd:annotation>
      <xsd:documentation>A sample product definition</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:element name="ID" type="xsd:string" nillable="false"/>
      <xsd:element name="Category" type="xsd:string" nillable="true"/>
      <xsd:element name="Name" type="xsd:string" nillable="true"/>
      <xsd:element name="Manufacturer" type="xsd:string" nillable="true"/>
      <xsd:element name="Price" type="xsd:decimal" nillable="true"/>
    </xsd:sequence>
  </xsd:complexType>

Second, I created two more XSD files named ProductRequest.xsd and ProductResponse.xsd that define the payload used by the BPEL process.  Next, I created a new BPEL process named ProductProcess.bpel as shown below.



 At this point, my application looks like this.



Then, I wire up the BPEL process and the Spring Component using the standard JDeveloper drag and drop methods.  At this point, JDeveloper will issue a warning statement and will compile the Java classes that we created in the previous steps.  When everything has compiled, the application will look like:



Inside the BPEL process, I created two Invoke activities that use the Spring component's Put and Get methods to add/update a record in the database and to retrieve it. 







 





The Oracle NoSQL database can store any object represented by a Byte Array as the record value.  The easiest method to convert the Product XML data to/from a Java String is to use the following built-in XPath functions:
  • getContentAsString - convert's an XML object to a String
  • parseEscapedXML - convert's a String object to XML


The example BPEL code uses these functions to interact with the KVStore Spring Component that we created earlier.  You might want to consider some other serialization method that offers better performance or small storage space.  For example, the open-source XStream project located might be worth investigating.
 Conclusion

These examples demonstrate how easy it is to use Oracle NoSQL with Oracle SOA Suite.  Oracle NoSQL provides other more sophisticated methods for structuring the data and querying it using major and minor keys.  Support for ensuring the consistency and performance of database operations are also provided and well documented.
 

No comments:

Post a Comment