轉載自:
http://www.onlinetechvision.com/?p=425
This article shows how to distribute Spring beans by using Coherence. In below sample application, a new cluster named OTV has been created and a spring bean has been distributed by using a cache object named user-map. It has been distributed between two members of the cluster.
Used Technologies :
JDK 1.6.0_21
Spring 3.0.5
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 via http://www.oracle.com/technetwork/middleware/coherence/downloads/index.html
STEP 3 : LIBRARIES
Firstly, Spring dependencies are added to Maven’ s pom.xml.
?
1 2 3 4 5 6 7 8 9 10 11 | |
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 | |
For creating runnable-jar, below plugin can be used.
?
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 | |
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 cache configuration 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 | |
STEP 5 : CREATE tangosol-coherence-override.xml
tangosol-coherence-override.xml contains cluster, member-identity and configurable-cache-factory configuration. Also below configuration xml file show first member of the cluster.
tangosol-coherence-override.xml for first member of the cluster :
<iframe id="aswift_1" style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; left: 0px; position: absolute; top: 0px;" name="aswift_1" frame marginwidth="0" marginheight="0" scrolling="no" width="728" height="90"></iframe> ?
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 | |
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 | |
STEP 6 : CREATE applicationContext.xml
applicationContext.xml is created.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
STEP 7 : CREATE SPRING BEAN
A new User Spring 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 | |
STEP 8 : CREATE ICacheService INTERFACE
A new ICacheService Interface is created for service layer.
?
1 2 3 4 5 6 7 8 9 10 11 | |
STEP 9 : CREATE CacheService
CacheService is created for service layer by implementing ICacheService.
?
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 | |
STEP 10 : 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 | |
STEP 11 : CREATE CacheUpdater
CacheUpdater Class is created to add new entry to cache and monitor cache content.
?
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 71 72 73 74 75 76 | |
STEP 12 : CREATE Application
Application Class is created to run the application.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
STEP 13 : BUILD PROJECT
After OTV_Spring_Coherence Project is build, OTV_Spring_Coherence-0.0.1-SNAPSHOT.jar will be created.
Important Note : Members of the cluster have got different configurations for Coherence so the project should be built separately for each member.
STEP 14 : RUN PROJECT ON FIRST MEMBER OF THE CLUSTER
After created OTV_Spring_Coherence-0.0.1-SNAPSHOT.jar file is run at the members of the cluster, below output logs will be shown on first member’ s console:
?
1 2 3 4 5 6 7 8 9 10 | |
Second member’ s console :
?
1 2 3 4 | |
STEP 15 : DOWNLOAD
OTV_Spring_Coherence