SimpleProxyProvider #535
parent
62a6985103
commit
eb632a93d3
@ -0,0 +1,58 @@
|
||||
package us.codecraft.webmagic.proxy;
|
||||
|
||||
import us.codecraft.webmagic.Page;
|
||||
import us.codecraft.webmagic.Task;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* A simple ProxyProvider. Provide proxy as round-robin without heartbeat and error check. It can be used when all proxies are stable.
|
||||
* @author code4crafter@gmail.com
|
||||
* Date: 17/4/16
|
||||
* Time: 10:18
|
||||
* @since 0.7.0
|
||||
*/
|
||||
public class SimpleProxyProvider implements ProxyProvider {
|
||||
|
||||
private final List<Proxy> proxies;
|
||||
|
||||
private final AtomicInteger pointer;
|
||||
|
||||
private SimpleProxyProvider(List<Proxy> proxies, AtomicInteger pointer) {
|
||||
this.proxies = proxies;
|
||||
this.pointer = pointer;
|
||||
}
|
||||
|
||||
public static SimpleProxyProvider from(Proxy... proxies) {
|
||||
List<Proxy> proxiesTemp = new ArrayList<Proxy>(proxies.length);
|
||||
for (Proxy proxy : proxies) {
|
||||
proxiesTemp.add(proxy);
|
||||
}
|
||||
return new SimpleProxyProvider(Collections.unmodifiableList(proxiesTemp), new AtomicInteger(-1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void returnProxy(Proxy proxy, Page page, Task task) {
|
||||
//Donothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public Proxy getProxy(Task task) {
|
||||
return proxies.get(incrForLoop());
|
||||
}
|
||||
|
||||
private int incrForLoop() {
|
||||
int p = pointer.incrementAndGet();
|
||||
int size = proxies.size();
|
||||
if (p < size) {
|
||||
return p;
|
||||
}
|
||||
while (!pointer.compareAndSet(p, p % size)) {
|
||||
p = pointer.get();
|
||||
}
|
||||
return p % size;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package us.codecraft.webmagic.proxy;
|
||||
|
||||
import org.junit.Test;
|
||||
import us.codecraft.webmagic.Site;
|
||||
import us.codecraft.webmagic.Task;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
* Date: 17/4/16
|
||||
* Time: 上午10:29
|
||||
*/
|
||||
public class SimpleProxyProviderTest {
|
||||
|
||||
public static final Task TASK = Site.me().toTask();
|
||||
|
||||
@Test
|
||||
public void test_get_proxy() throws Exception {
|
||||
Proxy originProxy1 = new Proxy("127.0.0.1", 1087);
|
||||
Proxy originProxy2 = new Proxy("127.0.0.1", 1088);
|
||||
SimpleProxyProvider proxyProvider = SimpleProxyProvider.from(originProxy1, originProxy2);
|
||||
Proxy proxy = proxyProvider.getProxy(TASK);
|
||||
assertThat(proxy).isEqualTo(originProxy1);
|
||||
proxy = proxyProvider.getProxy(TASK);
|
||||
assertThat(proxy).isEqualTo(originProxy2);
|
||||
proxy = proxyProvider.getProxy(TASK);
|
||||
assertThat(proxy).isEqualTo(originProxy1);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue