Difference between revisions of "Java Batch Client"

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