使用org.apache.commons.httpclient.httpclient向多个URL发送数据流



首先,让我说我不是java程序员,我是IBM Iseries的程序员。然而,我的任务是更改当前的java应用程序,该应用程序当前将数据流发送到一个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();
}
}

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

Java通过名称和参数签名来识别方法。我们可以通过在现有的过程方法中添加一个String url参数,并创建一个新的process(String request)方法来拆分和迭代url,从而利用这一点。唯一的缺点是它只会向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上获得。

最新更新