Commit 35288787 authored by yihua.huang's avatar yihua.huang

remove shutdown call

parent a3f9ad19
...@@ -74,6 +74,8 @@ public class Spider implements Runnable, Task { ...@@ -74,6 +74,8 @@ public class Spider implements Runnable, Task {
protected AtomicInteger stat = new AtomicInteger(STAT_INIT); protected AtomicInteger stat = new AtomicInteger(STAT_INIT);
protected boolean exitWhenComplete = false;
protected final static int STAT_INIT = 0; protected final static int STAT_INIT = 0;
protected final static int STAT_RUNNING = 1; protected final static int STAT_RUNNING = 1;
...@@ -240,7 +242,7 @@ public class Spider implements Runnable, Task { ...@@ -240,7 +242,7 @@ public class Spider implements Runnable, Task {
while (!Thread.currentThread().isInterrupted() && stat.get() == STAT_RUNNING) { while (!Thread.currentThread().isInterrupted() && stat.get() == STAT_RUNNING) {
Request request = scheduler.poll(this); Request request = scheduler.poll(this);
if (request == null) { if (request == null) {
if (threadAlive.get() == 0) { if (threadAlive.get() == 0 && exitWhenComplete) {
break; break;
} }
// when no request found but some thread is alive, sleep a // when no request found but some thread is alive, sleep a
...@@ -258,7 +260,7 @@ public class Spider implements Runnable, Task { ...@@ -258,7 +260,7 @@ public class Spider implements Runnable, Task {
try { try {
processRequest(requestFinal); processRequest(requestFinal);
} catch (Exception e) { } catch (Exception e) {
logger.error("download "+requestFinal+" error",e); logger.error("download " + requestFinal + " error", e);
} finally { } finally {
threadAlive.decrementAndGet(); threadAlive.decrementAndGet();
} }
...@@ -372,20 +374,12 @@ public class Spider implements Runnable, Task { ...@@ -372,20 +374,12 @@ public class Spider implements Runnable, Task {
public void stop() { public void stop() {
if (stat.compareAndSet(STAT_RUNNING, STAT_STOPPED)) { if (stat.compareAndSet(STAT_RUNNING, STAT_STOPPED)) {
if (executorService != null) {
executorService.shutdown();
}
logger.info("Spider " + getUUID() + " stop success!"); logger.info("Spider " + getUUID() + " stop success!");
} else { } else {
logger.info("Spider " + getUUID() + " stop fail!"); logger.info("Spider " + getUUID() + " stop fail!");
} }
} }
public void stopAndDestroy() {
stop();
destroy();
}
/** /**
* start with more than one threads * start with more than one threads
* *
...@@ -413,6 +407,23 @@ public class Spider implements Runnable, Task { ...@@ -413,6 +407,23 @@ public class Spider implements Runnable, Task {
EnvironmentUtil.setUseXsoup(false); EnvironmentUtil.setUseXsoup(false);
} }
public boolean isExitWhenComplete() {
return exitWhenComplete;
}
/**
* Exit when complete. <br/>
* True: exit when all url of the site is downloaded. <br/>
* False: not exit until call stop manually.<br/>
*
* @param exitWhenComplete
* @return
*/
public Spider setExitWhenComplete(boolean exitWhenComplete) {
this.exitWhenComplete = exitWhenComplete;
return this;
}
@Override @Override
public String getUUID() { public String getUUID() {
if (uuid != null) { if (uuid != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment