java多线程发送URL请求




import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* This Class to test the PrintNet processing the snapshot performance.
* It's mainly to solve the out of memory issue.
*
* @author yfan
*
*/
public class PnSnapshotPerformanceTest extends Thread {

//TODO threadNumber
private static final int threadNumber = 50;//50

private static final int perThreadCreatSnpNumber = 20;//20

private static final String urlStr = "http://**************com:6080/pn_entry.jsp";

private static final SimpleDateFormat dateFomat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms");

public PnSnapshotPerformanceTest() {
super();
}

public PnSnapshotPerformanceTest(String name) {
super(name);
}

@Override
public void run() {
for (int i = 1; i <= perThreadCreatSnpNumber; i++) {
long startTime = System.currentTimeMillis();
System.out.println(getCurrDateStr() + " " + this.getName() + "_" + i + ": Start to handle the snapshot <<<<<<<<<<<<<<<<<<<<<<<<<<" + getCurrentMemoryInfo());

try {
doPost();// send post request
} catch (Exception e) {
e.printStackTrace();
}

long useTime = System.currentTimeMillis() - startTime;
System.out.println(getCurrDateStr() + " " +this.getName() + "_" + i + ": Handle the snapshot end >>>>>>>>>>>>>>>>>>>>>>>>>>>>use time: " + useTime/60000 + ":" + useTime%60000/1000 + ":" + useTime%60000%1000);
}
}

public void doPost() {
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.setUseCaches(false);

//TODO test
String snapshotStr = readFile("C:/bill/HP/eclipse_workspace/DailyPractice/src/d20130304/snapshot.xml");
//String snapshotStr = readFile("C:/bill/HP/eclipse_workspace/DailyPractice/src/d20130304/snapshot_2.xml");
StringBuffer sb = new StringBuffer();
addPair(sb, "XML_string", snapshotStr);
String parametersStr = sb.substring(0, sb.length() - 1);
//System.out.println("parametersStr: \n" + parametersStr.substring(0, 1000));

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(parametersStr);
out.flush();
out.close();

//conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}

line = sb.toString();
System.out.println("return line: \n" + line);
//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>Send request end. ");

System.out.println("conn.getResponseCode(): " + conn.getResponseCode());
} catch (IOException e) {
// handle e
System.out.println(">>>>>>>>>>>>>>IOException: " + e);
} finally {
if (conn != null) {
conn.disconnect();
//System.out.println("Disconnect successfully. ");
}
}
}

private String readFile(String filePath) {
FileReader fr = null;
BufferedReader br = null;
StringBuffer sBuffer = new StringBuffer();

try {
fr = new FileReader(filePath);// d20130304/snapshot.xml
} catch (FileNotFoundException e) {
e.printStackTrace();
}

br = new BufferedReader(fr);
String tempLine;
try {
tempLine = br.readLine();
while (tempLine != null) {
sBuffer.append(tempLine);
sBuffer.append("\n");
tempLine = br.readLine();
}
//System.out.println("sBuffer: \n" + sBuffer);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sBuffer.toString().trim();
}

public static void addPair(StringBuffer sb, String name, String value)
throws UnsupportedEncodingException {
if (value == null) {
return;
}
sb.append(name);
sb.append("=");
sb.append(java.net.URLEncoder.encode(value, "UTF-8"));
sb.append("&");
}

public static String getCurrDateStr() {
Date date = new Date();
return dateFomat.format(date);
}

public String getCurrentMemoryInfo () {
StringBuilder sb = new StringBuilder();
try {
Runtime runtime = Runtime.getRuntime();

NumberFormat format = NumberFormat.getInstance();

long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

sb.append("free memory: " + format.format(freeMemory / 1048576) + "; ");
sb.append("allocated memory: " + format.format(allocatedMemory / 1048576)
+ "; ");
sb.append("max memory: " + format.format(maxMemory / 1048576) + "; ");
sb.append("total free memory: "
+ format.format((freeMemory + (maxMemory - allocatedMemory)) / 1048576)
+ "; ");

return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}

return sb.toString();
}

public static void main(String[] args) {
for (int i = 1; i <= threadNumber; i++) {
PnSnapshotPerformanceTest pspt = new PnSnapshotPerformanceTest("Thread_" + i);
pspt.start();
}
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值