PageRenderTime 602ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/dd-web/src/main/java/ao/dd/web/proxy/pool/ProxyPoolImpl.java

http://digital-delegation.googlecode.com/
Java | 209 lines | 158 code | 27 blank | 24 comment | 5 complexity | 1cab55e1bebdc54fc99b1e75290e8e27 MD5 | raw file
  1. package ao.dd.web.proxy.pool;
  2. import java.io.InterruptedIOException;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.List;
  8. import java.util.concurrent.Callable;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import org.apache.log4j.Logger;
  12. import ao.dd.web.RequestError;
  13. import ao.dd.web.apache.ApacheSessionManager;
  14. import ao.dd.web.apache.Session;
  15. import ao.dd.web.apache.SessionManager;
  16. import ao.dd.web.proxy.live.LiveProxyStore;
  17. import ao.dd.web.proxy.model.AnonProxy;
  18. import ao.dd.web.proxy.select.UcbSelector;
  19. import ao.dd.web.proxy.store.ProxyStore;
  20. import ao.dd.web.proxy.store.ProxyStore.Processor;
  21. import ao.util.time.Sched;
  22. import com.google.inject.AbstractModule;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Inject;
  25. /**
  26. *
  27. */
  28. public class ProxyPoolImpl implements ProxyPool
  29. {
  30. //--------------------------------------------------------------------
  31. private final static Logger LOG =
  32. Logger.getLogger(ProxyPoolImpl.class);
  33. public static ProxyPool newInstance()
  34. {
  35. return Guice.createInjector(new AbstractModule() {
  36. @Override protected void configure() {
  37. }
  38. }).getInstance(ProxyPoolImpl.class);
  39. }
  40. //--------------------------------------------------------------------
  41. public static void main(String args[]) throws InterruptedException
  42. {
  43. ProxyPool proxyPool = newInstance();
  44. for (int i = 0; i < 1000; i++) {
  45. proxyPool.submitAndWait(Arrays.<Worker>asList(new Worker() {
  46. public void work(Session with) throws Throwable {
  47. with.load(new URL("http://www.google.com/"));
  48. System.out.println("DONE google!!!");
  49. }}, new Worker() {
  50. public void work(Session with) throws Throwable {
  51. with.load(new URL("http://www.perl.com/"));
  52. System.out.println("DONE perl!!!");
  53. }}, new Worker() {
  54. public void work(Session with) throws Throwable {
  55. with.load(new URL("http://www.slashdot.org/"));
  56. System.out.println("DONE slashdot!!!");
  57. }
  58. }));
  59. Sched.sleep(60 * 1000);
  60. }
  61. // proxyPool = null;
  62. // System.gc();
  63. }
  64. //--------------------------------------------------------------------
  65. private static final int CONCURRANCY = 2;
  66. private static final int RETRY_COUNT = 1000;
  67. //--------------------------------------------------------------------
  68. private final ProxyStore STORE;
  69. private final SessionManager mgr = new ApacheSessionManager();
  70. private final ExecutorService exec =
  71. Executors.newFixedThreadPool(CONCURRANCY);
  72. //--------------------------------------------------------------------
  73. @Inject public ProxyPoolImpl(
  74. LiveProxyStore store)
  75. {
  76. STORE = store;
  77. }
  78. //--------------------------------------------------------------------
  79. /**
  80. * Submits a task and waits for its completion,
  81. * the task will be restarted as long as it takes to
  82. * complete the task without timing out.
  83. *
  84. * @param forWorker the task to complete
  85. */
  86. public void submitAndWait(Worker forWorker)
  87. {
  88. int i = 0;
  89. while (i++ < RETRY_COUNT)
  90. {
  91. boolean success = doSubmitAndWait(forWorker);
  92. if (success) return;
  93. LOG.trace("unsuccessful doSubmitAndWait");
  94. Sched.sleep(i * 100);
  95. }
  96. throw new RequestError("Retry count exhausted.");
  97. }
  98. private boolean doSubmitAndWait(
  99. final Worker forWorker)
  100. {
  101. LOG.trace("processing store");
  102. return STORE.process(new UcbSelector(),
  103. new Processor() {
  104. public boolean process(AnonProxy proxy) {
  105. LOG.trace("processing " + proxy);
  106. return doSubmitAndWait(
  107. forWorker,
  108. // proxy.open(new ApacheSessionManager()));
  109. proxy.open(mgr));
  110. }
  111. });
  112. }
  113. private boolean doSubmitAndWait(
  114. Worker worker, Session session)
  115. {
  116. try
  117. {
  118. return workOnSession(worker, session);
  119. }
  120. catch (Throwable throwable)
  121. {
  122. if (!(throwable.getCause() instanceof
  123. InterruptedIOException))
  124. {
  125. try
  126. {
  127. return workOnSession(worker, session);
  128. }
  129. catch (Throwable throwableB)
  130. {
  131. LOG.debug("workOnSession failed: " +
  132. throwableB.getMessage());
  133. // throwableB.printStackTrace();
  134. // System.out.println(
  135. // throwableB.getMessage());
  136. }
  137. }
  138. else
  139. {
  140. LOG.debug("skipping " + throwable.getCause());
  141. }
  142. }
  143. return false;
  144. }
  145. private boolean workOnSession(
  146. Worker worker, Session session)
  147. throws Throwable
  148. {
  149. worker.work( session );
  150. return true;
  151. }
  152. //--------------------------------------------------------------------
  153. public void submitAndWait(Worker... workers)
  154. {
  155. submitAndWait( Arrays.asList(workers) );
  156. }
  157. @SuppressWarnings("unchecked")
  158. public void submitAndWait(List<Worker> workers)
  159. {
  160. final Collection<Callable<Void>> tasks =
  161. new ArrayList<Callable<Void>>();
  162. for (final Worker worker : workers)
  163. {
  164. tasks.add(new Callable() {
  165. public Void call() throws Exception {
  166. submitAndWait(worker);
  167. return null;
  168. }
  169. });
  170. }
  171. try
  172. {
  173. LOG.trace("submitting " + tasks.size() + " tasks");
  174. exec.invokeAll( tasks );
  175. }
  176. catch (Exception e)
  177. {
  178. LOG.error("Could not invoke all workers", e);
  179. // e.printStackTrace();
  180. }
  181. }
  182. }