1 22 package org.jboss.ejb3.test.clusteredentity; 23 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 import javax.ejb.Remote ; 28 import javax.ejb.Stateless ; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import javax.persistence.EntityManager; 32 import javax.persistence.PersistenceContext; 33 34 import org.jboss.cache.AbstractCacheListener; 35 import org.jboss.cache.Fqn; 36 import org.jboss.cache.Cache; 37 import org.jboss.cache.CacheListener; 38 import org.jboss.cache.jmx.CacheJmxWrapperMBean; 39 import org.jboss.mx.util.MBeanProxyExt; 40 import org.jboss.mx.util.MBeanServerLocator; 41 import org.jgroups.View; 42 43 49 @Stateless 50 @Remote (EntityTest.class) 51 public class EntityTestBean implements EntityTest 52 { 53 @PersistenceContext 54 private EntityManager manager; 55 56 static MyListener listener; 57 58 public EntityTestBean() 59 { 60 try 61 { 62 Cache cache = getCache(); 64 if (listener == null) 65 { 66 listener = new MyListener(); 67 cache.addCacheListener(listener); 68 } 69 } 70 catch (Exception e) 71 { 72 throw new RuntimeException (e); 73 } 74 } 75 76 public Customer createCustomer() 77 { 78 System.out.println("CREATE CUSTOMER"); 79 try 80 { 81 listener.clear(); 82 83 Customer customer = new Customer(); 84 customer.setId(1); 85 customer.setName("JBoss"); 86 Set <Contact> contacts = new HashSet <Contact>(); 87 88 Contact kabir = new Contact(); 89 kabir.setId(1); 90 kabir.setCustomer(customer); 91 kabir.setName("Kabir"); 92 kabir.setTlf("1111"); 93 contacts.add(kabir); 94 95 Contact bill = new Contact(); 96 bill.setId(2); 97 bill.setCustomer(customer); 98 bill.setName("Bill"); 99 bill.setTlf("2222"); 100 contacts.add(bill); 101 102 customer.setContacts(contacts); 103 104 manager.persist(customer); 105 return customer; 106 } 107 catch (Exception e) 108 { 109 throw new RuntimeException (e); } 111 finally 112 { 113 System.out.println("CREATE CUSTOMER - END"); 114 } 115 } 116 117 public Customer findByCustomerId(Integer id) 118 { 119 System.out.println("FIND CUSTOMER"); 120 listener.clear(); 121 try 122 { 123 Customer customer = manager.find(Customer.class, id); 124 125 return customer; 126 } 127 catch (Exception e) 128 { 129 throw new RuntimeException (e); } 131 finally 132 { 133 System.out.println("FIND CUSTOMER - END"); 134 } 135 } 136 137 public void loadedFromCache() 138 { 139 System.out.println("CHECK CACHE"); 140 try 141 { 142 System.out.println("Visited: " + listener.visited); 143 if (!listener.visited.contains("Customer#1")) 144 throw new RuntimeException ("Customer#1 was not in cache"); 145 if (!listener.visited.contains("Contact#1")) 146 throw new RuntimeException ("Contact#1 was not in cache"); 147 if (!listener.visited.contains("Contact#2")) 148 throw new RuntimeException ("Contact2#1 was not in cache"); 149 if (!listener.visited.contains("Customer.contacts#1")) 150 throw new RuntimeException ("Customer.contacts#1 was not in cache"); 151 } 152 finally 153 { 154 System.out.println("CHECK CACHE - END"); 155 } 156 157 } 158 159 private Cache getCache() throws Exception 160 { 161 MBeanServer server = MBeanServerLocator.locateJBoss(); 162 CacheJmxWrapperMBean proxy = (CacheJmxWrapperMBean)MBeanProxyExt.create(CacheJmxWrapperMBean.class, new ObjectName ("jboss.cache:service=EJB3EntityTreeCache"), server); 163 Cache cache = proxy.getCache(); 164 165 return cache; 166 } 167 168 class MyListener extends AbstractCacheListener 169 { 170 HashSet visited = new HashSet (); 171 172 public void clear() 173 { 174 visited.clear(); 175 } 176 177 @Override 178 public void nodeVisited(Fqn fqn, boolean pre) 179 { 180 if (!pre) 181 { 182 System.out.println("MyListener - Visiting node " + fqn.toString()); 183 String name = fqn.toString(); 184 String token = ".clusteredentity."; 185 int index = name.indexOf(token); 186 index += token.length(); 187 name = name.substring(index); 188 System.out.println(name); 189 visited.add(name); 190 } 191 } 192 } 193 } 194 | Popular Tags |