commons.pool2 参数测试

package pool2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

/**
 * Created by Ken on 2014/5/27.
 */
public class WebClientFactory extends BasePooledObjectFactory<WebDriver> {

	static final org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(WebClientFactory.class);
	private static DesiredCapabilities capability = null;
	static {
		// 查找指定资源的URL,其中res.txt仍然开始的bin目录下
		// URL
		// plugin=SeleniumUtil.class.getResource("/plugin/killspinners-1.1.1-fx.xpi");
		// String plugin =
		// SeleniumUtil.class.getResource("/plugin/killspinners-1.1.1-fx.xpi").getPath();
		capability = DesiredCapabilities.firefox();
		capability.setJavascriptEnabled(true);
		FirefoxProfile firefoxProfile = new FirefoxProfile();
		// 去掉css
		// firefoxProfile.setPreference("permissions.default.stylesheet", 2);
		// 去掉图片
		// firefoxProfile.setPreference("permissions.default.image", 2);
		// 去掉flash
		firefoxProfile.setPreference(
				"dom.ipc.plugins.enabled.libflashplayer.so", false);
		try {
			//firefoxProfile.addExtension(createKillspinnersFile());
			//firefoxProfile.setPreference("extensions.firebug.currentVersion","1.8.1");// 避免启动画
			capability.setCapability("firefox_profile", firefoxProfile);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * 创建WebDriver
	 * 
	 * @throws Exception
	 * @return WebDriver
	 */
	public static WebDriver createWebDriver() throws Exception {
		/*
		 * DesiredCapabilities capability = DesiredCapabilities.firefox();
		 * capability.setJavascriptEnabled(true); FirefoxProfile firefoxProfile
		 * = new FirefoxProfile(); // 去掉css
		 * //firefoxProfile.setPreference("permissions.default.stylesheet", 2);
		 * // 去掉图片 //firefoxProfile.setPreference("permissions.default.image",
		 * 2); // 去掉flash firefoxProfile.setPreference(
		 * "dom.ipc.plugins.enabled.libflashplayer.so", false);
		 * 
		 * capability.setCapability("firefox_profile", firefoxProfile);
		 */

		WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.107:4444/wd/hub"), capability);
		// driver = new HtmlUnitDriver();
		// 如果3s内还定位不到则抛出异常
		driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
		// 页面加载超时时间设置为5s
		driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
		// driver.manage.script_timeout = 3 #seconds
		driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
		System.out.println("create driver is"+driver.hashCode());
		return driver;

	}

	public static File createKillspinnersFile() {
		String path = WebClientFactory.class.getProtectionDomain()
				.getCodeSource().getLocation().getPath();
		File file = new File(path);
		File killspinners = null;
		if (file.exists()) {
			killspinners = new File(file.isDirectory() ? file
					: file.getParentFile(), "killspinners-1.1.1-fx.xpi");
			if (killspinners.isFile() && killspinners.exists()) {
				return killspinners;
			}
		} else {
			log.error("localtion path is not exists");
		}
		URL plugin = WebClientFactory.class
				.getResource("/plugin/killspinners-1.1.1-fx.xpi");
		InputStream in = null;
		try {
			in = plugin.openStream();
			FileUtils.writeByteArrayToFile(killspinners,
					IOUtils.toByteArray(in));
			log.error("createKillspinnersFile succeed");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
		return killspinners;

	}

	/**
	 * 检测webDeriver 是否正常
	 * 
	 * @param driver
	 * @return
	 */
	public static boolean checkWebDriver(WebDriver driver) {
		WebDriver newWebDriver = null;
		String url = null;
		boolean falg = true;
		try {
			driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
			url = driver.getCurrentUrl();
			log.info("checkWebDriver success!");
			newWebDriver = driver;
		} catch (Exception e) {
			url = null;
			log.error("checkWebDriver  is error!", e);
		} finally {
			if (url == null) {
				falg = false;
			}
		}
		return falg;
	}

	@Override
	public WebDriver create() throws Exception {
		return createWebDriver();
	}

	@Override
	public PooledObject<WebDriver> wrap(WebDriver obj) {
		return new DefaultPooledObject<WebDriver>(obj);
	}

	@Override
	public boolean validateObject(PooledObject<WebDriver> p) {
		return checkWebDriver(p.getObject());
	}

	@Override
	public void passivateObject(PooledObject<WebDriver> p) throws Exception {
		super.passivateObject(p);
	}

	@Override
	public void destroyObject(PooledObject<WebDriver> p) throws Exception {
		System.out.println("destroyObject driver is"+p.getObject().hashCode());
		p.getObject().quit();
		super.destroyObject(p);

	}
	
	

}

  

 

package pool2;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.openqa.selenium.WebDriver;

public class Test {

	public static void main(String[] args)throws Exception {
		GenericObjectPoolConfig config = new GenericObjectPoolConfig();
		config.setMaxIdle(2);//最大空闲数
		config.setMinIdle(1);//最小空闲数.相当于初始化的数量
		config.setMaxTotal(4);//总数
		
		
		//回收时间等于setMinEvictableIdleTimeMillis+setTimeBetweenEvictionRunsMillis
		
		//最大空闲时间-在Pool中,实例空闲的最长时间
		config.setMinEvictableIdleTimeMillis(2000);
		//设置收回时间
		config.setTimeBetweenEvictionRunsMillis(3000);
		

		WebClientFactory webClientFactory = new WebClientFactory();
		GenericObjectPool<WebDriver> pool = new GenericObjectPool<WebDriver>(
				webClientFactory,config);
		
		pool.borrowObject();
		pool.borrowObject();
		int i=60;
		while(i!=0){
			System.out.println("getDestroyedCount"+pool.getDestroyedCount());
			System.out.println("getBorrowedCount"+pool.getBorrowedCount());
			System.out.println("getCreatedCount"+pool.getCreatedCount());
			System.out.println("-----------------------------------------------------");
			Thread.sleep(1000);
			i--;
		}
		
		
		
	}
}

 

你可能感兴趣的:(commons.pool2 参数测试)