デュアルセンタ同期、httpclient、スレッドプール非同期要求
5935 ワード
よびだし
スレッドプールの非同期要求と戻り値の記録
httpclientリクエスト
ツールメソッド
List<String> list=SyncUtil.syncData("PortalSyncAddr", "commonData", syncData, workID[0] ,20);
String syncResult=JSON.toJSONString(list);
return syncResult+"workID:"+workID;
スレッドプールの非同期要求と戻り値の記録
package com.ffcs.wlan.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.log4j.Logger;
public class SyncUtil {
private static Logger log = Logger.getLogger(SyncUtil.class);
/**
*
* @param syncTo URL Request ( 100 url OracleSyncAddr|PortalSyncAddr)
* @param synMethod URL
* @param sysData ( , , null)
* @param workID (String workId = WifiUtil.getSerialStr())
* @param threadPoolSize
* @return
*/
public static List<String> syncData(String syncTo, String synMethod, String sysData , String workID ,int threadPoolSize) {
ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);// 20
List<String> sysncAddrList =WifiUtil.getSyncAddr(syncTo);//
List<Future<String>> resultFutureList = new ArrayList<Future<String>>();//
List<String> resultStringList = new ArrayList<String>();// ,
//
for (String urlAddr : sysncAddrList) {
Future<String> future = executorService.submit(new HttpCallUtil(
urlAddr+synMethod, sysData));
resultFutureList.add(future);//
}
//
for (Future<String> future : resultFutureList) {
try {
String resultString=future.get();
resultStringList.add(resultString);// list
log.info(resultString+" workID:"+workID); // ( )
} catch (Exception e) {
log.error(e.getMessage());
} finally {
executorService.shutdown();// , Executor.execute() ( , 20 , 100 ,80 ) 。
}
}
while(!executorService.isTerminated()){//
}
log.info("workID:"+workID+" finish all job");
return resultStringList;
}
}
httpclientリクエスト
package com.ffcs.wlan.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class HttpCallUtil implements Callable<String>{
private static Logger log = Logger.getLogger(HttpCallUtil.class);
private String url;
private String syncData;
public HttpCallUtil(String url, String syncData) {
this.syncData = syncData;
this.url = url;
}
public String call() throws Exception {
return ("HttpCall result :" + getRequest(url, syncData) + ",HttpCall URL:" + url);
}
public static String getRequest(String url, String syncData)throws Exception {
String result="";
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("syncData", syncData));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
return EntityUtils.toString(response.getEntity());
} catch (Exception e) {
log.error(e.getMessage());
} finally {
response.close();
}
} catch (Exception e) {
log.error(e.getMessage());
result=e.getMessage();
} finally {
httpclient.close();
}
return result;
}
}
ツールメソッド
/**
* 14 ,( )
* 10000 / , 3
* , , 。
*
* @return a serial string of length 14
*/
public static String getSerialStr(){
String randomStr=String.valueOf(getRandom(1,9999));
String str_len="0000";
String sufix = str_len.substring(0, 4-randomStr.length())+randomStr;
String prefix =String.valueOf(new Date().getTime()).substring(3);
return prefix+sufix;
}
/**
*
* @param min
* @param max The param max must be greater than param min
* @return a random code between min number and max number.
*/
public static int getRandom(int min,int max){
if(min>max){
int temp = min;
max = min;
min = temp;
}
int num=(int)Math.rint(Math.random()*max);
if(num<min)
num=num + min;
return num;
}
/**
*
* @param params
* @return true
*/
public static Boolean paramHasNull(String...params){
for (String param : params) {
if(param==null || param.equals(""))
return true;
}
return false;
}
/**
* ( url)
* @return
*/
public static List<String> getSyncAddr(String propertyName) {
//
ResourceBundle rb = ResourceBundle.getBundle(propertyName.trim());
// key
Enumeration<String> allKey = rb.getKeys();
// key value
List<String> addrList = new ArrayList<String>();
while (allKey.hasMoreElements()) {
String key = allKey.nextElement();
String addr = (String) rb.getString(key);
addrList.add(addr);
}
return addrList;
}