Skip to content

Commit

Permalink
Updated Crawler.java
Browse files Browse the repository at this point in the history
  • Loading branch information
lwj5 committed Sep 1, 2020
1 parent db2ac43 commit 6cb6ade
Showing 1 changed file with 50 additions and 24 deletions.
74 changes: 50 additions & 24 deletions src/main/java/ai/preferred/venom/Crawler.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,7 @@ private void run() {
}
});

final Callback callback = new Callback() {
@Override
public void completed(final @NotNull Request request, final @NotNull Response response) {
LOGGER.debug("Completed received for job {} - {}.", Integer.toHexString(job.hashCode()),
job.getRequest().getUrl());
completableResponseFuture.complete(response);
}

@Override
public void failed(final @NotNull Request request, final @NotNull Exception ex) {
LOGGER.debug("Failed received for job {} - {}.", Integer.toHexString(job.hashCode()),
job.getRequest().getUrl());
completableResponseFuture.completeExceptionally(ex);
}

@Override
public void cancelled(final @NotNull Request request) {
LOGGER.debug("Cancelled received for job {} - {}.", Integer.toHexString(job.hashCode()),
job.getRequest().getUrl());
completableResponseFuture.cancel(true);
}
};

fetcher.fetch(crawlerRequest, callback);
fetcher.fetch(crawlerRequest, new CompletableCallback(job, completableResponseFuture));
});
} catch (final InterruptedException e) {
LOGGER.debug("({}) producer thread interrupted.", crawlerThread.getName(), e);
Expand Down Expand Up @@ -517,6 +494,55 @@ public void close() throws Exception {
}
}

/**
* A callback that utilises CompletableFuture.
*/
private static final class CompletableCallback implements Callback {

/**
* The job this callback is for.
*/
private final Job job;

/**
* The CompletableFuture to call upon response.
*/
private final CompletableFuture<Response> completableResponseFuture;

/**
* Constructs an instance of CompletableCallback.
*
* @param job The job this callback is for.
* @param completableResponseFuture The CompletableFuture to call upon response.
*/
private CompletableCallback(final Job job, final CompletableFuture<Response> completableResponseFuture) {
this.job = job;
this.completableResponseFuture = completableResponseFuture;
}

@Override
public void completed(final @NotNull Request request, final @NotNull Response response) {
LOGGER.debug("Completed received for job {} - {}.", Integer.toHexString(job.hashCode()),
job.getRequest().getUrl());
completableResponseFuture.complete(response);
}

@Override
public void failed(final @NotNull Request request, final @NotNull Exception ex) {
LOGGER.debug("Failed received for job {} - {}.", Integer.toHexString(job.hashCode()),
job.getRequest().getUrl());
completableResponseFuture.completeExceptionally(ex);
}

@Override
public void cancelled(final @NotNull Request request) {
LOGGER.debug("Cancelled received for job {} - {}.", Integer.toHexString(job.hashCode()),
job.getRequest().getUrl());
completableResponseFuture.cancel(true);
}

}

/**
* A builder for crawler class.
*/
Expand Down

0 comments on commit 6cb6ade

Please sign in to comment.