使用org.apache.commons.httpclient.HttpClient将数据流发送到多个URL

时间:2014-01-14 14:57:18

标签: java apache ibm-midrange

首先,我要说我不是java程序员,我是IBM Iseries的程序员。但是,我的任务是将当前发送数据流的当前Java应用程序更改为一个URL,该URL允许根据属性文件将相同的数据流发送到多个URL。我们的Java应用程序在Iseries上运行,我们使用org.apache.commons.httpclient.HttpClient类发送数据并处理响应。现在一切都很好,但我想知道是否有人能指出我正确的方向来完成这项任务。

基本上,我需要将相同的数据块发送到同一个线程或实例中的多个URL。我不确定是否有可能或最好的方法来完成这个。那么,有没有办法在同一个线程中创建多个实例,将多个URL发送到同一个数据流?在你开始评论之前,我会再次说我不是一个java程序员,我甚至不确定如何说出这个问题。

添加了代码示例:

public class Replication_CC implements TextProcessor {

public static String VERSION = "v2014.1.0";
static Logger log = Logger.getLogger(Replication_CC.class);
String url;
int retries = 1;

public Replication_CC(Properties p) {

    super();
    url = p.getProperty("url");
    log.info("Service URL set to " + url);
    retries = PropertiesUtil.getOptionalIntProperty(p, "retries", 1);
    log.info("Retries set to " + retries);
}

public static void main(String[] args) throws Exception {

    log.info("Replication " + VERSION);
    log.info("Initializing...");

    Properties p = PropertiesUtil.loadProperties(Replication_CC.class.getResource("/Replication_CC.properties"));
    DQServer server = new DQServer(p, new Replication_CC(p));
    server.run();
}

public String process(String request) throws Exception {

    long processStart = System.currentTimeMillis();
    String response = null;
    for (int i=0; i<=retries; i++) {
        try {
            response = send(request, url);
            if (response!=null) break;
        }
        catch (Exception e) {
            log.warn("Error processing:  " + e.getMessage());
            if (i<retries) {
                log.warn("Trying again (retry " + (i+1) + "...");
            }
            else {
                log.error("Giving up on this transaction.");
                break;
            }
        }
    }
    long processFinished = System.currentTimeMillis();
    log.info("Request was processed in " + (processFinished-processStart) + "ms.");
    return response;
}

public String send(String request, String url) throws Exception {
    log.debug("Creating request...");
    HttpClientParams params = new HttpClientParams();
    params.setParameter("http.useragent", "http-api / Replication");
    HttpClient client = new HttpClient(params);
    PostMethod post = new PostMethod(url);
    /*
    List<NameValuePair>  params = new ArrayList<NameValuePair>();
    for (String key : globalRequest.keySet()) {
        params.add(nvp(key, globalRequest.get(key)));
    }
    */
    post.setRequestBody(request);

    // Log the request
    if (log.isDebugEnabled()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        post.getRequestEntity().writeRequest(baos);
        baos.close();
        log.debug("HTTP Request: \n" + StringUtils.repeat("*", 100) + "\n" + "Content Type: "
                + post.getRequestEntity().getContentType() + "\n" + "Content Length: "
                + post.getRequestEntity().getContentLength() + "\n" + "Request Headers: "
                + ArrayUtils.toString(post.getRequestHeaders()) + "\n" + "Request Params: " + baos.toString() + "\n" + 
        StringUtils.repeat("*", 100));
    }

    try {   
        log.info("Sending request...");
        int responseCode = client.executeMethod(post);
        //log.debug(String.format("Http Response Code [%s]", responseCode));
        log.debug("Http Response Code [" + responseCode + "]");

        if (responseCode == HttpStatus.SC_OK) {
            String charset = post.getResponseCharSet();
            log.debug("Response Character Set [" + charset + "]");
            /*
            byte[] body = post.getResponseBody();
            String response = new String(body, charset);
            */

            String response = IOUtils.toString(post.getResponseBodyAsStream());
            log.debug("Response Body: \n" + response);
            return response;
        }
        else {
            throw new Exception(post.getStatusLine().toString());       
        }
    }
    catch (IOException ioe) {
        log.error(ioe);
        throw ioe;
    }
    finally {
        post.releaseConnection();
    }
}

1 个答案:

答案 0 :(得分:1)

一种简单的方法是在由唯一字符分隔的现有url属性中包含多个URL。我选择了“|” (管道)在这个例子中,因为它不太可能在普通网址中看到管道。

Java通过名称和参数签名来识别方法。我们可以通过向现有流程方法添加String url参数并创建一个新的process(String request)方法来对我们有利,该方法将拆分并遍历网址。唯一的缺点是它只会返回DQServer类的最后一个响应。

public String process(String request) throws Exception {
    String response;
    for (String u : url.split("\\|")) {
        response = process(request, u);
    }
    return response;
}

public String process(String request, String url) throws Exception {

    long processStart = System.currentTimeMillis();
    String response = null;
    for (int i=0; i<=retries; i++) {
        try {
            response = send(request, url);
            if (response!=null) break;
        }
        catch (Exception e) {
            log.warn("Error processing:  " + e.getMessage());
            if (i<retries) {
                log.warn("Trying again (retry " + (i+1) + "...");
            }
            else {
                log.error("Giving up on this transaction.");
                break;
            }
        }
    }
    long processFinished = System.currentTimeMillis();
    log.info("Request was processed in " + (processFinished-processStart) + "ms.");
    return response;
}

<小时/> 完整示例可在GitHub Gist上找到。