Last active
August 29, 2015 14:15
-
-
Save murayama333/c1fe600d406ae8bf3e04 to your computer and use it in GitHub Desktop.
This file contains 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
package com.example.app; | |
import java.util.Random; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.container.AsyncResponse; | |
import javax.ws.rs.container.Suspended; | |
import javax.ws.rs.core.Response; | |
@Path("/async") | |
public class AsyncResource { | |
@GET | |
public void asyncGet(@Suspended AsyncResponse asyncResponse) { | |
new Thread(() -> { | |
int sleepMillis = sleep(); | |
log("runnable thread end.(" + sleepMillis + ")"); | |
Response response = Response.ok("Success (" + sleepMillis + ")").build(); | |
asyncResponse.resume(response); | |
}).start(); | |
log("server thread end."); | |
} | |
private int sleep() { | |
try { | |
int millis = new Random().nextInt(5) * 1000; | |
Thread.sleep(millis); | |
return millis; | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private void log(String message) { | |
System.out.println(message | |
+ " / " + Thread.currentThread().getId() | |
+ " / " + Thread.currentThread().getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment