Skip to content

Instantly share code, notes, and snippets.

@igorzg
Created June 18, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorzg/fb859cdc7c8525ddebd7cc12258117f9 to your computer and use it in GitHub Desktop.
Save igorzg/fb859cdc7c8525ddebd7cc12258117f9 to your computer and use it in GitHub Desktop.
Jaxrs async marshaling feature reqeust
@Path("/{partner}/sync")
@Produces(MediaType.APPLICATION_JSON)
public class SyncResource {
@PathParam("partner") String partner;
@Inject SyncService syncService;
/**
* IMPLEMENTATION PROPOSAL
*/
@GET
@Path("{model}")
public CompletableFuture<SyncStatus> asyncReqeust(@PathParam("model") String model) {
return syncService.sync(model, partner)
.exceptionally((Throwable e) ->
Response.status(500).entity(SyncStatus.error(partner, "Sync successfully finished", e)).build()
);
}
/**
* CURRENT IMPLEMENTATION
* Sync one collection
*
* @param model model name
* @param asyncResponse AsyncResponse
*/
@GET
@Path("{model}")
public void syncOne(@PathParam("model") String model, @Suspended AsyncResponse asyncResponse) {
registerTimeoutHandler(asyncResponse);
syncService.sync(model, partner)
.thenApply(data -> asyncResponse.resume(data))
.exceptionally((Throwable e) -> {
return asyncResponse.resume(
Response.status(500).entity(SyncStatus.error(partner, "Sync successfully finished", e)).build()
);
});
}
}
@igorzg
Copy link
Author

igorzg commented Jun 18, 2017

Or it would be in case of global exception handler:

    @GET
    @Path("{model}")
    public CompletableFuture<SyncStatus> asyncReqeust(@PathParam("model") String model) {
        return syncService.sync(model, partner);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment