Tuesday, September 23, 2008

Replacing proxy information on the fly

This is another neat trick under Coherence's arm. Lets say you have an *Extend client that connects to a set of Coherence proxies. If the proxies for some reason are down and none is available and you do want to connect to another cluster that you know is up (like a DR), how could you connect to it without restarting the application or using any DNS Switch? Following is a way to replace the Extend configuration on the fly:


public void changeConfigOnTheFly (String host, String port) {
ConfigurableCacheFactory factory = CacheFactory.getConfigurableCacheFactory();
XmlElement cc = new SimpleElement ("cache-config");
XmlElement rcs = cc.addElement("caching-schemes")
.addElement ("remote-cache-scheme");
rcs.addElement("service-name")
.setString("ExtendTcpCacheService");
XmlElement xE = rcs.addElement("initiator-config")
.addElement("tcp-initiator")
.addElement("remote-addresses");
XmlElement rAs = xE.addElement("socket-address");
XmlElement add = rAs.addElement("address");
add.setString(host);
XmlElement port = rAs.addElement("port");
port.setString(port);

// -- Now set the new XmlElement
factory.setConfig(cc);

// -- And of course you need to restart the Cache Service thread
factory.ensureService("ExtendTcpCacheService").stop ();
factory.ensureService("ExtendTcpCacheService").start ();
}

Yay! And you can keep your *Extend cache configuration simple and tidy by replacing the proxy configuration on the fly. Something is missing in this configuration though - The cache mappings. Actually you do not need it. As long as cache schemes are defined, caches too can be created on the fly:

CacheService cS = (CacheService)CacheFactory.getService("ExtendTcpCacheService");
NamedCache sCache = cS.ensureCache("MyCache", null);

Don't you just love Oracle Coherence?

No comments: