Skip to content

Instantly share code, notes, and snippets.

@curtkim
Created October 29, 2017 16:02
Show Gist options
  • Save curtkim/8dc5536f8988cf4141052254eeae400b to your computer and use it in GitHub Desktop.
Save curtkim/8dc5536f8988cf4141052254eeae400b to your computer and use it in GitHub Desktop.
import com.mashape.unirest.http.HttpResponse
import com.mashape.unirest.http.JsonNode
import com.mashape.unirest.http.Unirest
import com.mashape.unirest.http.async.Callback
import com.mashape.unirest.http.exceptions.UnirestException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Future
int count = 100
long firstTime = System.currentTimeMillis()
CountDownLatch responseWaiter = new CountDownLatch(count)
List<String> origins = [
'6,127.108967,37.3344094,-1',
'261,127.108998,37.339129,-1',
'593,127.108705,37.3407883,-1',
'222,127.1096024,37.3435279,-1',
'816,127.1102945,37.3467869,-1',
'472,127.109691,37.3500737,-1',
'247,127.1090828,37.3501526,-1',
'127,127.1088189,37.3504406,-1',
'592,127.1086417,37.3506228,-1',
'16,127.1089396,37.3506562,-1',
'55,127.1045953,37.3382255,-1',
'331,127.107112,37.3482508,-1',
'248,127.1012284,37.3493322,-1',
'916,127.1046536,37.3533746,-1',
'7,127.1051666,37.3534581,-1',
'196,127.1047057,37.3536624,-1',
'1062,127.1059626,37.361944,-1',
'918,127.1058131,37.3625375,-1',
'304,127.1061103,37.363829,-1',
'393,127.1079853,37.3640161,-1',
'574,127.1161483,37.3430064,-1',
'17,127.111618,37.3466978,-1',
'47,127.1113702,37.3482615,-1',
'136,127.1199905,37.3498575,-1',
'297,127.116302,37.3520301,-1',
'778,127.1193534,37.3592367,-1',
'183,127.1192351,37.3594174,-1',
'239,127.1234037,37.3604474,-1',
'350,127.1246447,37.3604698,-1',
'525,127.1161839,37.3631974,-1',
'453,127.1088618,37.3508557,-1',
'923,127.1093978,37.3513726,-1',
'578,127.1091532,37.3526923,-1',
'412,127.1083923,37.3606963,-1',
'205,127.1107383,37.3634516,-1',
'60,127.1084461,37.3675772,-1',
'151,127.1098943,37.3687296,-1',
'206,127.1105707,37.376873,-1',
'387,127.1092721,37.3781774,-1',
'76,127.1090804,37.3784924,-1',
'675,127.1078811,37.3645075,-1',
'317,127.1047861,37.364514,-1',
'419,127.1080659,37.3647238,-1',
'426,127.108225,37.3648439,-1',
'90,127.105618,37.3659899,-1',
'444,127.1078058,37.3661828,-1',
'332,127.1081432,37.3663557,-1',
'702,127.1076574,37.3684761,-1',
'252,127.1048733,37.3684806,-1',
'736,127.108141,37.3685883,-1',
'351,127.1154299,37.3634726,-1',
'443,127.1212633,37.3635405,-1',
'236,127.119415,37.3635878,-1',
'290,127.1199537,37.3636268,-1',
'482,127.1256357,37.3659815,-1',
'173,127.1147536,37.3661109,-1',
'614,127.1145584,37.3685956,-1',
'440,127.1201212,37.3689819,-1',
'564,127.1149355,37.3689871,-1',
'1128,127.1130725,37.3762461,-1',
'278,127.1087977,37.378759,-1',
'250,127.1170897,37.3790667,-1',
'347,127.1071007,37.3799196,-1'
]
(0..count).each { it->
println it
long startTime = System.currentTimeMillis()
Future<HttpResponse<JsonNode>> future = Unirest.get("http://taxi-navi-api.kakao.com/multiSearchRoute")
//.header("accept", "application/json")
.queryString("origins", origins)
.queryString("destination", "127.1080923,37.358493")
.queryString('radius', "7920")
.queryString('limit', "2147483647")
.queryString('route_option', "SHORTEST_REALTIME")
.queryString('road_option', "NONE")
.queryString('guide_option', "LEVEL_1")
.queryString('route_polyline', "false")
.queryString('gate_point', "false")
.queryString('ics', "WGS84")
.queryString('ocs', "WGS84")
.queryString('service', "taxi")
.asJsonAsync(new Callback<JsonNode>() {
public void failed(UnirestException e) {
System.out.println("The request has failed");
}
public void completed(HttpResponse<JsonNode> response) {
int code = response.getStatus()
Map<String, String> headers = response.getHeaders()
JsonNode body = response.getBody()
println Thread.currentThread().getName() + "] " + it + " " + (System.currentTimeMillis() - startTime) + "ms " + body
responseWaiter.countDown()
}
public void cancelled() {
System.out.println("The request has been cancelled");
}
})
}
responseWaiter.await()
println Thread.currentThread().getName() + " " + (System.currentTimeMillis() - firstTime) + "ms "
import java.util.concurrent.Callable
import java.util.concurrent.ExecutionException
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future
class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(1000)
return Thread.currentThread().getName()
}
}
ExecutorService executor = Executors.newFixedThreadPool(10)
List<Future<String>> list = new ArrayList<Future<String>>()
Callable<String> callable = new MyCallable()
for(int i=0; i< 100; i++){
Future<String> future = executor.submit(callable)
list.add(future)
}
for(Future<String> fut : list){
try {
//print the return value of Future, notice the output delay in console
// because Future.get() waits for task to get completed
System.out.println(new Date().toString()+ "::"+fut.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
executor.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment