Search This Blog

Monday 27 September 2010

Coherence - Continuous Query Caching

Coherence provides the ability to have a cache provide a feature that combines a query result with a continuous stream of related events to maintain an up-to-date query result in a real-time fashio. To demonstrate this here is a simple demo.

1. Create a very simple object which contains a single attribute "gender" for either male's or females.

package pas.au.coherence.querycaching;

public class DemoObject implements java.io.Serializable
{
  private String gender;

  public DemoObject ()
  {  
  }

  public DemoObject (String _gender)
  {  
   gender = _gender;
  }
  
  public String getGender() 
  {
    return gender;
  }

  public void setGender(String gender) 
  {
    this.gender = gender;
  }
  
}
2. Create a simple test class as follows.
package pas.au.coherence.querycaching;

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
import com.tangosol.net.cache.ContinuousQueryCache;
import com.tangosol.util.Filter;
import com.tangosol.util.filter.EqualsFilter;

import java.util.logging.Level;
import java.util.logging.Logger;

public class ContinuousQueryCacheDemo
{
  private Logger logger = Logger.getLogger(this.getClass().getSimpleName());
  
  public ContinuousQueryCacheDemo()
  {
  }

  public void doLogMessage (String message)
  {
    logger.log (Level.INFO, message);  
  }
  
  public void run()
  {
    NamedCache test = CacheFactory.getCache("mycache");
    
    // put some data in
    test.put("1", new DemoObject("male"));
    test.put("2", new DemoObject("female"));
    test.put("3", new DemoObject("female"));
    test.put("4", new DemoObject("female"));
    test.put("5", new DemoObject("female"));
    test.put("6", new DemoObject("male"));
    
    doLogMessage("Size of mycache [mycache] = " + test.size());
    
    Filter filter = new EqualsFilter("getGender", "male");
    // Create Continuous Query Cache
    ContinuousQueryCache allMales =
      new ContinuousQueryCache(test, filter);
    
    doLogMessage("Created Query Cache with just MALES [allmales]");
    doLogMessage("Size of Continuous Query Caching [allMales] = " + allMales.size());
    
    // add another male entry to test cache
    test.put("7", new DemoObject("male"));
    
    doLogMessage("New MALE object added");
    
    // check if Continuous Query Caching has that entry added
    doLogMessage("Size of Continuous Query Cache [allMales] = " + allMales.size());
    
    doLogMessage("all done.."); 
  }
  
  public static void main(String[] args) 
  {
    ContinuousQueryCacheDemo test = new ContinuousQueryCacheDemo();
    test.run();
  }
}  

3.When you run this example you can see how adding objects to the named cache "mycache" it automatically maintains the ContinuousQueryCache locally on the client.

OUTPUT from an ANT client

....
     [java]   ThisMember=Member(Id=1, Timestamp=2010-09-27 09:36:06.633, Address=10.187.114.243:8088, MachineId=50163, L
ocation=machine:paslap-au,process:4176, Role=PasAuContinuousQueryCacheDemo)
     [java]   OldestMember=Member(Id=1, Timestamp=2010-09-27 09:36:06.633, Address=10.187.114.243:8088, MachineId=50163,
 Location=machine:paslap-au,process:4176, Role=PasAuContinuousQueryCacheDemo)
     [java]   ActualMemberSet=MemberSet(Size=1, BitSetCount=2
     [java]     Member(Id=1, Timestamp=2010-09-27 09:36:06.633, Address=10.187.114.243:8088, MachineId=50163, Location=m
achine:paslap-au,process:4176, Role=PasAuContinuousQueryCacheDemo)
     [java]     )
     [java]   RecycleMillis=1200000
     [java]   RecycleSet=MemberSet(Size=0, BitSetCount=0
     [java]     )
     [java]   )
     [java]
     [java] TcpRing{Connections=[]}
     [java] IpMonitor{AddressListSize=0}
     [java]
     [java] 2010-09-27 09:36:10.003/4.197 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=1): Ser
vice Management joined the cluster with senior service member 1
     [java] 2010-09-27 09:36:10.174/4.368 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=1): Service
DistributedCache joined the cluster with senior service member 1
     [java] 27/09/2010 9:36:10 AM pas.au.coherence.querycaching.ContinuousQueryCacheDemo doLogMessage
     [java] INFO: Size of mycache [mycache] = 6
     [java] 27/09/2010 9:36:10 AM pas.au.coherence.querycaching.ContinuousQueryCacheDemo doLogMessage
     [java] INFO: Created Query Cache with just MALES [allmales]
     [java] 27/09/2010 9:36:10 AM pas.au.coherence.querycaching.ContinuousQueryCacheDemo doLogMessage
     [java] INFO: Size of Continuous Query Caching [allMales] = 2
     [java] 27/09/2010 9:36:10 AM pas.au.coherence.querycaching.ContinuousQueryCacheDemo doLogMessage
     [java] INFO: New MALE object added
     [java] 27/09/2010 9:36:10 AM pas.au.coherence.querycaching.ContinuousQueryCacheDemo doLogMessage
     [java] INFO: Size of Continuous Query Cache [allMales] = 3
     [java] 27/09/2010 9:36:10 AM pas.au.coherence.querycaching.ContinuousQueryCacheDemo doLogMessage
     [java] INFO: all done..
     [java]


More info on this as follows

Oracle® Coherence Developer's Guide
Release 3.6

Part Number E15723-01
http://download.oracle.com/docs/cd/E15357_01/coh.360/e15723/api_continuousquery.htm

No comments: