1 7 8 package org.jboss.tutorial.clusteredentity.client; 9 10 import java.util.Properties ; 11 import java.util.Set ; 12 13 import javax.naming.InitialContext ; 14 import javax.naming.NamingException ; 15 16 import org.jboss.tutorial.clusteredentity.bean.Contact; 17 import org.jboss.tutorial.clusteredentity.bean.Customer; 18 import org.jboss.tutorial.clusteredentity.bean.EntityTest; 19 20 25 public class CachedEntityRun 26 { 27 public static void main(String [] args)throws NamingException 28 { 29 if (args.length != 2) 30 { 31 throw new RuntimeException ("You need to pass in two parameters of the type ipaddress:port"); 32 } 33 34 Properties prop1 = new Properties (); 35 prop1.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 36 prop1.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); 37 prop1.put("java.naming.provider.url", "jnp://" + args[0]); 38 39 Properties prop2 = new Properties (); 40 prop2.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); 41 prop2.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); 42 prop2.put("java.naming.provider.url", "jnp://" + args[1]); 43 44 System.out.println("Saving customer to node 1"); 45 InitialContext ctx1 = new InitialContext (prop1); 46 47 EntityTest tester1 = (EntityTest)ctx1.lookup(EntityTest.class.getName()); 48 Customer customer = tester1.createCustomer(); 49 customer = tester1.findByCustomerId(customer.getId()); 50 51 System.out.println("Looking for customer in node 2's cache"); 52 InitialContext ctx2 = new InitialContext (prop2); 53 54 EntityTest tester2 = (EntityTest)ctx1.lookup(EntityTest.class.getName()); 55 56 if(!tester2.isCustomerInCache(customer.getId())) 57 { 58 throw new RuntimeException ("Could not find Customer in node2's cache"); 59 } 60 System.out.println("Found customer " + customer.getId() + " in node2 cache"); 61 62 if (!tester2.isCustomerContactsInCache(customer.getId())) 63 { 64 throw new RuntimeException ("Could not find Customer contacts in node2's cache"); 65 } 66 System.out.println("Found contacts collection for " + customer.getId() + " in node2 cache"); 67 68 Set <Contact> contacts = customer.getContacts(); 69 70 for (Contact contact : contacts) 71 { 72 if (!tester2.isContactInCache(contact.getId())) 73 { 74 throw new RuntimeException ("Could not find Contact in node2's cache"); 75 } 76 System.out.println("Found contact " + contact.getId() + " collection in node2 cache"); 77 } 78 79 customer = tester2.findByCustomerId(customer.getId()); 80 if (customer == null) 81 { 82 throw new RuntimeException ("Customer was not found in node 2"); 83 } 84 System.out.println("Lookup of customer from node2 worked (via cache)"); 85 System.out.println("Customer: id=" + customer.getId() + "; name=" + customer.getName()); 86 87 for (Contact contact : contacts) 88 { 89 System.out.println("\tContact: id=" + contact.getId() + "; name=" + contact.getName()); 90 } 91 92 } 93 } 94 | Popular Tags |