欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的Coherence demo

程序员文章站 2022-07-03 08:37:45
...

   在一个Java Project中添加Coherence.jar,在jar文件中config目录中存在默认的coherence的配置信息。

package com.etrip.app;

import com.tangosol.net.CacheFactory; 
import com.tangosol.net.NamedCache;
/**
 * 一个简单的Coherence的使用采用默认的coherence cache config配置文件
 * 
 * @Title: TODO
 * @Description: 实现TODO
 * @Copyright:Copyright (c) 2011
 * @Company:
 * @Date:2013-1-4
 * @author 
 * @version 1.0
 */
public class CoherenceCache {
    NamedCache cache;
    public CoherenceCache() {
    }
   
   /**
    * 从 CacheFactory 类中创建了一个缓存实例。使用 CacheFactory 类的 getCache() 
    * 方法创建一个 NamedCache。使用一个映射到分布式缓存模式的缓存名称 VirtualCache。
	* Named cache = CacheFactory.getCache ( "VirtualCache");
	* NamedCache 是一个保存整个集群中的节点所共享的资源的 java.util.Map。
	* 使用 put() 方法添加缓存项。cache.put (key, "Hello Cache");
	* 可以使用 get() 方法检索缓存项。
	* System.out.println((String)cache.get("hello"));
    */
   public void putCache(){
        cache = CacheFactory.getCache("VirtualCache"); 
       String key = "hello"; 
       cache.put (key, "Hello Cache"); 
   
   }
   
   
   public void retrieveCache(){
       System.out.println((String)cache.get("hello"));
   }
   
    public static void main (String [] args) {
	    CoherenceCache cache = new CoherenceCache(); 
	    cache.putCache();
	    cache.retrieveCache();
    }
    
}


 

执行结果如下:

 

2013-01-05 17:15:54.099/0.830 Oracle Coherence 3.7.1.1 <Info> (thread=main, member=n/a): Loaded operational configuration from "jar:file:/D:/app/Oracle/Middleware/coherence_3.7/lib/coherence.jar!/tangosol-coherence.xml"
2013-01-05 17:15:54.151/0.882 Oracle Coherence 3.7.1.1 <Info> (thread=main, member=n/a): Loaded operational overrides from "jar:file:/D:/app/Oracle/Middleware/coherence_3.7/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
2013-01-05 17:15:54.152/0.883 Oracle Coherence 3.7.1.1 <D5> (thread=main, member=n/a): Optional configuration override "/tangosol-coherence-override.xml" is not specified
2013-01-05 17:15:54.175/0.906 Oracle Coherence 3.7.1.1 <D5> (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml" is not specified

Oracle Coherence Version 3.7.1.1 Build 28901
 Grid Edition: Development mode
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

2013-01-05 17:15:54.518/1.249 Oracle Coherence GE 3.7.1.1 <Info> (thread=main, member=n/a): Loaded cache configuration from "jar:file:/D:/app/Oracle/Middleware/coherence_3.7/lib/coherence.jar!/coherence-cache-config.xml"
2013-01-05 17:15:55.550/2.281 Oracle Coherence GE 3.7.1.1 <D4> (thread=main, member=n/a): TCMP bound to /172.30.101.179:8092 using SystemSocketProvider
2013-01-05 17:15:55.987/2.718 Oracle Coherence GE 3.7.1.1 <Info> (thread=Cluster, member=n/a): This Member(Id=4, Timestamp=2013-01-05 17:15:55.923, Address=172.30.101.179:8092, MachineId=6892, Location=site:,machine:longgangbai-PC,process:3772, Role=EtripAppCoherenceCache, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4) joined cluster "cluster:0xFCDB" with senior Member(Id=1, Timestamp=2013-01-05 16:44:45.949, Address=172.30.101.179:8088, MachineId=6892, Location=site:,machine:longgangbai-PC,process:7048, Role=CoherenceServer, Edition=Grid Edition, Mode=Development, CpuCount=4, SocketCount=4)
2013-01-05 17:15:56.184/2.915 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member(Id=3, Timestamp=2013-01-05 17:11:40.531, Address=172.30.101.179:8090, MachineId=6892, Location=site:,machine:longgangbai-PC,process:3900, Role=CoherenceConsole) joined Cluster with senior member 1
2013-01-05 17:15:56.205/2.936 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member 3 joined Service Management with senior member 3
2013-01-05 17:15:56.206/2.937 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member 1 joined Service Management with senior member 1
2013-01-05 17:15:56.206/2.937 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with senior member 1
2013-01-05 17:15:56.206/2.937 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with senior member 1
2013-01-05 17:15:56.206/2.937 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with senior member 1
2013-01-05 17:15:56.206/2.938 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with senior member 1
2013-01-05 17:15:56.209/2.940 Oracle Coherence GE 3.7.1.1 <Info> (thread=main, member=n/a): Started cluster Name=cluster:0xFCDB

Group{Address=224.3.7.0, Port=37000, TTL=4}

MasterMemberSet(
  ThisMember=Member(Id=4, Timestamp=2013-01-05 17:15:55.923, Address=172.30.101.179:8092, MachineId=6892, Location=site:,machine:longgangbai-PC,process:3772, Role=EtripAppCoherenceCache)
  OldestMember=Member(Id=1, Timestamp=2013-01-05 16:44:45.949, Address=172.30.101.179:8088, MachineId=6892, Location=site:,machine:longgangbai-PC,process:7048, Role=CoherenceServer)
  ActualMemberSet=MemberSet(Size=3
    Member(Id=1, Timestamp=2013-01-05 16:44:45.949, Address=172.30.101.179:8088, MachineId=6892, Location=site:,machine:longgangbai-PC,process:7048, Role=CoherenceServer)
    Member(Id=3, Timestamp=2013-01-05 17:11:40.531, Address=172.30.101.179:8090, MachineId=6892, Location=site:,machine:longgangbai-PC,process:3900, Role=CoherenceConsole)
    Member(Id=4, Timestamp=2013-01-05 17:15:55.923, Address=172.30.101.179:8092, MachineId=6892, Location=site:,machine:longgangbai-PC,process:3772, Role=EtripAppCoherenceCache)
    )
  MemberId|ServiceVersion|ServiceJoined|MemberState
    1|3.7.1|2013-01-05 16:44:45.949|JOINED,
    3|3.7.1|2013-01-05 17:11:40.531|JOINED,
    4|3.7.1|2013-01-05 17:15:56.197|JOINED
  RecycleMillis=1200000
  RecycleSet=MemberSet(Size=0
    )
  )

TcpRing{Connections=[3]}
IpMonitor{AddressListSize=0}

2013-01-05 17:15:56.253/2.984 Oracle Coherence GE 3.7.1.1 <D5> (thread=Invocation:Management, member=4): Service Management joined the cluster with senior service member 1
2013-01-05 17:15:56.699/3.430 Oracle Coherence GE 3.7.1.1 <D5> (thread=DistributedCache, member=4): Service DistributedCache joined the cluster with senior service member 1
2013-01-05 17:15:56.780/3.511 Oracle Coherence GE 3.7.1.1 <D4> (thread=DistributedCache, member=4): Asking member 1 for 128 primary partitions
Hello Cache
2013-01-05 17:15:56.929/3.660 Oracle Coherence GE 3.7.1.1 <D4> (thread=ShutdownHook, member=4): ShutdownHook: stopping cluster node
2013-01-05 17:15:56.931/3.662 Oracle Coherence GE 3.7.1.1 <D5> (thread=DistributedCache, member=n/a): Service DistributedCache left the cluster
2013-01-05 17:15:56.933/3.664 Oracle Coherence GE 3.7.1.1 <D5> (thread=Invocation:Management, member=n/a): Service Management left the cluster
2013-01-05 17:15:56.939/3.670 Oracle Coherence GE 3.7.1.1 <D5> (thread=Cluster, member=n/a): Service Cluster left the cluster