1import java.rmi.RemoteException;
2import java.net.InetAddress;
3import java.net.MalformedURLException;
4import java.rmi.NotBoundException;
5import java.util.HashMap;
6import java.util.Vector;
7import java.util.concurrent.ConcurrentHashMap;
8import java.util.concurrent.locks.ReentrantLock;
9
10import javax.naming.NamingException;
11import javax.naming.InitialContext;
12import javax.naming.Context;
13import javax.naming.NameNotFoundException;
14import javax.naming.NamingException;
15import javax.rmi.PortableRemoteObject;
16
17import org.omg.CORBA.Any;
18import org.omg.CORBA.ORB;
19
20public class HelloClient implements Runnable {
21    static final int MAX_RETRY = 10;
22    static final int ONE_SECOND = 1000;
23    private static boolean responseReceived;
24
25    public static void main(String args[]) throws Exception {
26        executeRmiClientCall();
27    }
28
29    @Override
30    public void run() {
31        try {
32            executeRmiClientCall();
33        } catch (Exception e) {
34            e.printStackTrace();
35            throw new RuntimeException(e);
36        }
37    }
38
39
40    public static boolean isResponseReceived () {
41        return responseReceived;
42    }
43
44    public static void executeRmiClientCall() throws Exception {
45        Context ic;
46        Object objref;
47        HelloInterface helloSvc;
48        String response;
49        int retryCount = 0;
50
51        Test test = new Test();
52        System.out.println("HelloClient.main: enter ...");
53        while (retryCount < MAX_RETRY) {
54            try {
55                ic = new InitialContext();
56                System.out.println("HelloClient.main: HelloService lookup ...");
57                // STEP 1: Get the Object reference from the Name Service
58                // using JNDI call.
59                objref = ic.lookup("HelloService");
60                System.out.println("HelloClient: Obtained a ref. to Hello server.");
61
62                // STEP 2: Narrow the object reference to the concrete type and
63                // invoke the method.
64                helloSvc = (HelloInterface) PortableRemoteObject.narrow(objref,
65                    HelloInterface.class);
66                System.out.println("HelloClient: Invoking on remote server with ConcurrentHashMap parameter");
67                ConcurrentHashMap <String, String> testConcurrentHashMap = new ConcurrentHashMap<String, String>();
68                response = helloSvc.sayHelloWithHashMap(testConcurrentHashMap);
69                System.out.println("HelloClient: Server says:  " + response);
70                if (!response.contains("Hello with hashMapSize ==")) {
71                    System.out.println("HelloClient: expected response not received");
72                    throw new RuntimeException("Expected Response Hello with hashMapSize == 0 not received");
73                }
74                responseReceived = true;
75                break;
76            } catch (NameNotFoundException nnfEx) {
77                System.err.println("NameNotFoundException Caught  .... try again");
78                retryCount++;
79                try {
80                    Thread.sleep(ONE_SECOND);
81                } catch (InterruptedException e) {
82                    e.printStackTrace();
83                }
84                continue;
85            } catch (Exception e) {
86                System.err.println("Exception " + e + "Caught");
87                e.printStackTrace();
88                throw new RuntimeException(e);
89            }
90        }
91        System.err.println("HelloClient terminating ");
92        try {
93            Thread.sleep(5000);
94        } catch (InterruptedException e) {
95            e.printStackTrace();
96        }
97    }
98}
99