Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interrupt thread in sleep and waitNewUrl when InterruptException is catched #1004

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ protected void sleep(int time) {
Thread.sleep(time);
} catch (InterruptedException e) {
logger.error("Thread interrupted when sleep",e);
//restore interrupted thread
Thread.currentThread().interrupt();
}
}

Expand Down Expand Up @@ -564,6 +566,7 @@ private void waitNewUrl() {
newUrlCondition.await(emptySleepTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logger.warn("waitNewUrl - interrupted, error {}", e);
Thread.currentThread().interrupt();
} finally {
newUrlLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
public class FilePersistentBase {

protected String path;

public FilePersistentBase() {
setPath("/data/webmagic/");
}

public FilePersistentBase(String path) {
setPath(path);
}

public static String PATH_SEPERATOR = "/";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public class FilePageModelPipeline extends FilePersistentBase implements PageMod
* new JsonFilePageModelPipeline with default path "/data/webmagic/"
*/
public FilePageModelPipeline() {
setPath("/data/webmagic/");
super();
}

public FilePageModelPipeline(String path) {
setPath(path);
super(path);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class JsonFilePageModelPipeline extends FilePersistentBase implements Pag
* new JsonFilePageModelPipeline with default path "/data/webmagic/"
*/
public JsonFilePageModelPipeline() {
setPath("/data/webmagic/");
super();
}

public JsonFilePageModelPipeline(String path) {
setPath(path);
super(path);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public abstract class IPUtils {

public static String getFirstNoLoopbackIPAddresses() throws SocketException {
public static String getFirstNoLoopbackIPAddresses() throws SocketException, NullPointerException{

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

Expand Down