Showing posts with label coherence. Show all posts
Showing posts with label coherence. Show all posts
Monday, September 9, 2013
Coherence Data Affinity Part 2 using KeyAssociator
In the earlier section we defined the association in the key object by implementing KeyAssociation.
The other way to do it is by writing a class that implements KeyAssociator and configuring that in cache-config.xml.
1. In this technique we will first create a key class. The difference between the earlier key and the current key is. The earlier one implements KeyAssociation but the current one doesn't.
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import java.io.Serializable;
@Portable
public class OrderLineItemKeyBean implements Serializable
{
@PortableProperty
private String mOrderNumber;
@PortableProperty
private String mLineItemNumber;
public OrderLineItemKeyBean()
{
super();
}
public OrderLineItemKeyBean(String pOrderNumber, String pLineItemNumber)
{
super();
this.mOrderNumber = pOrderNumber;
this.mLineItemNumber = pLineItemNumber;
}
public void setOrderNumber(String pOrderNumber)
{
this.mOrderNumber = pOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setLineItemNumber(String pLineItemNumber)
{
this.mLineItemNumber = pLineItemNumber;
}
public String getLineItemNumber()
{
return mLineItemNumber;
}
public String toString()
{
return mOrderNumber +" "+ mLineItemNumber;
}
}
2. Lets now create a KeyAssociator class. This class actually implements KeyAssociator and is configured in the cache-config.xml. The getAssociatedKey method actually returns the associated key.
package com.coher.dataaffinity;
import com.coher.dto.OrderLineItemKeyBean;
import com.tangosol.net.PartitionedService;
import com.tangosol.net.partition.KeyAssociator;
public class MyKeyAssociator
implements KeyAssociator
{
public MyKeyAssociator()
{
super();
}
public void init(PartitionedService partitionedService)
{
}
public Object getAssociatedKey(Object object)
{
if (object instanceof OrderLineItemKeyBean)
{
return ((OrderLineItemKeyBean) object).getOrderNumber();
}
else
{
return object;
}
}
}
3. Configure the KeyAssociator class in cache-config.xml
<?xml version="1.0" ?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">
<scope-name>OneSystemScope</scope-name>
<defaults>
<serializer>pof</serializer>
</defaults>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>mycache</cache-name>
<scheme-name>dist-default</scheme-name>
<init-params>
<init-param>
<param-name>size-limit</param-name>
<param-value>5000</param-value>
</init-param>
</init-params>
</cache-mapping>
<cache-mapping>
<cache-name>DistributedOrders</cache-name>
<scheme-name>dist-default</scheme-name>
<init-params>
<init-param>
<param-name>size-limit</param-name>
<param-value>5000</param-value>
</init-param>
</init-params>
</cache-mapping>
<cache-mapping>
<cache-name>DistributedOrderLines</cache-name>
<scheme-name>dist-default</scheme-name>
<init-params>
<init-param>
<param-name>size-limit</param-name>
<param-value>5000</param-value>
</init-param>
</init-params>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<distributed-scheme>
<scheme-name>dist-default</scheme-name>
<service-name>DistributedCache</service-name>
<thread-count>5</thread-count>
<backup-count>1</backup-count>
<backup-storage>
<type>file-mapped</type>
<initial-size>1M</initial-size>
<maximum-size>5G</maximum-size>
<directory>/software/CoherenceInsall/coherence/backup</directory>
</backup-storage>
<key-associator>
<class-name>com.coher.dataaffinity.MyKeyAssociator</class-name>
</key-associator> <backing-map-scheme>
<overflow-scheme>
<scheme-name>LocalMemoryWithDiskOverflow</scheme-name>
<front-scheme>
<local-scheme>
<high-units>300000</high-units>
</local-scheme>
</front-scheme>
<back-scheme>
<external-scheme>
<scheme-name>DiskScheme</scheme-name>
<nio-file-manager>
<initial-size>1MB</initial-size>
<maximum-size>1024MB</maximum-size>
<directory>/software/CoherenceInstall/coherence/backup</directory>
</nio-file-manager>
</external-scheme>
</back-scheme>
</overflow-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
<invocation-scheme>
<scheme-name>InvocationService</scheme-name>
<service-name>InvocationService</service-name>
<thread-count>5</thread-count>
<autostart>true</autostart>
</invocation-scheme>
</caching-schemes>
</cache-config>
4. Add the key class to pof-config.xml. If you are using POF.
5. Create a cache loader class to test
package com.coher.dataaffinity;
import com.coher.dto.OrderBean;
import com.coher.dto.OrderLineItemBean;
import com.coher.dto.OrderLineItemKeyBean;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import java.util.Random;
public class DataAffinityLoader
{
public DataAffinityLoader()
{
super();
}
public static void main(String[] args)
{
NamedCache orderCache = CacheFactory.getCache("DistributedOrders");
NamedCache orderLineCache =
CacheFactory.getCache("DistributedOrderLines");
orderCache.clear();
orderLineCache.clear();
int k = 0;
for (int i = 0; i < 4; i++)
{
OrderBean ob = new OrderBean("" + i, "Customer" + i);
orderCache.put(ob.getOrderNumber(), ob);
Random r = new Random();
int l = r.nextInt(20);
for (int j = 0; j <= 3; j++)
{
k++;
OrderLineItemBean olib =
new OrderLineItemBean(ob.getOrderNumber(), "lineitem" + k,
"" + k, k);
orderLineCache.put(new OrderLineItemKeyBean(ob.getOrderNumber(),
olib.getItemNumber()), olib);
}
}
}
}
6. Create a class to test the affinity
package com.coher.dataaffinity;
import com.coher.dto.OrderLineItemKeyBean;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Member;
import com.tangosol.net.NamedCache;
import com.tangosol.net.PartitionedService;
import com.tangosol.util.Filter;
import com.tangosol.util.filter.EqualsFilter;
import com.tangosol.util.filter.KeyAssociatedFilter;
import java.util.Set;
public class DataAffinityCacheViewer
{
public DataAffinityCacheViewer()
{
super();
}
public static void main(String[] args)
{
NamedCache orderCache = CacheFactory.getCache("DistributedOrders");
NamedCache orderLineCache = CacheFactory.getCache("DistributedOrderLines");
System.out.println("orders Cache.size() = " + orderCache.size());
PartitionedService ps1 = (PartitionedService) orderCache.getCacheService();
Set<String> odrKeySet = (Set<String>) orderCache.keySet();
for (String key: odrKeySet)
{
Member member = ps1.getKeyOwner(key);
System.out.println("Coherence member:" + member.getId() + "; order key:" + key );
EqualsFilter filterEq = new EqualsFilter("getOrderNumber", key);
Filter filterAsc = new KeyAssociatedFilter(filterEq, key);
Set<OrderLineItemKeyBean> ONKeySet = (Set<OrderLineItemKeyBean>) orderLineCache.keySet(filterAsc);
PartitionedService ps2 = (PartitionedService) orderLineCache.getCacheService();
for (OrderLineItemKeyBean key1: ONKeySet)
{
Member member1 = ps2.getKeyOwner(key1);
System.out.println(" Coherence member:" + member1.getId() + "; Order Line Key:" + key1 );
}
}
}
}
Running the test.
a. Start two instances of coherence
b. first run DataAffinityLoader to load data
c. Now run DataAffinityCacheViewer to test affinity
orders Cache.size() = 4
Coherence member:3; order key:0
Coherence member:3; Order Line Key:0 lineitem4
Coherence member:3; Order Line Key:0 lineitem1
Coherence member:3; Order Line Key:0 lineitem3
Coherence member:3; Order Line Key:0 lineitem2
Coherence member:3; order key:1
Coherence member:3; Order Line Key:1 lineitem7
Coherence member:3; Order Line Key:1 lineitem8
Coherence member:3; Order Line Key:1 lineitem6
Coherence member:3; Order Line Key:1 lineitem5
Coherence member:1; order key:2
Coherence member:1; Order Line Key:2 lineitem12
Coherence member:1; Order Line Key:2 lineitem11
Coherence member:1; Order Line Key:2 lineitem9
Coherence member:1; Order Line Key:2 lineitem10
Coherence member:1; order key:3
Coherence member:1; Order Line Key:3 lineitem14
Coherence member:1; Order Line Key:3 lineitem15
Coherence member:1; Order Line Key:3 lineitem16
Coherence member:1; Order Line Key:3 lineitem13
Wednesday, July 10, 2013
Coherence Data Affinity Part 1 using KeyAssociation
Data affinity can be used to keep related objects in the same partition. If you have an order and line item caches, We can use this to keep related objects together thus increasing the efficiency of coherence.
The objects can be unevenly distributed if the child keys are not unique.
This can be done in two ways
1. Using a Key that implements KeyAssociation Interface in the Child Cache
using this method is illustrated below
2. Creating a class that implements KeyAssociator and configuring it CacheConfig.xml
Below is an illustration of how to achieve it using KeyAssociation
1. Lets create a Order class
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import java.io.Serializable;
@Portable
public class OrderBean
implements Serializable
{
public OrderBean()
{
super();
}
@PortableProperty
private String mOrderNumber;
@PortableProperty
private String mCustomerName;
public OrderBean(String mOrderNumber, String mCustomerName)
{
super();
this.mOrderNumber = mOrderNumber;
this.mCustomerName = mCustomerName;
}
public void setOrderNumber(String mOrderNumber)
{
this.mOrderNumber = mOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setCustomerName(String mCustomerName)
{
this.mCustomerName = mCustomerName;
}
public String getCustomerName()
{
return mCustomerName;
}
}
2. Lets Create a line item class
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import java.io.Serializable;
@Portable
public class OrderLineItemBean
implements Serializable
{
public OrderLineItemBean()
{
super();
}
@PortableProperty
String mLineItemNumber;
@PortableProperty
String mOrderNumber;
@PortableProperty
String mItemName;
@PortableProperty
Integer mQuantity;
public OrderLineItemBean(String pOrderNumber, String pLineItemNumber,
String pItemName, Integer pQuantity)
{
super();
this.mOrderNumber = pOrderNumber;
this.mLineItemNumber = pLineItemNumber;
this.mItemName = pItemName;
this.mQuantity = pQuantity;
}
public void setOrderNumber(String pOrderNumber)
{
this.mOrderNumber = pOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setItemNumber(String pLineItemNumber)
{
this.mLineItemNumber = pLineItemNumber;
}
public String getItemNumber()
{
return mLineItemNumber;
}
public void setItemName(String pItemName)
{
this.mItemName = pItemName;
}
public String getItemName()
{
return mItemName;
}
public void setQuantity(Integer pQuantity)
{
this.mQuantity = pQuantity;
}
public Integer getQuantity()
{
return mQuantity;
}
public String toString()
{
return mLineItemNumber + " - " + mOrderNumber + " - " + mItemName +
" - " + mQuantity;
}
}
3. Create a key association.
This class is used as a key for the detail cache entries and
package com.coher.dto;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import com.tangosol.net.cache.KeyAssociation;
import java.io.Serializable;
@Portable
public class OrderNumberKey implements KeyAssociation, Serializable
{
@PortableProperty
private String mOrderNumber;
@PortableProperty
private String mLineItemNumber;
public OrderNumberKey()
{
super();
}
public OrderNumberKey(String pOrderNumber, String pLineItemNumber)
{
super();
this.mOrderNumber = pOrderNumber;
this.mLineItemNumber = pLineItemNumber;
}
public Object getAssociatedKey()
{
return getOrderNumber();
}
public void setOrderNumber(String pOrderNumber)
{
this.mOrderNumber = pOrderNumber;
}
public String getOrderNumber()
{
return mOrderNumber;
}
public void setLineItemNumber(String pLineItemNumber)
{
this.mLineItemNumber = pLineItemNumber;
}
public String getLineItemNumber()
{
return mLineItemNumber;
}
public String toString()
{
return mOrderNumber +" "+ mLineItemNumber;
}
}
4. Package the jar and place it in the classpath of coherence server
5. Lets create a class that loads the cache. In this cache when loading the detail objects into the cache we use the Key Association we created above in Step 3.
package com.coher.dataaffinity;
import com.coher.dto.OrderBean;
import com.coher.dto.OrderLineItemBean;
import com.coher.dto.OrderNumberKey;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import java.util.Random;
public class DataAffinityLoader
{
public DataAffinityLoader()
{
super();
}
public static void main(String[] args)
{
NamedCache orderCache = CacheFactory.getCache("DistributedOrders");
NamedCache orderLineCache =
CacheFactory.getCache("DistributedOrderLines");
orderCache.clear();
orderLineCache.clear();
int k = 0;
for (int i = 0; i < 4; i++)
{
OrderBean ob = new OrderBean("" + i, "Customer" + i);
orderCache.put(ob.getOrderNumber(), ob);
Random r = new Random();
int l = r.nextInt(20);
for (int j = 0; j <= 3; j++)
{
k++;
OrderLineItemBean olib =
new OrderLineItemBean(ob.getOrderNumber(), "lineitem" + k,
"" + k, k);
orderLineCache.put(new OrderNumberKey(ob.getOrderNumber(),
olib.getItemNumber()), olib);
}
}
}
}
6. Lets create a class that reads and shows us the distribution
package com.coher.dataaffinity;
import com.coher.dto.OrderNumberKey;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Member;
import com.tangosol.net.NamedCache;
import com.tangosol.net.PartitionedService;
import com.tangosol.util.Filter;
import com.tangosol.util.filter.EqualsFilter;
import com.tangosol.util.filter.KeyAssociatedFilter;
import java.util.Set;
public class DataAffinityCacheViewer
{
public DataAffinityCacheViewer()
{
super();
}
public static void main(String[] args)
{
NamedCache orderCache = CacheFactory.getCache("DistributedOrders");
NamedCache orderLineCache = CacheFactory.getCache("DistributedOrderLines");
System.out.println("orders Cache.size() = " + orderCache.size());
PartitionedService ps1 = (PartitionedService) orderCache.getCacheService();
Set<String> odrKeySet = (Set<String>) orderCache.keySet();
for (String key: odrKeySet)
{
Member member = ps1.getKeyOwner(key);
System.out.println("Coherence member:" + member.getId() + "; order key:" + key );
EqualsFilter filterEq = new EqualsFilter("getOrderNumber", key);
Filter filterAsc = new KeyAssociatedFilter(filterEq, key);
Set<OrderNumberKey> ONKeySet = (Set<OrderNumberKey>) orderLineCache.keySet(filterAsc);
PartitionedService ps2 = (PartitionedService) orderLineCache.getCacheService();
for (OrderNumberKey key1: ONKeySet)
{
Member member1 = ps2.getKeyOwner(key1);
System.out.println(" Coherence member:" + member1.getId() + "; Order Line Key:" + key1 );
}
}
}
}
7. Start the caches
8. Run the Data affinity loader to load the cache
9. Run the cache viewer to read the cache
Coherence member:1; order key:2
Coherence member:1; Order Line Key:2 lineitem9
Coherence member:1; Order Line Key:2 lineitem12
Coherence member:1; Order Line Key:2 lineitem11
Coherence member:1; Order Line Key:2 lineitem10
Coherence member:1; order key:3
Coherence member:1; Order Line Key:3 lineitem14
Coherence member:1; Order Line Key:3 lineitem15
Coherence member:1; Order Line Key:3 lineitem13
Coherence member:1; Order Line Key:3 lineitem16
Coherence member:5; order key:0
Coherence member:5; Order Line Key:0 lineitem3
Coherence member:5; Order Line Key:0 lineitem2
Coherence member:5; Order Line Key:0 lineitem4
Coherence member:5; Order Line Key:0 lineitem1
Coherence member:5; order key:1
Coherence member:5; Order Line Key:1 lineitem5
Coherence member:5; Order Line Key:1 lineitem8
Coherence member:5; Order Line Key:1 lineitem7
Coherence member:5; Order Line Key:1 lineitem6
rohith raj puchalapalli
Thursday, June 20, 2013
Coherence Distributed Bulk Cache Loading using Invocation Service
When Loading a large amounts of cache, It is efficient to divide the load among the cache members. Below is a demonstration to do this.
My example assumes that you keep the files in the below directory Structure
Coherence Extract Directory
-----\Coherence Extract Directory
---\applib
---\CoherenceWork.jar
---\projectLoader.jar
---\bin
---\doc
---\lib
---\run
---\CacheConfig.xml
---\CacheOverride.xml
---\cacheproxy1.cmd
---\CacheProxyOverride.xml
---\cacheserver.cmd
---\pof-config.xml
---\ProxyConfig1.xml
1. Create cache operational config override file
CacheOverride.xml
<?xml version="1.0" ?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd">
<cluster-config>
<member-identity>
<cluster-name system-property="tangosol.coherence.cluster">DataCluster</cluster-name>
<process-name system-property="tangosol.coherence.process">CoherenceCacheServer</process-name>
<role-name system-property="tangosol.coherence.role">CacheServer</role-name>
</member-identity>
<unicast-listener>
<address system-property="tangosol.coherence.localhost">localhost</address>
<port system-property="tangosol.coherence.localport">8088</port>
<port-auto-adjust system-property="tangosol.coherence.localport.adjust">true</port-auto-adjust>
</unicast-listener>
</cluster-config>
<logging-config>
<destination system-property="tangosol.coherence.log">stdout</destination>
<severity-level system-property="tangosol.coherence.log.level">9</severity-level>
</logging-config>
</coherence>
2. Lets create a Cache Config file CacheConfig.xml
<?xml version="1.0" ?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">
<scope-name>OneSystemScope</scope-name>
<defaults>
<serializer>pof</serializer>
</defaults>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>mycache</cache-name>
<scheme-name>dist-default</scheme-name>
<init-params>
<init-param>
<param-name>size-limit</param-name>
<param-value>5000</param-value>
</init-param>
</init-params>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<distributed-scheme>
<scheme-name>dist-default</scheme-name>
<service-name>DistributedCache</service-name>
<thread-count>5</thread-count>
<backup-count>1</backup-count>
<backup-storage>
<type>file-mapped</type>
<initial-size>1M</initial-size>
<maximum-size>5G</maximum-size>
<directory>/software/CoherenceInsall/coherence/backup</directory>
</backup-storage>
<backing-map-scheme>
<overflow-scheme>
<scheme-name>LocalMemoryWithDiskOverflow</scheme-name>
<front-scheme>
<local-scheme>
<high-units>300000</high-units>
</local-scheme>
</front-scheme>
<back-scheme>
<external-scheme>
<scheme-name>DiskScheme</scheme-name>
<nio-file-manager>
<initial-size>1MB</initial-size>
<maximum-size>1024MB</maximum-size>
<directory>/software/CoherenceInstall/coherence/backup</directory>
</nio-file-manager>
</external-scheme>
</back-scheme>
</overflow-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
<invocation-scheme>
<scheme-name>InvocationService</scheme-name>
<service-name>InvocationService</service-name>
<thread-count>5</thread-count>
<autostart>true</autostart>
</invocation-scheme>
</caching-schemes>
</cache-config>
3. Create a cmd to start cache instances cacheserver.cmd
@echo off
@
@rem This will start a cache server
@
setlocal
:config
@rem specify the Coherence installation directory
set coherence_home=%~dp0\..
@rem specify the JVM heap size
set memory=512m
:start
if not exist "%coherence_home%\lib\coherence.jar" goto instructions
if "%java_home%"=="" (set java_exec=java) else (set java_exec=%java_home%\bin\java)
:launch
if "%1"=="-jmx" (
set jmxproperties=-Dcom.sun.management.jmxremote -Dtangosol.coherence.management=all -Dcom.sun.management.jmxremote.port=7771 -Dtangosol.coherence.management.remote=true
shift
)
set java_opts=-Xms%memory% -Xmx%memory% %jmxproperties%
%java_exec% -server -showversion %java_opts% -Dtangosol.coherence.override=CacheOverride.xml -Dtangosol.coherence.distributed.localstorage=true -Dtangosol.coherence.cacheconfig=CacheConfig.xml -Dtangosol.coherence.edition=EE -cp "%coherence_home%\lib\coherence.jar";"%coherence_home%\applib\projectLoader.jar";"%coherence_home%\applib\CoherenceWork.jar" com.tangosol.net.DefaultCacheServer %1
goto exit
:instructions
echo Usage:
echo ^<coherence_home^>\bin\cache-server.cmd
goto exit
:exit
endlocal
@echo on
4 Create an Override file for the Proxy CacheProxyOverride.xml
<?xml version="1.0" ?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd">
<cluster-config>
<member-identity>
<cluster-name system-property="tangosol.coherence.cluster">DataCluster</cluster-name>
<process-name system-property="tangosol.coherence.process">CoherenceCacheServer</process-name>
<role-name system-property="tangosol.coherence.role">CacheProxy</role-name>
</member-identity>
<unicast-listener>
<address system-property="tangosol.coherence.localhost">localhost</address>
<port system-property="tangosol.coherence.localport">8088</port>
<port-auto-adjust system-property="tangosol.coherence.localport.adjust">true</port-auto-adjust>
</unicast-listener>
</cluster-config>
<logging-config>
<destination system-property="tangosol.coherence.log">stdout</destination>
<severity-level system-property="tangosol.coherence.log.level">9</severity-level>
</logging-config>
</coherence>
5. Create a cache config for the proxy. ProxyConfig1.xml
<?xml version="1.0" ?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">
<scope-name>OneSystemScope</scope-name>
<defaults>
<serializer>pof</serializer>
</defaults>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>mycache</cache-name>
<scheme-name>dist-default</scheme-name>
<init-params>
<init-param>
<param-name>size-limit</param-name>
<param-value>5000</param-value>
</init-param>
</init-params>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<distributed-scheme>
<scheme-name>dist-default</scheme-name>
<service-name>DistributedCache</service-name>
<thread-count>5</thread-count>
<backup-count>1</backup-count>
<backup-storage>
<type>file-mapped</type>
<initial-size>1M</initial-size>
<maximum-size>5G</maximum-size>
<directory>/software/CoherenceInsall/coherence/backup</directory>
</backup-storage>
<backing-map-scheme>
<overflow-scheme>
<scheme-name>LocalMemoryWithDiskOverflow</scheme-name>
<front-scheme>
<local-scheme>
<high-units>300000</high-units>
</local-scheme>
</front-scheme>
<back-scheme>
<external-scheme>
<scheme-name>DiskScheme</scheme-name>
<nio-file-manager>
<initial-size>1MB</initial-size>
<maximum-size>1024MB</maximum-size>
<directory>/software/CoherenceInstall/coherence/backup</directory>
</nio-file-manager>
</external-scheme>
</back-scheme>
</overflow-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
<proxy-scheme>
<service-name>ExtendTcpProxyService</service-name>
<thread-count>5</thread-count>
<acceptor-config>
<tcp-acceptor>
<local-address>
<address>localhost</address>
<port>9098</port>
</local-address>
</tcp-acceptor>
<serializer>
<instance>
<class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
<init-params>
<init-param>
<param-type>String</param-type>
<param-value>pof-config.xml</param-value>
</init-param>
</init-params>
</instance>
</serializer>
</acceptor-config>
<proxy-config>
<cache-service-proxy>
<enabled>true</enabled>
</cache-service-proxy>
<invocation-service-proxy>
<enabled>true</enabled>
</invocation-service-proxy>
</proxy-config>
<autostart>true</autostart>
</proxy-scheme>
<invocation-scheme>
<scheme-name>InvocationService</scheme-name>
<service-name>InvocationService</service-name>
<thread-count>5</thread-count>
<autostart>true</autostart>
</invocation-scheme>
</caching-schemes>
</cache-config>
6. Create a command to start the proxy server cacheproxy1.cmd
@echo off
@
@rem This will start a cache server
@
setlocal
:config
@rem specify the Coherence installation directory
set coherence_home=%~dp0\..
@rem specify the JVM heap size
set memory=512m
:start
if not exist "%coherence_home%\lib\coherence.jar" goto instructions
if "%java_home%"=="" (set java_exec=java) else (set java_exec=%java_home%\bin\java)
:launch
if "%1"=="-jmx" (
set jmxproperties=-Dcom.sun.management.jmxremote -Dtangosol.coherence.management=all -Dcom.sun.management.jmxremote.port=7772 -Dtangosol.coherence.management.remote=true
shift
)
set java_opts=-Xms%memory% -Xmx%memory% %jmxproperties%
%java_exec% -server -showversion %java_opts% -Dtangosol.coherence.override=CacheProxyOverride.xml -Dtangosol.coherence.distributed.localstorage=false -Dtangosol.coherence.cacheconfig=ProxyConfig1.xml -Dtangosol.coherence.edition=EE -cp "%coherence_home%\lib\coherence.jar";"%coherence_home%\applib\CoherenceWork.jar";"%coherence_home%\applib\projectLoader.jar" com.tangosol.net.DefaultCacheServer %1
goto exit
:instructions
echo Usage:
echo ^<coherence_home^>\bin\cache-server.cmd
goto exit
:exit
endlocal
@echo on
Now we have all the cache cluster configurations ready. We now need to write the client programs and configurations
7. Create a pof-config.xml place a copy of this in the run directory
<?xml version="1.0" encoding="UTF-8" ?>
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config/1.1/coherence-pof-config.xsd">
<user-type-list>
<include>coherence-pof-config.xml</include>
<user-type>
<type-id>1001</type-id>
<class-name>com.coher.dto.OrderBean</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
<user-type>
<type-id>1002</type-id>
<class-name>com.coher.loader.LoaderAgent</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
</user-type-list>
<allow-interfaces>true</allow-interfaces>
<allow-subclasses>true</allow-subclasses>
</pof-config>
8. Create a local cache config
<?xml version="1.0" ?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="coherence-cache-config.xsd">
<scope-name>OneSystemScope</scope-name>
<defaults>
<serializer>pof</serializer>
</defaults>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>mycache</cache-name>
<scheme-name>extend-dist</scheme-name>
<init-params>
<init-param>
<param-name>size-limit</param-name>
<param-value>5000</param-value>
</init-param>
</init-params>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<remote-cache-scheme>
<scheme-name>extend-dist</scheme-name>
<service-name>ExtendTcpCacheService</service-name>
<initiator-config>
<tcp-initiator>
<remote-addresses>
<!-- include a socket-address for each distributed tier cluster node -->
<socket-address>
<address>10.10.2.106</address>
<port>9098</port>
</socket-address>
<socket-address>
<address>10.10.2.106</address>
<port>9099</port>
</socket-address>
</remote-addresses>
<connect-timeout>10s</connect-timeout>
</tcp-initiator>
<outgoing-message-handler>
<request-timeout>60s</request-timeout>
</outgoing-message-handler>
<serializer>
<instance>
<class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
<init-params>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>pof-config.xml</param-value>
</init-param>
</init-params>
</instance>
</serializer>
</initiator-config>
</remote-cache-scheme>
<!-- the remote invocation scheme configuration defines the remote connection
interface between the front and back caches for the near cache. It enables
the near cache to perform operations as well as get update events. -->
<remote-invocation-scheme>
<scheme-name>extend-invocation</scheme-name>
<service-name>ExtendTcpInvocationService</service-name>
<initiator-config>
<tcp-initiator>
<remote-addresses>
<!-- include a socket-address for each distributed tier cluster node -->
<socket-address>
<address>10.10.2.106</address>
<port>9098</port>
</socket-address>
<socket-address>
<address>10.10.2.106</address>
<port>9099</port>
</socket-address>
</remote-addresses>
<connect-timeout>60s</connect-timeout>
</tcp-initiator>
<outgoing-message-handler>
<request-timeout>20s</request-timeout>
</outgoing-message-handler>
<serializer>
<instance>
<class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
<init-params>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>pof-config.xml</param-value>
</init-param>
</init-params>
</instance>
</serializer>
</initiator-config>
</remote-invocation-scheme>
<invocation-scheme>
<scheme-name>InvocationService</scheme-name>
<service-name>InvocationService</service-name>
<thread-count>5</thread-count>
<autostart>true</autostart>
</invocation-scheme>
</caching-schemes>
</cache-config>
9. Create a local Cache Override file
<?xml version="1.0" ?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-operational-config coherence-operational-config.xsd">
<cluster-config>
<member-identity>
<cluster-name system-property="tangosol.coherence.cluster">DataCluster</cluster-name>
<process-name system-property="tangosol.coherence.process">CoherenceCacheServer</process-name>
<role-name system-property="tangosol.coherence.role">CacheClient</role-name>
</member-identity>
</cluster-config>
<logging-config>
<destination system-property="tangosol.coherence.log">stdout</destination>
<severity-level system-property="tangosol.coherence.log.level">9</severity-level>
</logging-config>
</coherence>
10 Creating Invokable class that extends AbstractInvocable
package com.coher.loader;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import com.tangosol.net.AbstractInvocable;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import java.io.Serializable;
@Portable
public class LoaderAgent extends AbstractInvocable implements Serializable
{
@PortableProperty
private String mFromOrderNumber;
@PortableProperty private String mToOrderNumber;
public LoaderAgent()
{
super();
}
public LoaderAgent(String pFromOrderNumber, String pToOrderNumber)
{
super();
this.mFromOrderNumber = pFromOrderNumber;
this.mToOrderNumber = pToOrderNumber;
}
public void run()
{
NamedCache cache = CacheFactory.getCache("mycache");
// Write logic to query and load caches here
System.out.println(" ----------------------------------------------------------------------- ");
System.out.println("Loading Cache for Orders from " +mFromOrderNumber+ " to "+ mToOrderNumber);
System.out.println(" ----------------------------------------------------------------------- ");
}
public void setFromOrderNumber(String pFromOrderNumber)
{
this.mFromOrderNumber = pFromOrderNumber;
}
public String getFromOrderNumber()
{
return mFromOrderNumber;
}
public void setToOrderNumber(String pToOrderNumber)
{
this.mToOrderNumber = pToOrderNumber;
}
public String getToOrderNumber()
{
return mToOrderNumber;
}
}
11. Create a class that can observe Execution that implements InvocationObserver
package com.coher.loader;
import com.tangosol.net.InvocationObserver;
import com.tangosol.net.Member;
public class LoaderObserver
implements InvocationObserver
{
public LoaderObserver()
{
super();
}
public void memberCompleted(Member member, Object object)
{
System.out.println("Received membercompleted notification from Member: " + member + ", Result: " + object);
}
public void memberFailed(Member member, Throwable throwable)
{
System.out.println("Received member failed notification from Member: " + member.getId() + ", exception " + throwable);
}
public void memberLeft(Member member)
{
System.out.println("Received memberleft notification from Member: " + member.getId());
}
public void invocationCompleted()
{
System.out.println("Received invocationcompleted notification");
}
}
12. Create a class that can distribute load
package com.coher.loader;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.InvocationService;
import com.tangosol.net.Member;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
public class LoaderAgentTest
{
public LoaderAgentTest()
{
super();
}
public static void main(String[] args)
{
InvocationService service = (InvocationService) CacheFactory.getService("InvocationService");
Set setMembers = service.getInfo().getServiceMembers();
int counter = 0;
for (Iterator it = setMembers.iterator() ; it.hasNext(); )
{
Member m = (Member)it.next();
System.out.println(m.getRoleName());
if (m.getRoleName().equals("CacheServer"))
{
LoaderAgent task = new LoaderAgent(counter + 1+"", counter+500+"");
service.execute(task, Collections.singleton(m),
new LoaderObserver());
}
counter = counter+500;
}
}
}
Start two instances of coherence cache servers(by double clicking on cacheserver.cmd twice) and one instance of the proxy (cacheproxy1.cmd)
Below are the links to project Source and configuration files
Project Source zip
Run folder zip
Subscribe to:
Posts (Atom)