This article shows how to provide distributed(partitioned) data management by using Oracle Coherence. In below sample application, a new cluster named OTV has been created and a cache object nameduser-map has been distributed between two members of the cluster.
Used Technologies :
JDK 1.6.0_21
Maven 3.0.2
Coherence 3.7.0
SolarisOS 5.10
STEP 1 : CREATE MAVEN PROJECT
A maven project is created as below. (It can be created by using Maven or IDE Plug-in).
STEP 2 : DOWNLOAD COHERENCE PACKAGE
Coherence package is downloaded viahttp://www.oracle.com/technetwork/middleware/coherence/downloads/index.html
STEP 3 : LIBRARIES
Firstly, Coherence library is installed to Local Maven Repository and its description is added to pom.xml as below. Also, if maven is not used, coherence.jar file can be added to classpath.
1
2
3
4
5
6
|
<
dependency
>
<
groupId
>com.tangosol
groupId
>
<
artifactId
>coherence
artifactId
>
<
version
>3.7.0
version
>
dependency
>
|
Below plugin can be used to create runnable-jar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<
plugin
>
<
groupId
>org.apache.maven.plugins
groupId
>
<
artifactId
>maven-assembly-plugin
artifactId
>
<
configuration
>
<
descriptorRefs
>
<
descriptorRef
>jar-with-dependencies
descriptorRef
>
descriptorRefs
>
<
archive
>
<
manifest
>
<
mainClass
>com.otv.exe.TestCacheExecutor
mainClass
>
manifest
>
archive
>
configuration
>
<
executions
>
<
execution
>
<
phase
>package
phase
>
<
goals
>
<
goal
>single
goal
>
goals
>
execution
>
executions
>
plugin
>
|
STEP 4 : CREATE otv-coherence-cache-config.xml
otv-coherence-cache-config.xml contains caching-schemes(distributed or replicated) and caching-scheme-mapping configuration. Created all cache mappings should be added to coherence-cache-config.xml.
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
|
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">
<
caching-scheme-mapping
>
<
cache-mapping
>
<
cache-name
>user-map
cache-name
>
<
scheme-name
>MapDistCache
scheme-name
>
cache-mapping
>
caching-scheme-mapping
>
<
caching-schemes
>
<
distributed-scheme
>
<
scheme-name
>MapDistCache
scheme-name
>
<
service-name
>MapDistCache
service-name
>
<
backing-map-scheme
>
<
local-scheme
>
<
unit-calculator
>BINARY
unit-calculator
>
local-scheme
>
backing-map-scheme
>
<
autostart
>true
autostart
>
distributed-scheme
>
caching-schemes
>
cache-config
>
|
STEP 5 : CREATE tangosol-coherence-override.xml
tangosol-coherence-override.xml contains cluster, member-identity and configurable-cache-factoryconfiguration. Also below configuration xml file show first member of the cluster.
tangosol-coherence-override.xml for first member of the cluster :
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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
>OTV
cluster-name
>
<
role-name
>OTV1
role-name
>
member-identity
>
<
unicast-listener
>
<
well-known-addresses
>
<
socket-address
id
=
"1"
>
<
address
>x.x.x.x
address
>
<
port
>8089
port
>
socket-address
>
<
socket-address
id
=
"2"
>
<
address
>y.y.y.y
address
>
<
port
>8089
port
>
socket-address
>
well-known-addresses
>
<
machine-id
>OTV1
machine-id
>
<
address
>x.x.x.x
address
>
<
port
>8089
port
>
<
port-auto-adjust
>true
port-auto-adjust
>
unicast-listener
>
cluster-config
>
<
configurable-cache-factory-config
>
<
init-params
>
<
init-param
>
<
param-type
>java.lang.String
param-type
>
<
param-value
system-property
=
"tangosol.coherence.cacheconfig"
>
otv-coherence-cache-config.xml
param-value
>
init-param
>
init-params
>
configurable-cache-factory-config
>
coherence
>
|
tangosol-coherence-override.xml for second member of the cluster :
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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
>OTV
cluster-name
>
<
role-name
>OTV2
role-name
>
member-identity
>
<
unicast-listener
>
<
well-known-addresses
>
<
socket-address
id
=
"1"
>
<
address
>x.x.x.x
address
>
<
port
>8089
port
>
socket-address
>
<
socket-address
id
=
"2"
>
<
address
>y.y.y.y
address
>
<
port
>8089
port
>
socket-address
>
well-known-addresses
>
<
machine-id
>OTV2
machine-id
>
<
address
>y.y.y.y
address
>
<
port
>8089
port
>
<
port-auto-adjust
>true
port-auto-adjust
>
unicast-listener
>
cluster-config
>
<
configurable-cache-factory-config
>
<
init-params
>
<
init-param
>
<
param-type
>java.lang.String
param-type
>
<
param-value
system-property
=
"tangosol.coherence.cacheconfig"
>
otv-coherence-cache-config.xml
param-value
>
init-param
>
init-params
>
configurable-cache-factory-config
>
coherence
>
|
STEP 6 : CREATE USER BEAN
A new User bean is created. This bean will be distributed between two node in OTV cluster. For serializing,java.io.Serializable interface has been implemented but PortableObject can be implemented for better performance.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package
com.otv.user;
import
java.io.Serializable;
/**
* @author onlinetechvision.com
* @since 9 Oct 2011
* @version 1.0.0
*
*/
public
class
User
implements
Serializable {
private
static
final
long
serialVersionUID = 1L;
private
String name;
private
String surname;
public
User(String name, String surname) {
this
.name = name;
this
.surname = surname;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getSurname() {
return
surname;
}
public
void
setSurname(String surname) {
this
.surname = surname;
}
@Override
public
String toString() {
StringBuffer strBuff =
new
StringBuffer();
strBuff.append(
"name : "
).append(name);
strBuff.append(
", surname : "
).append(surname);
return
strBuff.toString();
}
}
|
STEP 7 : CREATE CACHE CLASS
A new TestCache Class is created. This class initializes distributed(patitioned) data management and creates a cache object named user-map.
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package
com.otv;
import
org.apache.log4j.Logger;
import
com.otv.listener.UserMapListener;
import
com.tangosol.net.CacheFactory;
import
com.tangosol.net.NamedCache;
/**
* @author onlinetechvision.com
* @since 9 Oct 2011
* @version 1.0.0
*
*/
public
class
TestCache {
private
static
Logger log = Logger.getLogger(TestCache.
class
);
private
static
TestCache instance =
null
;
private
NamedCache cache =
null
;
private
static
final
String USER_MAP =
"user-map"
;
private
static
final
long
LOCK_TIMEOUT = -
1
;
public
TestCache() {
setCache(CacheFactory.getCache(USER_MAP));
getCache().addMapListener(
new
UserMapListener());
}
public
static
TestCache getInstance() {
if
(instance ==
null
) {
instance =
new
TestCache();
}
return
instance;
}
public
static
void
setInstance(TestCache instance) {
TestCache.instance = instance;
}
public
NamedCache getCache() {
return
cache;
}
public
void
setCache(NamedCache cache) {
this
.cache = cache;
}
public
void
addToCache(Object key, Object value) {
// key is locked
getCache().lock(key, LOCK_TIMEOUT);
try
{
// application logic
getCache().put(key, value);
}
finally
{
// key is unlocked
getCache().unlock(key);
}
}
public
void
deleteFromCache(Object key) {
// key is locked
getCache().lock(key, LOCK_TIMEOUT);
try
{
// application logic
getCache().remove(key);
}
finally
{
// key is unlocked
getCache().unlock(key);
}
}
}
|
STEP 8 : CREATE UserMapListener IMPL CLASS
A new UserMapListener class is created. This listener receives distributed user-map events.
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
|
package
com.otv.listener;
import
org.apache.log4j.Logger;
import
com.tangosol.util.MapEvent;
import
com.tangosol.util.MapListener;
/**
|