Friday, 12 November 2010

JDeveloper 11g - Using Coherence Resource Adapater (RA) Within Weblogic 11g

The following demo is an example on how to use the Coherence RA within Weblogic 11g (10.3.3). In this example we are using Coherence 3.6 and this setup is based on having the RA as a stand alone application which my web based application developed in JDeveloper 11g ( would then use.

Steps for Setup in WLS 11g (10.3.3)

1. Place coherence.jar into $DOM_HOME/lib directory as shown below.

[oracle@wayne-p2 lib]$ pwd
[oracle@wayne-p2 lib]$ ls -la
total 5324
drwxr-x---   2 oracle oinstall    4096 Nov 10 13:17 .
drwxr-x---  13 oracle oinstall    4096 Nov 10 08:38 ..
-rw-r-----   1 oracle oinstall 5409133 Nov 10 13:17 coherence.jar
-rw-r-----   1 oracle oinstall     702 Nov  7 13:29 readme.txt
[oracle@wayne-p2 lib]$

2. Deploy $COH_HOME/lib/coherence-transaction.rar (Coherence 3.6) to the WLS 10.3.3. Accept all the defaults here during deployment BUT ensure you target it to the managed server you wish to use.

For information on how to use the Coherence RA you just deployed use the documentation
link below.
Using the Coherence Resource Adapter

Steps for Application Side (JDeveloper 11g -

In the steps below we just highlight what is needed from the JDeveloper project itself, rather then full steps on what to create. This demo was built using Struts/JSP.

1. In the web.xml create a resource reference as shown below.


2. Create a weblogic.xml file as follows. You add a new one to a web project using "File -> New -> General -> Deployment Descriptors -> Weblogic Deployment Descriptor"
<?xml version = '1.0' encoding = 'windows-1252'?>
<weblogic-web-app xmlns:xsi=""

Note: The JNDI resource description "tangosol.coherenceTx" is defined in the Coherence RA we deployed earlier. This maps back to our web.xml JNDI reference "eis/CoherenceTxCF".

3.The cache config file is included in the web project and so must be loaded at application startup. To do this we use a empty servlet and define this in the init() method as shown below.


import javax.servlet.*;
import javax.servlet.http.*;

public class CacheSetupServlet
  extends HttpServlet
  private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

  public void init(ServletConfig config) throws ServletException
    // servlet only required to ensure this application using our cache config
    System.out.println("** CacheSetupServlet.init() called **");

Note: coherence-tx-cache-config.xml is defined as follows
<?xml version="1.0"?>

<!DOCTYPE cache-config SYSTEM "cache-config.dtd">






4. In order for this HTTP Servlet to start up at application start up we define an entry in web.xml as follows.

5. Now we are ready to dpeloy our application which we can do from JDeveloper itself by creating a WAR deployment profile. A successful deployment is shown as follows

[09:08:54 AM] ----  Deployment started.  ----
[09:08:54 AM] Target platform is  (Weblogic 10.3).
[09:08:58 AM] Retrieving existing application information
[09:08:58 AM] Running dependency analysis...
[09:08:58 AM] Building...
[09:09:02 AM] Deploying profile...
[09:09:02 AM] Wrote Web Application Module to C:\jdev\jdevprod\11113\jdeveloper\jdev\mywork\CoherenceRAWebApplication\Demo\deploy\cohra-web.war
[09:09:02 AM] Redeploying Application...
[09:09:04 AM] [Deployer:149191]Operation 'deploy' on application 'cohra-web' is initializing on 'apple'
[09:09:05 AM] [Deployer:149192]Operation 'deploy' on application 'cohra-web' is in progress on 'apple'
[09:09:06 AM] [Deployer:149194]Operation 'deploy' on application 'cohra-web' has succeeded on 'apple'
[09:09:06 AM] Application Redeployed Successfully.
[09:09:06 AM] The following URL context root(s) were defined and can be used as a starting point to test your application:
[09:09:06 AM]
[09:09:06 AM] Elapsed time for deployment:  12 seconds
[09:09:06 AM] ----  Deployment finished.  ----

The Struts action which does the work here is as follows.


import com.tangosol.coherence.transaction.Connection;
import com.tangosol.coherence.transaction.ConnectionFactory;

import com.tangosol.coherence.transaction.OptimisticNamedCache;


import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class TestCohRAAction extends Action
  private final String DEPT_TX_CACHE = "tx-dept";
  private final String EMP_TX_CACHE = "tx-emp";
  private final String TX_SERVICE = "TestTxnService";
  /**This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
  public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
    throws IOException, ServletException
    InitialContext initCtx = null;
    ConnectionFactory factory = null;
    Connection coherenceTxConn = null;
      initCtx = getInititalContext();
      factory = (ConnectionFactory) 
      coherenceTxConn = factory.createConnection(TX_SERVICE);
      OptimisticNamedCache dept = coherenceTxConn.getNamedCache(DEPT_TX_CACHE);
      OptimisticNamedCache emp  = coherenceTxConn.getNamedCache(EMP_TX_CACHE);
      // Empty both caches we want to esnure no data exists prior to the run
      // as inserts will fail if they already exist
      dept.insert("10", "ACCOUNTING");
      emp.insert("1", "dept 10 : PAS");
      emp.insert("2", "dept 10 : LUCIA");
      emp.insert("3", "dept 10 : SIENA");
      emp.insert("4", "dept 10 : LUCAS");
      System.out.println(String.format("Size of dept cache = %s", dept.size()));
      System.out.println(String.format("Size of emp cache = %s", emp.size()));
         String.format("Size of dept cache = %s", dept.size()));

         String.format("Size of emp cache is %s", emp.size()));
    catch (Exception ex)
      if (coherenceTxConn != null)
    return mapping.findForward( "success");
  private InitialContext getInititalContext () throws NamingException
    return new InitialContext();

The resulting view page simply displays the cache sizes as part of the transaction once committed as shown below.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@ page contentType="text/html;charset=windows-1252"%>
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
  <title>Coherence RA Web Application</title>
<h2>Coherence RA Web Application</h2>

<p />
<hr />
<b>Oracle Support Services</b>


The JDeveloper project would look something like this.

No comments: