Wednesday Jan 30, 2013

Accessing Coherence from OSB Pipeline

I recently got an request from customer that they want to invalidate the data in OSB ResultCache. As there is no way that we can create Key to invalidate the data, best way we can take is using filter. Here is the snippet of the code. 

 extern "C" void invalidate(char* key) {

    try {

        String::View vsCacheName = "/osb/service/ResultCache";

        NamedCache::Handle hCache = CacheFactory::getCache(vsCacheName);

        ValueExtractor::Handle hExtractor = KeyExtractor::create(ReflectionExtractor::create("getToken"));

        Filter::View vEqualsFilter = EqualsFilter::create(hExtractor, String::create(key));

        hCache->invokeAll(vEqualsFilter, ConditionalRemove::create(AlwaysFilter::getInstance(),true));

    } catch (const std::exception& e) {

        std::cerr << "error: " << e.what() << std::endl;

    }

}

One issue here is that we cannot specify which result of BusinessService should be invalidated. If two BusinessServices happen to have same token, This will remove both of them which sometimes is not desirable. Also, Index is not in place, so if size of cache is huge, we would expect the performance issue.Currently, I'm not aware of any way to make Key class. I tried making it by myself, but somehow, the one I make and generated by OSB is different.so I ended up making my own ResultCache logic to achieve this purpose.

--

public class ResultCacheHelper {
 private static String CACHE_NAME = "dist-test";
 
 public static void put(String key, org.apache.xmlbeans.XmlObject value) throws IOException {
 NamedCache cache = CacheFactory.getCache(CACHE_NAME);
 cache.put(key, serialize(value));
 CacheFactory.log("Cache Key : " + key + " is put in Cache!:" + serialize(value).toString());
 }
 
 public static org.apache.xmlbeans.XmlObject get(String key) throws IOException, ClassNotFoundException, XmlException {
 XmlObject result;
 NamedCache cache = CacheFactory.getCache(CACHE_NAME);
 
 byte[] bytes = (byte[]) cache.get(key);
 if(bytes == null) {
 String xmlNull = "<cache xmlns=\"http://com.oracle.demoapp.ws/\">null</cache>";
 result = org.apache.xmlbeans.XmlObject.Factory.parse(xmlNull);
 CacheFactory.log("Cache Key : " + key + " is not found in Cache!");
 } else  {
 result = (org.apache.xmlbeans.XmlObject)deserialize(bytes, Thread.currentThread().getContextClassLoader());
 CacheFactory.log("Cache Key : " + key + " is found in Cache!:"+result.toString());
 }
 
 return result;
 }
 
 protected static Object deserialize(byte[] bytes, final ClassLoader cl)
 throws IOException, ClassNotFoundException {
 ObjectInputStream ois = null;
 try {
 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
 ois = new ObjectInputStream(bais) {
 protected Class<?> resolveClass(ObjectStreamClass desc)
 throws IOException, ClassNotFoundException {
 return Class.forName(desc.getName(), false, cl);
 }
 };
 return ois.readObject();
 } finally {
 if (ois != null)
 ois.close();
 }
 }
 protected static byte[] serialize(Object object) throws IOException {
 ObjectOutputStream oos = null;
 try {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 oos = new ObjectOutputStream(baos);
 oos.writeObject(object);
 return baos.toByteArray();
 } finally {
 if (oos != null)
 oos.close();
 }
 }
}

--


Now I can just simply invalidate the cache

        String::View deleteKey = key;

        String::View vsCacheName = "dist-test";

        NamedCache::Handle hCache = CacheFactory::getCache(vsCacheName);

        hCache->remove(deleteKey);

 I believe that OSB invalidation feature will be added in near future. 

Tuesday Oct 04, 2011

Coherence*Web Session clustering between WebSphere and WebLogic

Briefly describing the needed steps to share session information between WebSphere 6.x and WebLogic 10.x

Steps for WLS 

1. java -cp coherence.jar;coherence-web.jar com.tangosol.coherence.servlet.WebPluginInstaller [WLS_HOME] -install

2. java -jar webInstaller.jar SessionTest.war -inspect -server:WebLogic/10.x

3. Modify coherence-web.xml 
    <param-name>coherence-session-cookie-domain</param-name>
    <param-value>.test.com</param-value>

<param-name>coherence-session-cookie-path</param-name>
    <param-value>/</param-value>

<param-name>coherence-servletcontext-clustered</param-name>
    <param-value>true</param-value>

    <param-name>coherence-enable-sessioncontext</param-name>
    <param-value>true</param-value>

<param-name>coherence-scopecontroller-class</param-name>
    <param-value>
       com.tangosol.coherence.servlet.AbstractHttpSessionCollection$GlobalScopeController
    </param-value>
<param-name>coherence-session-id-length</param-name>
    <param-value>32</param-value>

3. java -jar webInstaller.jar SessionTest.war -install

4. Insert those two classes(MyCookieHelper, MyDefaultFactory) into the SessionTest.war file.
----------------------------------------------------
package com.tangosol.coherence.servlet.weblogic81;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.oracle.FileLogWriter;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.cache.LocalCache;

public class MyCookieHelper extends CookieHelper {
// protected static final char SUFFIX_SEPARATOR = 46;
private LocalCache m_cacheFailover;
private String m_sConfiguredJvmRoute;
private FileLogWriter fileWriter;
public MyCookieHelper(
com.tangosol.coherence.servlet.SessionHelper.Factory factory,
ServletContext ctx) {
this(factory, ctx, null);
}

public MyCookieHelper(
com.tangosol.coherence.servlet.SessionHelper.Factory factory,
ServletContext ctx, String sJvmRoute) {
super(factory, ctx);
fileWriter = new FileLogWriter();
m_cacheFailover = new LocalCache(128);
m_sConfiguredJvmRoute = sJvmRoute;
}

public String decodeCookie(HttpServletRequest req) {
String sSessionId = super.decodeCookie(req);
CacheFactory.log("before :" + sSessionId);
fileWriter.writeToFile("before :" + sSessionId);
// WebLogic Session
if (sSessionId != null) {
int ofSeparator = sSessionId.indexOf('!');
if (ofSeparator >= 0) {
String sServerSuffix = getWebLogicAffinitrySuffix();
String sSessionSuffix = sSessionId.substring(ofSeparator);
sSessionId = sSessionId.substring(0, ofSeparator);
if (!sSessionSuffix.equals(sServerSuffix))
m_cacheFailover.put(sSessionId, sSessionSuffix);
}
}

// WebSphere Session
if ((sSessionId != null) && (sSessionId.length() > 4) &&(sSessionId.startsWith("0000"))) {
sSessionId = sSessionId.substring("0000".length());

int ofSeparator = sSessionId.indexOf(getWebSphereAffinitySuffix());
if (ofSeparator >= 0) {
String sServerSuffix = getWebSphereAffinitySuffix();
String sSessionSuffix = sSessionId.substring(ofSeparator);
sSessionId = sSessionId.substring(0, ofSeparator);

if (!sSessionSuffix.equals(sServerSuffix)) {
this.m_cacheFailover.put(sSessionId, sSessionSuffix);
}
}
}
CacheFactory.log("after :" + sSessionId);
fileWriter.writeToFile("before :" + sSessionId);
return sSessionId;
}

public String decodeUrl(HttpServletRequest request) {
String sSessionId = super.decodeUrl(request);
CacheFactory.log("before :" + sSessionId);
fileWriter.writeToFile("before :" + sSessionId);
if (sSessionId != null) {
int ofSeparator = sSessionId.indexOf('!');
if (ofSeparator >= 0)
sSessionId = sSessionId.substring(0, ofSeparator);
}
   if ((sSessionId != null) && (sSessionId.length() > 4) && (sSessionId.startsWith("0000")))
   {
     sSessionId = sSessionId.substring("0000".length());

     int ofSeparator = sSessionId.indexOf(getWebSphereAffinitySuffix());
     if (ofSeparator >= 0)
     {
       sSessionId = sSessionId.substring(0, ofSeparator);
     }
   }
   CacheFactory.log("after :" + sSessionId);
   fileWriter.writeToFile("before :" + sSessionId);
return sSessionId;
}

public String getWebSphereAffinitySuffix() {
return ":";
}

public String getWebLogicAffinitrySuffix() {
return "!";
}
}
----------------------------------------------------
package com.tangosol.coherence.servlet.weblogic103;

import com.tangosol.coherence.servlet.SessionHelper;
import com.tangosol.coherence.servlet.weblogic81.MyCookieHelper;
import javax.servlet.ServletContext;
import com.tangosol.net.CacheFactory;

public class MyDefaultFactory  extends DefaultFactory {

    public SessionHelper instantiateSessionHelper(ServletContext ctx) {
        CacheFactory.log(toString() + ": creating SessionHelper");
        azzert(ctx != null);
        azzert(getServletContext() == null && getSessionHelper() == null);
        String sJvmRoute = System.getProperty("jvmRoute", (String)ctx.getAttribute("coherence-jvmroute"));
        MyCookieHelper helper = new MyCookieHelper(this, ctx, sJvmRoute);
        setServletContext(ctx);
        setSessionHelper(helper);
        return helper;
    }
}
----------------------------------------------------

5. Modify the web.xml.
<param-name>coherence-factory-class</param-name>
    <param-value>com.tangosol.coherence.servlet.weblogic103.MyDefaultFactory</param-value>

Steps for WebSphere

Basically steps are very similar with WebLogic part except helper classes.

1. java -jar webInstaller.jar SessionTest.war -inspect -server:WebSphere/6.x

2. Modify coherence-web.xml
    <param-name>coherence-session-cookie-domain</param-name>
    <param-value>.test.com</param-value>

<param-name>coherence-session-cookie-path</param-name>
    <param-value>/</param-value>

<param-name>coherence-servletcontext-clustered</param-name>
    <param-value>true</param-value>

    <param-name>coherence-enable-sessioncontext</param-name>
    <param-value>true</param-value>

<param-name>coherence-scopecontroller-class</param-name>
    <param-value>
       com.tangosol.coherence.servlet.AbstractHttpSessionCollection$GlobalScopeController
    </param-value>
