SchedulerImpl.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:6k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.task.impl;
  2. import net.javacoding.jspider.core.exception.*;
  3. import net.javacoding.jspider.core.task.*;
  4. import net.javacoding.jspider.core.task.work.DecideOnSpideringTask;
  5. import java.net.URL;
  6. import java.util.*;
  7. /**
  8.  * Default implementation of a Task scheduler
  9.  *
  10.  * $Id: SchedulerImpl.java,v 1.17 2003/04/25 21:29:04 vanrogu Exp $
  11.  *
  12.  * @author  G黱ther Van Roey
  13.  */
  14. public class SchedulerImpl implements Scheduler {
  15.     /** List of fetch tasks to be carried out. */
  16.     protected List fetchTasks;
  17.     /** List of thinker tasks to be carried out. */
  18.     protected List thinkerTasks;
  19.     /** Set of tasks that have been assigned. */
  20.     protected Set assignedSpiderTasks;
  21.     protected Set assignedThinkerTasks;
  22.     protected int spiderTasksDone;
  23.     protected int thinkerTasksDone;
  24.     protected Map blocked;
  25.     int blockedCount = 0;
  26.     public int getBlockedCount( ) {
  27.         return blockedCount;
  28.     }
  29.     public int getAssignedCount( ) {
  30.         return assignedSpiderTasks.size() + assignedThinkerTasks.size();
  31.     }
  32.     public int getJobCount() {
  33.         return getThinkerJobCount() + getSpiderJobCount();
  34.     }
  35.     public int getThinkerJobCount() {
  36.         return thinkerTasksDone + assignedThinkerTasks.size ( ) + thinkerTasks.size();
  37.     }
  38.     public int getSpiderJobCount() {
  39.         return spiderTasksDone + assignedSpiderTasks.size ( ) + fetchTasks.size();
  40.     }
  41.     public int getJobsDone() {
  42.       return getSpiderJobsDone() + getThinkerJobsDone();
  43.     }
  44.     public int getSpiderJobsDone() {
  45.         return spiderTasksDone;
  46.     }
  47.     public int getThinkerJobsDone() {
  48.         return thinkerTasksDone;
  49.     }
  50.     /**
  51.      * Public constructor?
  52.      */
  53.     public SchedulerImpl() {
  54.         fetchTasks = new ArrayList();
  55.         thinkerTasks = new ArrayList();
  56.         assignedThinkerTasks = new HashSet();
  57.         assignedSpiderTasks = new HashSet();
  58.         blocked = new HashMap();
  59.     }
  60.     public void block(URL siteURL, DecideOnSpideringTask task) {
  61.         ArrayList al = (ArrayList)blocked.get(siteURL);
  62.         if ( al == null ) {
  63.             al = new ArrayList();
  64.             blocked.put(siteURL, al);
  65.         }
  66.         int before = al.size();
  67.         al.add(task);
  68.         int after = al.size();
  69.         int diff = after-before;
  70.         blockedCount+=diff;
  71.     }
  72.     public DecideOnSpideringTask[] unblock(URL siteURL) {
  73.         ArrayList al = (ArrayList)blocked.remove(siteURL);
  74.         if ( al == null ) {
  75.             return new DecideOnSpideringTask[0];
  76.         } else {
  77.           blockedCount-=al.size();
  78.           return (DecideOnSpideringTask[]) al.toArray(new DecideOnSpideringTask[al.size()]);
  79.         }
  80.     }
  81.     /**
  82.      * Schedules a task to be processed.
  83.      * @param task task to be scheduled
  84.      */
  85.     public synchronized void schedule(WorkerTask task) {
  86.         if (task.getType() == WorkerTask.WORKERTASK_SPIDERTASK ) {
  87.             fetchTasks.add(task);
  88.         } else {
  89.             thinkerTasks.add(task);
  90.         }
  91.     }
  92.     /**
  93.      * Flags a task as done.
  94.      * @param task task that was complete
  95.      */
  96.     public synchronized void flagDone(WorkerTask task) {
  97.         if (task.getType() == WorkerTask.WORKERTASK_THINKERTASK ) {
  98.             assignedThinkerTasks.remove(task);
  99.             thinkerTasksDone++;
  100.         }else{
  101.             assignedSpiderTasks.remove(task);
  102.             spiderTasksDone++;
  103.         }
  104.     }
  105.     public synchronized WorkerTask getThinkerTask() throws TaskAssignmentException {
  106.         if (thinkerTasks.size() > 0) {
  107.             WorkerTask task = (WorkerTask) thinkerTasks.remove(0);
  108.             assignedThinkerTasks.add(task);
  109.             return task;
  110.         }
  111.         if (allTasksDone()) {
  112.             throw new SpideringDoneException();
  113.         } else {
  114.             throw new NoSuitableItemFoundException();
  115.         }
  116.     }
  117.     /**
  118.      * Returns a fetch task to be carried out.
  119.      * @return WorkerTask task to be done
  120.      * @throws TaskAssignmentException notifies when the work is done or there
  121.      * are no current outstanding tasks.
  122.      */
  123.     public synchronized WorkerTask getFethTask() throws TaskAssignmentException {
  124.         if (fetchTasks.size() > 0) {
  125.             WorkerTask task = (WorkerTask) fetchTasks.remove(0);
  126.             assignedSpiderTasks.add(task);
  127.             return task;
  128.         }
  129.         if (allTasksDone()) {
  130.             throw new SpideringDoneException();
  131.         } else {
  132.             throw new NoSuitableItemFoundException();
  133.         }
  134.     }
  135.     /**
  136.      * Determines whether all the tasks are done.   If there are no more tasks
  137.      * scheduled for process, and no ongoing tasks, it is impossible that new
  138.      * work will arrive, so the spidering is done.
  139.      * @return boolean value determining whether all work is done
  140.      */
  141.     public synchronized boolean allTasksDone() {
  142.         return (fetchTasks.size() == 0 &&
  143.                 thinkerTasks.size() == 0 &&
  144.                 assignedSpiderTasks.size() == 0 &&
  145.                 assignedThinkerTasks.size() == 0 &&
  146.                 blocked.size() == 0 );
  147.     }
  148.     /*
  149.     public synchronized String toString ( ) {
  150.         StringBuffer sb = new StringBuffer();
  151.         Iterator it = this.thinkerTasks.iterator();
  152.         while ( it.hasNext() ) {
  153.             System.out.println("TH . " + it.next().getClass());
  154.         }
  155.         it = this.assignedThinkerTasks.iterator();
  156.         while ( it.hasNext() ) {
  157.             System.out.println("TH A " + it.next().getClass());
  158.         }
  159.         it = this.fetchTasks.iterator();
  160.         while ( it.hasNext() ) {
  161.             System.out.println("SP . " + it.next().getClass());
  162.         }
  163.         it = this.assignedSpiderTasks.iterator();
  164.         while ( it.hasNext() ) {
  165.             System.out.println("SP A " + it.next().getClass());
  166.         }
  167.         return sb.toString();
  168.     }   */
  169. }