双中心同步,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;
	}
	



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值