title: Java EE 6 - Stateful REST as Stateful session bean
body: <p>I am creating REST web service that need to be stateful. Consider following situation:</p>
  
  <ol>
  <li>Web service performs difficult and time consuming computations and returns very large result. So only number of results is returned by this service and whole result is saved on the server in stateful bean.</li>
  <li>When the result exists. Client can ask for a subset of existing results.</li>
  </ol>
  
  <p>I am trying to do this via <code>@Stateful</code> session bean but it still acts like <code>@Stateless</code>. Now I am wondering wether it is even possible, because Client is not accepting any Cookie so server cannot identify it.</p>
  
  <p>Is it possible to have Stateful bean accesed via REST? </p>
  
  <p>Code sample:</p>
  
  <pre><code>@Path("/similarity/")
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  @Stateful
  @StatefulTimeout(600000) // 10 minutes
  public class SimilarityResource {
  
      private List&lt;SimilarityResult&gt; savedSimilarityResults = new ArrayList&lt;SimilarityResult&gt;();
  
      @POST
      @Path("/atom-count/")
      public List&lt;SimilarityResult&gt; atomCountSimilarity(JAXBElement&lt;SimilarityRequestXML&gt; sr) {
  
          try {
              if (this.savedSimilarityResults.isEmpty()) {
  
                  List&lt;SimilarityResult&gt; similarityResults = acs.findAllSimilar(); // Time consuming
  
                  this.savedSimilarityResults = similarityResults; // Save results
                  return similarityResults;
              } else {
                  CompoundResponse cr = new CompoundResponse("Hureeey stateful bean works!.", 404);
                  throw new WebApplicationException(cr.buildResponse());
              }
  
          } catch (CompoundSearchException e) {
              CompoundResponse cr = new CompoundResponse(500, e);
              throw new WebApplicationException(cr.buildResponse());
          }
      }
  
  }
  </code></pre>
  
  <p>What I expect is, when I call this <code>/atom-count/</code> method twice, it should response with 404.</p>