All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deep007.goniub.selenium.mitm.ChromeDriverTeam Maven / Gradle / Ivy

Go to download

基于selenium、browserproxy、okhttp等研发的工具包,方便使用。够牛逼!

There is a newer version: 2.0.2
Show newest version
package com.deep007.goniub.selenium.mitm;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.SessionId;

public abstract class ChromeDriverTeam {

	private LinkedBlockingQueue idleChromeDrivers = new LinkedBlockingQueue<>();

	private Map activeChromeDrivers = new ConcurrentHashMap<>();

	private int size;
	
	private volatile int createdSize = 0;

	public ChromeDriverTeam(int size) {
		this.size = size;
	}

	public static interface ChromeDriverTeamTask {
		void execute(ChromeDriver chromeDriver) throws Exception;
	}
	
	private synchronized ChromeDriver safeCreateChromeDriver() {
		ChromeDriver chromeDriver = createChromeDriver();
		createdSize ++;
		return chromeDriver;
	}

	public void execute(ChromeDriverTeamTask teamTask) throws InterruptedException {
		ChromeDriver chromeDriver = idleChromeDrivers.poll();
		if (chromeDriver == null) {
			if (createdSize < size) {
				chromeDriver = safeCreateChromeDriver();
			}else {
				chromeDriver = idleChromeDrivers.take();
			}
		}
		if (chromeDriver != null) {
			final ChromeDriver finalChromeDriver = chromeDriver;
			new Thread() {
				public void run() {
					SessionId sessionId = finalChromeDriver.getSessionId();
					try {
						activeChromeDrivers.put(sessionId, finalChromeDriver);
						teamTask.execute(finalChromeDriver);
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						activeChromeDrivers.remove(sessionId);
						idleChromeDrivers.add(finalChromeDriver);
					}
				};
			}.start();
		}
	}

	protected abstract ChromeDriver createChromeDriver();

	public void release() {
		while (true) {
			ChromeDriver chromeDriver = idleChromeDrivers.poll();
			if (chromeDriver != null) {
				chromeDriver.quit();
			}
			if (activeChromeDrivers.isEmpty() && idleChromeDrivers.isEmpty()) {
				break;
			}
			try {
				Thread.sleep(0);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy