]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/test/TestClient.java
added shutdown call to cleanly disconnect from the opensrf network
[OpenSRF.git] / src / java / org / opensrf / test / TestClient.java
1 package org.opensrf.test;
2 import org.opensrf.*;
3 import org.opensrf.util.*;
4 import java.util.Map;
5 import java.util.Date;
6 import java.util.List;
7 import java.util.ArrayList;
8 import java.io.PrintStream;
9
10 public class TestClient {
11
12     public static void main(String args[]) throws Exception {
13
14         /** which opensrf service are we sending our request to */
15         String service; 
16         /** which opensrf method we're calling */
17         String method;
18         /** method params, captures from command-line args */
19         List<Object> params;
20         /** knows how to read JSON */
21         JSONReader reader;
22         /** opensrf request */
23         Request request;
24         /** request result */
25         Result result;
26         /** start time for the request */
27         long start;
28         /** for brevity */
29         PrintStream out = System.out;
30
31         if(args.length < 3) {
32             out.println( "usage: org.opensrf.test.TestClient "+
33                 "<osrfConfigFile> <service> <method> [<JSONparam1>, <JSONparam2>]");
34             return;
35         }
36
37         /** connect to the opensrf network,  default config context 
38          * for opensrf_core.xml is /config/opensrf */
39         Sys.bootstrapClient(args[0], "/config/opensrf");
40
41         /* grab the server, method, and any params from the command line */
42         service = args[1];
43         method = args[2];
44         params = new ArrayList<Object>();
45         for(int i = 3; i < args.length; i++) 
46             params.add(new JSONReader(args[i]).read());
47
48
49         /** build the client session */
50         ClientSession session = new ClientSession(service);
51
52         /** kick off the timer */
53         start = new Date().getTime();
54
55         /** Create the request object from the session, method and params */
56         request = session.request(method, params);
57
58         while( (result = request.recv(60000)) != null ) { 
59             /** loop over the results and print the JSON version of the content */
60
61             if(result.getStatusCode() != 200) { 
62                 /** make sure the request succeeded */
63                 out.println("status = " + result.getStatus());
64                 out.println("status code = " + result.getStatusCode());
65                 continue;
66             }
67
68             /** JSON-ify the resulting object and print it */
69             out.println("\nresult JSON: " + new JSONWriter(result.getContent()).write());
70         }
71         
72         /** How long did the request take? */
73         out.println("Request round trip took: " + (new Date().getTime() - start) + " ms.");
74
75         Sys.shutdown();
76     }
77 }
78
79
80