Last active
June 20, 2020 09:25
-
-
Save likhoman/455b752c50c06790286ee66ed1ceffd2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.HttpVersion; | |
import org.apache.http.client.fluent.Request; | |
import org.apache.http.entity.ContentType; | |
import java.io.IOException; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ThreadLocalRandom; | |
public class HttpClientDemo { | |
public static void main(String[] args) throws InterruptedException { | |
final var executorService = Executors.newFixedThreadPool(5); | |
final var random = ThreadLocalRandom.current(); | |
for (int i = 0; i < 5; i++) { | |
executorService.execute(() -> { | |
final String string; | |
try { | |
final String name = Thread.currentThread().getName(); | |
System.out.println("Stasrt thread " + name); | |
string = Request.Post(String.format("http://localhost:%s/piu", 8080)) | |
.version(HttpVersion.HTTP_1_0) | |
.bodyString(name, ContentType.TEXT_PLAIN) | |
.execute() | |
.handleResponse(response -> { | |
final var entity = response.getEntity(); | |
final var inputStream = entity.getContent(); | |
return new String(inputStream.readAllBytes()); | |
}); | |
System.out.println(string); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
}); | |
Thread.sleep(random.nextInt(1_000, 10_000)); | |
} | |
executorService.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment