View Javadoc
1   /*
2   Copyright (c) 2007 Health Market Science, Inc.
3   
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package examples.iterator;
18  
19  import java.rmi.registry.LocateRegistry;
20  import java.rmi.registry.Registry;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import com.healthmarketscience.rmiio.SerialRemoteIteratorClient;
25  import com.healthmarketscience.rmiio.SerialRemoteIteratorServer;
26  
27  /**
28   * Example client which sends a bunch of strings (given on the command line)
29   * to the example server using a RemoteIterator.
30   *
31   * @author James Ahlborn
32   */
33  public class TestClient {
34  
35    public static void main(String[] args) throws Exception
36    {
37      if(args.length < 1) {
38        System.err.println("Usage: <string1> [<string2> ...]");
39        System.exit(1);
40      }
41  
42      // grab the string arguments from the commandline
43      List<String> strings = Arrays.asList(args);
44  
45      // get a handle to the remote service to which we want to send the strings
46      Registry registry = LocateRegistry.getRegistry(TestServer.REGISTRY_PORT);
47      RemoteStringServer stub = (RemoteStringServer)
48        registry.lookup("RemoteStringServer");
49  
50      System.out.println("Sending " + strings.size()  + " strings");
51  
52      SerialRemoteIteratorServer<String> server = null;
53      try {
54        // setup the remote iterator.  note, the client here is actually acting
55        // as an RMI server (very confusing, i know).  this code sets up an RMI
56        // server in the client, which the RemoteStringServer will then interact
57        // with to get the String data.
58        server = new SerialRemoteIteratorServer<String>(strings.iterator());
59        SerialRemoteIteratorClient<String> client =
60          new SerialRemoteIteratorClient<String>(server); 
61  
62        // call the remote method on the server.  the server will actually
63        // interact with the RMI "server" we started above to retrieve the
64        // String data
65        stub.sendStrings(client);
66        
67      } finally {
68        // always make a best attempt to shutdown RemoteIterator
69        if(server != null) {
70          server.close();
71        }
72      }
73  
74      System.out.println("Finished sending " + strings.size()  + " strings");
75      
76    }
77  
78  }