I am trying to make a web scraper that parses specific URL indefinitely with some delay in between.
I have a method genericMonitor
that accepts the url to parse and a callback to return a result.
This method is called repeatedly using SheduledExecutorService
. The code is below:
ScheduledFuture<?> task = service.scheduleWithFixedDelay(() -> {
try {
genericMonitor(percent, url, idSupplier, callback);
} catch (IOException connectException) {
logger.error("Error in generic", connectException);
}
}, 0, delay, TimeUnit.MINUTES);
tasks.put(link, task);
The tasks
map holds all currently running tasks.
I make http requests using OkHttp
Java library.
When I try to cancel the task using task.cancel
I get InterruptedIOException
:
java.io.InterruptedIOException: interrupted
at okio.Timeout.throwIfReached(Timeout.java:146)
at okio.Okio$1.write(Okio.java:76)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:180)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:179)
at okio.RealBufferedSink.writeByte(RealBufferedSink.java:125)
at okhttp3.internal.http2.Http2Writer.writeMedium(Http2Writer.java:265)
at okhttp3.internal.http2.Http2Writer.frameHeader(Http2Writer.java:253)
at okhttp3.internal.http2.Http2Writer.headers(Http2Writer.java:289)
at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:253)
at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:225)
at okhttp3.internal.http2.Http2Codec.writeRequestHeaders(Http2Codec.java:117)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:54)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:264)
at okhttp3.RealCall.execute(RealCall.java:93)
at org.dk.Bot.callRemoteWithProxy(Bot.java:82)
at org.dk.Bot.genericMonitor(Bot.java:182)
at org.dk.Bot.lambda$processCommand$1(Bot.java:129)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:832)
I am not sure what's the problem