JBoss.org Community Documentation

7.1. POJO Cache API, POJO manipulation, and Replication

For this tutorial, start two instance of the demo GUI. In this tutorial, we will:

  • Attach POJOs to the cache and see them being replicated.
  • After attaching, manipulate the POJOs and see the individual changes replicated.
  • Retrieve POJOs from the cache, manipulate them and see the changes replicated.
  • Create POJOs that share a common POJO and the consequences of changes to this.
  • Detach POJOs from the cache.
  • After detaching, manipulates the POJOs and see how the values in the cache are unchanged.

  1. In the 1st GUI instance, create a POJO, i.e. a Person with an Address:
       joe = new Person();
       joe.setName("Joe Black");
       joe.setAge(31);
    
       addr = new Address();
       addr.setCity("Sunnyvale");
       addr.setStreet("123 Albert Ave");
       addr.setZip(94086);
    
       joe.setAddress(addr);
                     
  2. Attach the POJO to the cache:
       cache.attach("pojo/joe", joe);
                     
  3. Change attributes of the POJO and see the individual changes being propagated to the 2nd cache GUI:
       joe.setAge(41);
                     
  4. In the 2nd GUI instance, get a reference to the Person in the cache and create a second Person with the existing Person's Address:
       joe = cache.find("pojo/joe");
    
       mary = new Person();
       mary.setName("Mary White");
       mary.setAge(30);
    
       mary.setAddress(joe.getAddress());
                     
  5. Attach the new POJO to the cache:
       cache.attach("pojo/mary", mary);
                     
  6. Now, change either Person's Address and see how the change applies to both POJOs and has been propagated to the other cache, visible in the 1st GUI instance:
       mary.getAddress().setZip(95000);
                     
  7. Still in the 2nd GUI instance, detach the POJOs from the cache and see how the POJOs are no longer visible:
       cache.detach("pojo/joe");
       cache.detach("pojo/mary");
                     
  8. Finally, in any of GUI instances, change some attributes of the POJO and see these changes have no effect in the cache:
       joe.setName("Joe White");