Skip to content

Commit 6cb6ade

Browse files
committed
Updated Crawler.java
1 parent db2ac43 commit 6cb6ade

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

src/main/java/ai/preferred/venom/Crawler.java

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -351,30 +351,7 @@ private void run() {
351351
}
352352
});
353353

354-
final Callback callback = new Callback() {
355-
@Override
356-
public void completed(final @NotNull Request request, final @NotNull Response response) {
357-
LOGGER.debug("Completed received for job {} - {}.", Integer.toHexString(job.hashCode()),
358-
job.getRequest().getUrl());
359-
completableResponseFuture.complete(response);
360-
}
361-
362-
@Override
363-
public void failed(final @NotNull Request request, final @NotNull Exception ex) {
364-
LOGGER.debug("Failed received for job {} - {}.", Integer.toHexString(job.hashCode()),
365-
job.getRequest().getUrl());
366-
completableResponseFuture.completeExceptionally(ex);
367-
}
368-
369-
@Override
370-
public void cancelled(final @NotNull Request request) {
371-
LOGGER.debug("Cancelled received for job {} - {}.", Integer.toHexString(job.hashCode()),
372-
job.getRequest().getUrl());
373-
completableResponseFuture.cancel(true);
374-
}
375-
};
376-
377-
fetcher.fetch(crawlerRequest, callback);
354+
fetcher.fetch(crawlerRequest, new CompletableCallback(job, completableResponseFuture));
378355
});
379356
} catch (final InterruptedException e) {
380357
LOGGER.debug("({}) producer thread interrupted.", crawlerThread.getName(), e);
@@ -517,6 +494,55 @@ public void close() throws Exception {
517494
}
518495
}
519496

497+
/**
498+
* A callback that utilises CompletableFuture.
499+
*/
500+
private static final class CompletableCallback implements Callback {
501+
502+
/**
503+
* The job this callback is for.
504+
*/
505+
private final Job job;
506+
507+
/**
508+
* The CompletableFuture to call upon response.
509+
*/
510+
private final CompletableFuture<Response> completableResponseFuture;
511+
512+
/**
513+
* Constructs an instance of CompletableCallback.
514+
*
515+
* @param job The job this callback is for.
516+
* @param completableResponseFuture The CompletableFuture to call upon response.
517+
*/
518+
private CompletableCallback(final Job job, final CompletableFuture<Response> completableResponseFuture) {
519+
this.job = job;
520+
this.completableResponseFuture = completableResponseFuture;
521+
}
522+
523+
@Override
524+
public void completed(final @NotNull Request request, final @NotNull Response response) {
525+
LOGGER.debug("Completed received for job {} - {}.", Integer.toHexString(job.hashCode()),
526+
job.getRequest().getUrl());
527+
completableResponseFuture.complete(response);
528+
}
529+
530+
@Override
531+
public void failed(final @NotNull Request request, final @NotNull Exception ex) {
532+
LOGGER.debug("Failed received for job {} - {}.", Integer.toHexString(job.hashCode()),
533+
job.getRequest().getUrl());
534+
completableResponseFuture.completeExceptionally(ex);
535+
}
536+
537+
@Override
538+
public void cancelled(final @NotNull Request request) {
539+
LOGGER.debug("Cancelled received for job {} - {}.", Integer.toHexString(job.hashCode()),
540+
job.getRequest().getUrl());
541+
completableResponseFuture.cancel(true);
542+
}
543+
544+
}
545+
520546
/**
521547
* A builder for crawler class.
522548
*/

0 commit comments

Comments
 (0)