1 package net.javacoding.jspider.core.task.impl; 2 3 import net.javacoding.jspider.core.exception.*; 4 import net.javacoding.jspider.core.task.*; 5 import net.javacoding.jspider.core.task.work.DecideOnSpideringTask; 6 7 import java.net.URL ; 8 import java.util.*; 9 10 17 public class SchedulerImpl implements Scheduler { 18 19 20 protected List fetchTasks; 21 22 23 protected List thinkerTasks; 24 25 26 protected Set assignedSpiderTasks; 27 protected Set assignedThinkerTasks; 28 29 protected int spiderTasksDone; 30 protected int thinkerTasksDone; 31 32 protected Map blocked; 33 34 int blockedCount = 0; 35 36 public int getBlockedCount( ) { 37 return blockedCount; 38 } 39 40 public int getAssignedCount( ) { 41 return assignedSpiderTasks.size() + assignedThinkerTasks.size(); 42 } 43 44 public int getJobCount() { 45 return getThinkerJobCount() + getSpiderJobCount(); 46 } 47 48 public int getThinkerJobCount() { 49 return thinkerTasksDone + assignedThinkerTasks.size ( ) + thinkerTasks.size(); 50 } 51 52 public int getSpiderJobCount() { 53 return spiderTasksDone + assignedSpiderTasks.size ( ) + fetchTasks.size(); 54 } 55 56 public int getJobsDone() { 57 return getSpiderJobsDone() + getThinkerJobsDone(); 58 } 59 60 public int getSpiderJobsDone() { 61 return spiderTasksDone; 62 } 63 64 public int getThinkerJobsDone() { 65 return thinkerTasksDone; 66 } 67 68 69 72 public SchedulerImpl() { 73 fetchTasks = new ArrayList(); 74 thinkerTasks = new ArrayList(); 75 assignedThinkerTasks = new HashSet(); 76 assignedSpiderTasks = new HashSet(); 77 blocked = new HashMap(); 78 } 79 80 public void block(URL siteURL, DecideOnSpideringTask task) { 81 ArrayList al = (ArrayList)blocked.get(siteURL); 82 if ( al == null ) { 83 al = new ArrayList(); 84 blocked.put(siteURL, al); 85 } 86 int before = al.size(); 87 al.add(task); 88 int after = al.size(); 89 90 int diff = after-before; 91 blockedCount+=diff; 92 } 93 94 public DecideOnSpideringTask[] unblock(URL siteURL) { 95 ArrayList al = (ArrayList)blocked.remove(siteURL); 96 if ( al == null ) { 97 return new DecideOnSpideringTask[0]; 98 } else { 99 blockedCount-=al.size(); 100 return (DecideOnSpideringTask[]) al.toArray(new DecideOnSpideringTask[al.size()]); 101 } 102 } 103 104 108 public synchronized void schedule(WorkerTask task) { 109 if (task.getType() == WorkerTask.WORKERTASK_SPIDERTASK ) { 110 fetchTasks.add(task); 111 } else { 112 thinkerTasks.add(task); 113 } 114 } 115 116 120 public synchronized void flagDone(WorkerTask task) { 121 if (task.getType() == WorkerTask.WORKERTASK_THINKERTASK ) { 122 assignedThinkerTasks.remove(task); 123 thinkerTasksDone++; 124 }else{ 125 assignedSpiderTasks.remove(task); 126 spiderTasksDone++; 127 } 128 } 129 130 public synchronized WorkerTask getThinkerTask() throws TaskAssignmentException { 131 if (thinkerTasks.size() > 0) { 132 WorkerTask task = (WorkerTask) thinkerTasks.remove(0); 133 assignedThinkerTasks.add(task); 134 return task; 135 } 136 if (allTasksDone()) { 137 throw new SpideringDoneException(); 138 } else { 139 throw new NoSuitableItemFoundException(); 140 } 141 } 142 143 149 public synchronized WorkerTask getFethTask() throws TaskAssignmentException { 150 if (fetchTasks.size() > 0) { 151 WorkerTask task = (WorkerTask) fetchTasks.remove(0); 152 assignedSpiderTasks.add(task); 153 return task; 154 } 155 if (allTasksDone()) { 156 throw new SpideringDoneException(); 157 } else { 158 throw new NoSuitableItemFoundException(); 159 } 160 } 161 162 163 169 public synchronized boolean allTasksDone() { 170 return (fetchTasks.size() == 0 && 171 thinkerTasks.size() == 0 && 172 assignedSpiderTasks.size() == 0 && 173 assignedThinkerTasks.size() == 0 && 174 blocked.size() == 0 ); 175 } 176 177 198 } 199 | Popular Tags |