Java tutorial
/* * @(#)ProxyPool.java 2014-12-11 ?10:00:50 proxy_pool Copyright 2014 * Isoftstone, Inc. All rights reserved. ISOFTSTONE PROPRIETARY/CONFIDENTIAL. * Use is subject to license terms. */ package com.isoftstone.proxy.utils; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.isoftstone.proxy.api.model.ProxyVo; /** * ProxyPool * @author danhb * @date 2014-12-11 * @version 1.0 * */ public class ProxyPool { private static final Log LOG = LogFactory.getLog(ProxyPool.class); private int maxProxySize = 10000; private BlockingQueue<ProxyVo> proxyQueue = new ArrayBlockingQueue<ProxyVo>(maxProxySize); private static ProxyPool proxyPool; private ProxyPool() { } public static ProxyPool getInstance() { if (proxyPool == null) { proxyPool = new ProxyPool(); } return proxyPool; } /** * ???. * @param proxyList ?. */ public void insertProxyList(List<ProxyVo> proxyList) { if (CollectionUtils.isEmpty(proxyList)) { return; } //--HashSet??. HashSet<ProxyVo> hashSet = new HashSet<ProxyVo>(); if (CollectionUtils.isNotEmpty(this.proxyQueue)) { hashSet.addAll(this.proxyQueue); } hashSet.addAll(proxyList); this.proxyQueue.clear(); try { for (ProxyVo proxyVo : hashSet) { this.proxyQueue.offer(proxyVo, 1, TimeUnit.SECONDS); } } catch (InterruptedException e) { LOG.error("InterruptedException", e); } } /** * ??. * @return ? . */ public List<ProxyVo> getAllProxy() { return getProxyList(this.proxyQueue.size(), true); } /** * ?n?. * @param n ???. * @return ?List. */ public List<ProxyVo> getProxyList(int n, boolean isCheck) { List<ProxyVo> proxyList = new ArrayList<ProxyVo>(); if (n <= 0) { return proxyList; } int size = n; if (n > this.proxyQueue.size()) { size = this.proxyQueue.size(); } for (int i = 0; i < size; i++) { ProxyVo proxyVo = null; if (isCheck) { proxyVo = this.getProxy4Check(); } else { proxyVo = this.getProxy(); } if (proxyVo == null) { continue; } proxyList.add(proxyVo); } return proxyList; } /** * ??. * @return ?. */ private ProxyVo getProxy4Check() { ProxyVo proxyVo = null; try { proxyVo = this.proxyQueue.poll(1, TimeUnit.SECONDS); if (proxyVo != null) { this.proxyQueue.offer(proxyVo, 1, TimeUnit.SECONDS); } } catch (InterruptedException e) { LOG.error("InterruptedException", e); } return proxyVo; } public ProxyVo getProxy() { return getProxy(0); } /** * ??. * @return ?. */ public ProxyVo getProxy(int responseSpeed) { ProxyVo proxyVo = null; try { proxyVo = this.proxyQueue.poll(1, TimeUnit.SECONDS); if (proxyVo != null) { this.proxyQueue.offer(proxyVo, 1, TimeUnit.SECONDS); } if (proxyVo != null && !proxyVo.getIsAvailable()) { proxyVo = getProxy(responseSpeed); } if (responseSpeed > 0 && proxyVo.getResponseSpeed() > responseSpeed) { proxyVo = getProxy(responseSpeed); } } catch (InterruptedException e) { LOG.error("InterruptedException", e); } return proxyVo; } /** * ???. * @param proxy ?. */ public void addProxy(ProxyVo proxy) { try { this.proxyQueue.offer(proxy, 1, TimeUnit.SECONDS); } catch (InterruptedException e) { LOG.error("InterruptedException", e); } } /** * ??. * @param proxy */ public void removeProxy(ProxyVo proxy) { this.proxyQueue.remove(proxy); } public int getProxySize() { return this.proxyQueue.size(); } }