Difference between revisions of "Java Batch Client"
From Textserver wiki
Line 10: | Line 10: | ||
// | // | ||
//////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////// | ||
− | |||
import org.apache.http.client.HttpClient; | import org.apache.http.client.HttpClient; |
Revision as of 13:12, 20 January 2016
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Example client to submit a batch request to TextServer
4 // SERVICENAME service, and wait for the job to be finished.
5 //
6 // Input must be a ZIP file containing one or more text files to analyze
7 // Output will be a ZIP file containing results for each input file in
8 // the requested format (XML, json, conll)
9 //
10 ////////////////////////////////////////////////////////////////////////////
11
12 import org.apache.http.client.HttpClient;
13 import org.apache.http.HttpResponse;
14 import org.apache.http.HttpStatus;
15 import org.apache.http.entity.mime.content.FileBody;
16
17 import org.apache.http.client.methods.HttpPost;
18 import org.apache.http.entity.mime.MultipartEntityBuilder;
19 import org.apache.http.impl.client.HttpClientBuilder;
20 import org.apache.http.util.EntityUtils;
21
22 import java.io.File;
23 import java.io.InputStream;
24 import java.io.FileOutputStream;
25
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 public class SERVICENAMEClientBatch {
30
31 // service base URL
32 static final String textServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
33 static final String service = "SERVICENAME";
34
35 public static void main(String[] args) throws Exception {
36
37 // Get request parameters
38 System.out.print("Input ZIP file: ");
39 String fname = System.console().readLine();
40 System.out.print("Output ZIP file: ");
41 String outfname = System.console().readLine();
42 System.out.print("Language: ");
43 String lang = System.console().readLine();
44 System.out.print("Output format (xml,json,conll,naf): ");
45 String out = System.console().readLine();
46 System.out.print("TextServer Username: ");
47 String user = System.console().readLine();
48 System.out.print("TextServer Password: ");
49 String pwd = System.console().readLine();
50
51 // Create request, fill query parameters
52 HttpPost request = new HttpPost(textServer_URL+"/processQuery/"+service);
53 request.setEntity(MultipartEntityBuilder
54 .create()
55 .addTextBody("username", user)
56 .addTextBody("password", pwd)
57 .addPart("file", new FileBody(new File(fname)))
58 .addTextBody("language", lang)
59 .addTextBody("output", out)
60 .addTextBody("interactive", "0")
61 .build() );
62
63 // create client, send request, get response
64 HttpClient client = HttpClientBuilder.create().build();
65 HttpResponse response = client.execute(request);
66 String content = EntityUtils.toString(response.getEntity());
67
68 // check for communication errors
69 if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
70 System.out.println(response.getStatusLine() + " - " + content);
71 System.exit(1);
72 }
73
74 // Server response should include a job tokenID, retrieve it
75 Pattern pattern = Pattern.compile("<job_token_id>(.*)</job_token_id>");
76 Matcher matcher = pattern.matcher(content);
77 String tokenID="";
78 if (matcher.find())
79 tokenID = matcher.group(1);
80 else {
81 // unexpected response
82 System.out.println(content);
83 System.exit(1);
84 }
85
86 System.out.println("Job submitted. Token ID= "+tokenID);
87
88 // prepare request to poll for completion and retrieve results
89 request = new HttpPost(textServer_URL+"/resultRetrieve");
90 request.setEntity(MultipartEntityBuilder
91 .create()
92 .addTextBody("username", user)
93 .addTextBody("tokenID", tokenID)
94 .build() );
95
96 int seconds=20;
97 while (true) {
98
99 System.out.print("Sleeping "); System.out.print(seconds); System.out.println(" seconds");
100 Thread.sleep(seconds*1000);
101
102 System.out.println("Polling server for job completion");
103 response = client.execute(request);
104 content = EntityUtils.toString(response.getEntity());
105
106 // if status is OK, we got response, end waiting loop
107 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
108 break;
109
110 // we got an error code, lets see which one
111 if (response.getStatusLine().getStatusCode()==HttpStatus.SC_SERVICE_UNAVAILABLE &&
112 content.substring(0,8).equals("[TS-125]") ) {
113 // SC=503 and TS_code=TS-125 mean job is not finished. keep waiting
114 System.out.println("Job not finished yet.");
115 continue;
116 }
117 else {
118 // some unexpected error happened
119 System.out.println(content);
120 System.exit(1);
121 }
122 }
123
124 // write the received ZIP result to output file
125 System.out.println("Job finished. Saving results to "+outfname);
126
127 InputStream sin = response.getEntity().getContent();
128 byte data[] = new byte[sin.available()];
129 sin.read(data);
130
131 FileOutputStream outf = new FileOutputStream(outfname);
132 outf.write(data);
133 outf.close();
134 }
135 }