KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > clusteredentity > client > CachedEntityRun


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.tutorial.clusteredentity.client;
9
10 import java.util.Properties JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import javax.naming.InitialContext JavaDoc;
14 import javax.naming.NamingException JavaDoc;
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 /**
21  *
22  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
23  * @version $Revision$
24  */

25 public class CachedEntityRun
26 {
27    public static void main(String JavaDoc[] args)throws NamingException JavaDoc
28    {
29       if (args.length != 2)
30       {
31          throw new RuntimeException JavaDoc("You need to pass in two parameters of the type ipaddress:port");
32       }
33
34       Properties JavaDoc prop1 = new Properties JavaDoc();
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 JavaDoc prop2 = new Properties JavaDoc();
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 JavaDoc ctx1 = new InitialContext JavaDoc(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 JavaDoc ctx2 = new InitialContext JavaDoc(prop2);
53
54       EntityTest tester2 = (EntityTest)ctx1.lookup(EntityTest.class.getName());
55
56       if(!tester2.isCustomerInCache(customer.getId()))
57       {
58          throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc<Contact> contacts = customer.getContacts();
69
70       for (Contact contact : contacts)
71       {
72          if (!tester2.isContactInCache(contact.getId()))
73          {
74             throw new RuntimeException JavaDoc("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 JavaDoc("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