Difference between revisions of "Java Batch Client"

From Textserver wiki
Jump to: navigation, search
Line 10: Line 10:
 
//
 
//
 
////////////////////////////////////////////////////////////////////////////
 
////////////////////////////////////////////////////////////////////////////
 +
  
 
import org.apache.http.client.HttpClient;
 
import org.apache.http.client.HttpClient;
Line 39: Line 40:
 
         System.out.print("Input ZIP file: ");
 
         System.out.print("Input ZIP file: ");
 
         String fname = System.console().readLine();
 
         String fname = System.console().readLine();
        System.out.print("Output ZIP file: ");
 
        String outfname = System.console().readLine();
 
 
         System.out.print("Language: ");
 
         System.out.print("Language: ");
 
         String lang = System.console().readLine();
 
         String lang = System.console().readLine();
Line 85: Line 84:
 
         }
 
         }
 
          
 
          
         System.out.println("Job submitted. Token ID= "+tokenID);
+
         System.out.println("Job submitted. Token ID="+tokenID);
 
+
        // prepare request to poll for completion and retrieve results
+
        request = new HttpPost(textServer_URL+"/resultRetrieve");
+
        request.setEntity(MultipartEntityBuilder
+
                          .create()
+
                          .addTextBody("username", user)
+
                          .addTextBody("tokenID", tokenID)
+
                          .build() );
+
 
+
        int seconds=20;
+
        while (true) {
+
           
+
            System.out.print("Sleeping "); System.out.print(seconds); System.out.println(" seconds");
+
            Thread.sleep(seconds*1000);
+
           
+
            System.out.println("Polling server for job completion");
+
            response = client.execute(request);
+
            content = EntityUtils.toString(response.getEntity());
+
 
+
            // if status is OK, we got response, end waiting loop
+
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
+
                break;
+
           
+
            // we got an error code, lets see which one
+
            if (response.getStatusLine().getStatusCode()==HttpStatus.SC_SERVICE_UNAVAILABLE &&
+
                content.substring(0,8).equals("[TS-125]") ) {
+
                // SC=503 and TS_code=TS-125 mean job is not finished. keep waiting
+
                System.out.println("Job not finished yet.");
+
                continue;
+
            }
+
            else {
+
                // some unexpected error happened
+
                System.out.println(content);
+
                System.exit(1);
+
            }
+
        }
+
 
+
// write the received ZIP result to output file
+
        System.out.println("Job finished. Saving results to "+outfname);
+
 
+
        InputStream sin = response.getEntity().getContent();
+
        byte data[] = new byte[sin.available()];
+
        sin.read(data);
+
 
+
        FileOutputStream outf = new FileOutputStream(outfname);
+
        outf.write(data);
+
        outf.close();
+
 
     }
 
     }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:05, 21 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 
13 import org.apache.http.client.HttpClient;
14 import org.apache.http.HttpResponse;
15 import org.apache.http.HttpStatus;
16 import org.apache.http.entity.mime.content.FileBody;
17    
18 import org.apache.http.client.methods.HttpPost;
19 import org.apache.http.entity.mime.MultipartEntityBuilder;
20 import org.apache.http.impl.client.HttpClientBuilder;
21 import org.apache.http.util.EntityUtils;
22 
23 import java.io.File;
24 import java.io.InputStream;
25 import java.io.FileOutputStream;
26 
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 
30 public class SERVICENAMEClientBatch {
31     
32     // service base URL
33     static final String textServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
34     static final String service = "SERVICENAME";
35     
36     public static void main(String[] args) throws Exception {
37 
38         // Get request parameters
39         System.out.print("Input ZIP file: ");
40         String fname = System.console().readLine();
41         System.out.print("Language: ");
42         String lang = System.console().readLine();
43         System.out.print("Output format (xml,json,conll,naf): ");
44         String out = System.console().readLine();
45         System.out.print("TextServer Username: ");
46         String user = System.console().readLine();
47         System.out.print("TextServer Password: ");
48         String pwd = System.console().readLine();
49 
50         // Create request, fill query parameters
51         HttpPost request = new HttpPost(textServer_URL+"/processQuery/"+service);
52         request.setEntity(MultipartEntityBuilder
53                           .create()
54                           .addTextBody("username", user)
55                           .addTextBody("password", pwd)
56                           .addPart("file", new FileBody(new File(fname)))
57                           .addTextBody("language", lang)
58                           .addTextBody("output", out)
59                           .addTextBody("interactive", "0")
60                           .build() );
61         
62         // create client, send request, get response
63         HttpClient client = HttpClientBuilder.create().build();
64         HttpResponse response = client.execute(request);
65         String content = EntityUtils.toString(response.getEntity());
66 
67         // check for communication errors
68         if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
69             System.out.println(response.getStatusLine() + " - " + content);
70             System.exit(1);
71         }
72 
73         // Server response should include a job tokenID, retrieve it
74         Pattern pattern = Pattern.compile("<job_token_id>(.*)</job_token_id>");
75         Matcher matcher = pattern.matcher(content);
76         String tokenID="";
77         if (matcher.find()) 
78             tokenID = matcher.group(1);
79         else {
80             // unexpected response
81             System.out.println(content);
82             System.exit(1);
83         }
84         
85         System.out.println("Job submitted. Token ID="+tokenID);
86     }
87 }