<param-name>coherence-session-id-length</param-name>
    <param-value>32</param-value>

3. java -jar webInstaller.jar SessionTest.war -install

4. Insert those two classes(MyCookieHelper, MyDefaultFactory) into the SessionTest.war file.
----------------------------------------------------
package com.tangosol.coherence.servlet.websphere60;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import com.oracle.FileLogWriter;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.cache.LocalCache;

public class MyCookieHelper extends com.tangosol.coherence.servlet.websphere60.CookieHelper {
protected static final char SUFFIX_SEPARATOR = 46;
private LocalCache m_cacheFailover;
private String m_sConfiguredJvmRoute;
private FileLogWriter fileWriter;
public MyCookieHelper(
com.tangosol.coherence.servlet.SessionHelper.Factory factory,
ServletContext ctx) {
this(factory, ctx, null);
}

public MyCookieHelper(
com.tangosol.coherence.servlet.SessionHelper.Factory factory,
ServletContext ctx, String sJvmRoute) {
super(factory, ctx);
fileWriter = new FileLogWriter();
m_cacheFailover = new LocalCache(128);
m_sConfiguredJvmRoute = sJvmRoute;
}

public String decodeCookie(HttpServletRequest req) {
String sSessionId = super.decodeCookie(req);
CacheFactory.log("before :" + sSessionId);
fileWriter.writeToFile("before :" + sSessionId);
// WebLogic Session
if (sSessionId != null) {
int ofSeparator = sSessionId.indexOf('!');
if (ofSeparator >= 0) {
String sServerSuffix = getWebLogicAffinitrySuffix();
String sSessionSuffix = sSessionId.substring(ofSeparator);
sSessionId = sSessionId.substring(0, ofSeparator);
if (!sSessionSuffix.equals(sServerSuffix))
m_cacheFailover.put(sSessionId, sSessionSuffix);
}
}

// WebSphere Session
if ((sSessionId != null) && (sSessionId.length() > 4) && (sSessionId.startsWith("0000"))) {
sSessionId = sSessionId.substring("0000".length());

int ofSeparator = sSessionId.indexOf(getWebSphereAffinitySuffix());
if (ofSeparator >= 0) {
String sServerSuffix = getWebSphereAffinitySuffix();
String sSessionSuffix = sSessionId.substring(ofSeparator);
sSessionId = sSessionId.substring(0, ofSeparator);

if (!sSessionSuffix.equals(sServerSuffix)) {
this.m_cacheFailover.put(sSessionId, sSessionSuffix);
}
}
}
CacheFactory.log("after :" + sSessionId);
fileWriter.writeToFile("before :" + sSessionId);
return sSessionId;
}

public String decodeUrl(HttpServletRequest request) {
String sSessionId = super.decodeUrl(request);
CacheFactory.log("before :" + sSessionId);
fileWriter.writeToFile("before :" + sSessionId);
if (sSessionId != null) {
int ofSeparator = sSessionId.indexOf('!');
if (ofSeparator >= 0)
sSessionId = sSessionId.substring(0, ofSeparator);
}
   if ((sSessionId != null) && (sSessionId.length() > 4) && (sSessionId.startsWith("0000")))
   {
     sSessionId = sSessionId.substring("0000".length());

     int ofSeparator = sSessionId.indexOf(getWebSphereAffinitySuffix());
     if (ofSeparator >= 0)
     {
       sSessionId = sSessionId.substring(0, ofSeparator);
     }
   }
   CacheFactory.log("after :" + sSessionId);
   fileWriter.writeToFile("before :" + sSessionId);
return sSessionId;
}

public String getWebSphereAffinitySuffix() {
return ":";
}

public String getWebLogicAffinitrySuffix() {
return "!";
}
}
----------------------------------------------------
package com.tangosol.coherence.servlet.websphere60;

import javax.servlet.ServletContext;

import com.tangosol.coherence.servlet.SessionHelper;
import com.tangosol.coherence.servlet.websphere60.DefaultFactory;
import com.tangosol.coherence.servlet.websphere60.MyCookieHelper;
import com.tangosol.net.CacheFactory;

public class MyDefaultFactory   extends com.tangosol.coherence.servlet.websphere60.DefaultFactory {
public MyDefaultFactory() {
}
    public SessionHelper instantiateSessionHelper(ServletContext ctx) {
        CacheFactory.log(toString() + ": creating SessionHelper");
        azzert(ctx != null);
        azzert(getServletContext() == null && getSessionHelper() == null);
        String sJvmRoute = System.getProperty("jvmRoute", (String)ctx.getAttribute("coherence-tomcat-jvmroute"));
        MyCookieHelper helper = new MyCookieHelper(this, ctx, sJvmRoute);
        setServletContext(ctx);
        setSessionHelper(helper);
        return helper;
    }
}
----------------------------------------------------

5. Modify the web.xml. 
    <param-name>coherence-factory-class</param-name>
    <param-value>com.tangosol.coherence.servlet.websphere60.MyDefaultFactory</param-value>


About

Solution & Technology Architect, Asia Pacific

Search

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