Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kamil-adam/f02ea7dde043de4a6ab4a17d47d6ae68 to your computer and use it in GitHub Desktop.
Save kamil-adam/f02ea7dde043de4a6ab4a17d47d6ae68 to your computer and use it in GitHub Desktop.
Dropwizard AsyncResponse usage
package com.xyz.dw.resource;
import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.SettableFuture;
import com.xyz.dw.model.Greeting;
import org.glassfish.jersey.server.ManagedAsync;
import rx.Observable;
import rx.schedulers.Schedulers;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
private final ExecutorService executorService;
public HelloResource(String template, String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
this.executorService = Executors.newFixedThreadPool(100);
}
@GET
@Timed
public Greeting sayHello(@QueryParam("name") Optional<String> name){
final String value = String.format(template, name.or(defaultName));
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Greeting(counter.incrementAndGet(), value);
}
@GET
@Path("/async")
@Timed
@ManagedAsync
public void asyncHello(@QueryParam("name") Optional<String> name, @Suspended final AsyncResponse asyncResponse) throws InterruptedException {
final String value = String.format(template, name.or(defaultName));
Greeting greeting = new Greeting(counter.incrementAndGet(), value);
Thread.sleep(50);
asyncResponse.resume(greeting);
}
@GET
@Path("/async_observable")
@Timed
public void asyncObservableHello(@QueryParam("name") Optional<String> name, @Suspended final AsyncResponse asyncResponse){
Observable<Object> observable = Observable.create(s -> {
final String value = String.format(template, name.or(defaultName));
Greeting greeting = new Greeting(counter.incrementAndGet(), value);
s.onNext(greeting);
s.onCompleted();
}).delay(50, TimeUnit.MILLISECONDS);
observable.subscribeOn(Schedulers.from(executorService));
observable.subscribe(greeting -> asyncResponse.resume(greeting));
}
@GET
@Path("/async_future")
@Timed
public void asyncFutureHello(@QueryParam("name") Optional<String> name, @Suspended final AsyncResponse asyncResponse){
CompletableFuture<Greeting> completableFuture = new CompletableFuture<>();
executorService.submit(() -> {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
final String value = String.format(template, name.or(defaultName));
Greeting greeting = new Greeting(counter.incrementAndGet(), value);
completableFuture.complete(greeting);
});
completableFuture.thenAcceptAsync(greeting -> asyncResponse.resume(greeting));
}
@GET
@Path("/sync_future")
@Timed
public Greeting syncFutureHello(@QueryParam("name") Optional<String> name) throws ExecutionException, InterruptedException {
SettableFuture<Greeting> settableFuture = SettableFuture.create();
executorService.submit(() -> {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
final String value = String.format(template, name.or(defaultName));
Greeting greeting = new Greeting(counter.incrementAndGet(), value);
settableFuture.set(greeting);
});
Greeting greeting = settableFuture.get();
return greeting;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment