title: Async in Java Jersey
body: <p>I have some code that:</p>
  
  <p>1- Received some data through a REST call (POST);</p>
  
  <p>2- Performed some logic according to that data;</p>
  
  <p>3- Returned the result.</p>
  
  <p>For the sake of this question let's pretend it was a simple calculator webapi that allowed its clients to perform additions and subtractions. It looked like this:</p>
  
  <pre><code>@Path("/calculator")
  public class Calculator {
  
      @POST
      @Path("addition")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public Response add(String request){
          //Getting A and B from request
          ...
          //Calculating result
          int res = a + b;
          //Creating response payload
          JSONObject res = new JSONObject.put("result",res);
  
          return Response.status(Response.Status.OK).entity(res.toString()).build();
      }
  
      @POST
      @Path("subtract")
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public Response sub(String request){
          //Getting A and B from request
          ...
          //Calculating result
          int res = a - b;
          //Creating response payload
          JSONObject res = new JSONObject.put("result",res);
  
          return Response.status(Response.Status.OK).entity(res.toString()).build();
      }
  }
  </code></pre>
  
  <p><strong>Everything was fine until i realized that i couldn't perform more that one calculation in parallel because all the requests accessed a unique resource that can only be used by one of them at a time.</strong> </p>
  
  <p>So, for the sake of this example, let's pretend we have a single calculator and that all requests' computations must be performed by that same calculator processor. </p>
  
  <p>In my mind i think i would need something like a "CalculatorProcessor" that received requests from all the calculator webapi clients that:</p>
  
  <p>1- Receives request;</p>
  
  <p>2- Queues request;</p>
  
  <p>3- Dequeues request;</p>
  
  <p>4- Performs calculation;</p>
  
  <p>5- Returns result using callback.</p>
  
  <p><strong>This is something that is kinda trivial for me in native Java, but i don't have a single clue on how i should this in a Java Jersey's context.
  For instance... 
  How can i get back to the Calculator.add() and Calculator.sub() methods so i can send the http request response?
  Can someone please enlighten me please?</strong></p>
  
  <p>Here's my java implementation for such a component:</p>
  
  <pre><code>import java.util.concurrent.ConcurrentLinkedQueue;
  
  //IMPLEMENTS SINGLETON PATTERN
  public class Calculator {
  
  private static Calculator instance = null;
  private ConcurrentLinkedQueue&lt;Request&gt; queue = null;
  private Runnable processor = null;
  
  //PRIVATE CONSTRUCTOR
  private Calculator() {
      queue = new ConcurrentLinkedQueue&lt;&gt;();
  }
  
  //GET CALCULATOR INSTANCE
  static public Calculator getInstance() {
      if (instance == null) {
          instance = new Calculator();
      }
      return instance;
  }
  
  //REQUEST COMPUTATION
  public synchronized void requestComputation(CalculatorCallback c, SupportedOperations o, int a, int b) {
      //Adds request to queue
      queue.add(new Request(c, o, a, b));
  
      //Checks if there's an active processor
      if (processor == null) {
          //Launches a new processor if there isn't
          Runnable p = new CalculatorProcessor(queue);
          new Thread(p).start();
      }
  }
  
  //CALLBACK INTERFACE
  public interface CalculatorCallback {
      void computationReady(int result);
  }
  
  
  //SUPPORTED OPERATIONS ENUMERATION
  protected enum SupportedOperations {
      ADDITION, SUBTRACTION;
  }
  
  //CLASS THAT REPRESENTS A REQUEST
  private class Request {
  
      final SupportedOperations operation;
      final CalculatorCallback callback;
      final int a;
      final int b;
  
      public Request(CalculatorCallback c, SupportedOperations operation, int a, int b) {
          this.callback = c;
          this.operation = operation;
          this.a = a;
          this.b = b;
      }
  }
  
  //CALCULATOR PROCESSOR THREAD
  class CalculatorProcessor implements Runnable {
  
      final private ConcurrentLinkedQueue&lt;Calculator.Request&gt; queue;
  
      public CalculatorProcessor(ConcurrentLinkedQueue&lt;Calculator.Request&gt; queue) {
          this.queue = queue;
      }
  
      @Override
      public void run() {
          Calculator.Request current;
          int result;
          while (!queue.isEmpty()) {
              //Gets head
              current = queue.poll();
  
              if (current.operation == Calculator.SupportedOperations.ADDITION) {
                  result = current.a + current.b;
              } else if (current.operation == Calculator.SupportedOperations.SUBTRACTION) {
                  result = current.a - current.b;
              } else {
                  throw new UnsupportedOperationException();
              }
              //Calls back the requester
              current.callback.computationReady(result);
          }
      }
  }
  }
  </code></pre>
  
  <p>Here's the CalculatorClient code:</p>
  
  <pre><code>public class CalculatorClient implements Calculator.CalculatorCallback {
  
  public static void main(String[] args) {
  
      CalculatorClient client = new CalculatorClient();
      Random random = new Random();
      int a, b;
  
      for (int i = 0; i &lt; 1000; i++) {
          a = random.nextInt(Integer.MAX_VALUE/2);
          b = random.nextInt(Integer.MAX_VALUE/2);
          System.out.println("Requesting "+a+" + "+b+"...");
          Calculator.getInstance().requestComputation(client, Calculator.SupportedOperations.ADDITION,a,b);
      }
  }
  
  @Override
  public void computationReady(int result) {
      System.out.println("Result is: "+result);
  }
  </code></pre>
  
  <p>}</p>