Groovy: send request to url ignoring result

In groovy, is there a way to send a request to a URL ignoring the response? The main task is to send more requests to the server in a shorter period of time.

As a result, it doesn’t matter to me as soon as the request is sent, I don’t want the script to wait for a response before continuing.

Here is my current script:

(1..50).each { element-> def url = "http://someUrl" url.toURL().text } 

In this code, the text method should load the whole answer, which is really uninteresting to me. The important part is that the request is sent, and waiting for a response does not matter.

Is there a similar send method? (along the lines ..

 url.toURL().send 

Alternatively, is there a “groovy” way that I can speed up with GPARS to run the loop in parallel?

+4
source share
1 answer

To just send the url, you can simply use the withInputStream or withReader to send the request without reading the text. This will simply create a handler for reading the incoming text, which will be immediately closed.

As for GPars, you can simply use a combination of withPool and callAysnc to create a thread pool to run queries simultaneously. For instance:

 @Grab(group='org.codehaus.gpars', module='gpars', version='0.12') import static groovyx.gpars.GParsExecutorsPool.withPool withPool(50) { 50.times { Closure callUrl = {"http://google.com".toURL().withReader {}} callUrl.callAsync(); } } 

However, if you are not interested in the number of threads, you can simply create your own threads without GPars. For instance:

 50.times { Closure callUrl = {"http://google.com".toURL().withReader {}} Thread.start callUrl } 
+6
source

All Articles