1/* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.util.ArrayList; 25 26import javax.naming.InitialContext; 27import javax.naming.Context; 28import javax.naming.NameNotFoundException; 29 30import javax.rmi.PortableRemoteObject; 31 32 33 34public class HelloClient implements Runnable { 35 static final int MAX_RETRY = 10; 36 static final int ONE_SECOND = 1000; 37 private static boolean responseReceived; 38 39 public static void main(String args[]) throws Exception { 40 executeRmiClientCall(); 41 } 42 43 @Override 44 public void run() { 45 try { 46 executeRmiClientCall(); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 throw new RuntimeException(e); 50 } 51 } 52 53 54 public static boolean isResponseReceived () { 55 return responseReceived; 56 } 57 58 public static void executeRmiClientCall() throws Exception { 59 Context ic; 60 Object objref; 61 HelloInterface helloSvc; 62 String response; 63 Object testResponse; 64 int retryCount = 0; 65 66 ArrayList<Test> listParam = new ArrayList<Test>(); 67 listParam.add(null); 68 System.out.println("HelloClient.main: enter ..."); 69 while (retryCount < MAX_RETRY) { 70 try { 71 ic = new InitialContext(); 72 System.out.println("HelloClient.main: HelloService lookup ..."); 73 // STEP 1: Get the Object reference from the Name Service 74 // using JNDI call. 75 objref = ic.lookup("HelloService"); 76 System.out.println("HelloClient: Obtained a ref. to Hello server."); 77 78 // STEP 2: Narrow the object reference to the concrete type and 79 // invoke the method. 80 helloSvc = (HelloInterface) PortableRemoteObject.narrow(objref, 81 HelloInterface.class); 82 83 Test3 test3 = new Test3(listParam); 84 Test3 test3Response = helloSvc.sayHelloWithTest3(test3); 85 System.out.println("Server says: Test3 response == " + test3Response); 86 87 Test3 test3WithNullList = new Test3(null); 88 test3Response = helloSvc.sayHelloWithTest3(test3WithNullList); 89 System.out.println("Server says: Test3 response == " 90 + test3Response); 91 92 Test4 test4 = new Test4(listParam); 93 Test3 test4Response = helloSvc.sayHelloWithTest3(test4); 94 System.out.println("Server says: Test4 response == " + test4Response); 95 96 responseReceived = true; 97 break; 98 } catch (NameNotFoundException nnfEx) { 99 System.err.println("NameNotFoundException Caught .... try again"); 100 retryCount++; 101 try { 102 Thread.sleep(ONE_SECOND); 103 } catch (InterruptedException e) { 104 e.printStackTrace(); 105 } 106 continue; 107 } catch (Throwable t) { 108 System.err.println("Exception " + t + "Caught"); 109 t.printStackTrace(); 110 throw new RuntimeException(t); 111 } 112 } 113 System.err.println("HelloClient terminating "); 114 try { 115 Thread.sleep(ONE_SECOND); 116 } catch (InterruptedException e) { 117 e.printStackTrace(); 118 } 119 } 120} 121