Java EE 6 - Stateful REST as a session state bean

I am creating a REST web service that needs to be functional. Consider the following situation:

  • The web service performs complex and time consuming calculations and returns a very large result. So this service only returns the count of results and the whole result is stored on the server in a bean state.
  • When the result exists. The client can request a subset of the existing results.

I am trying to do this through a @Stateful

session bean, but it still acts like @Stateless

. Now I am wondering if this is possible because the Client does not accept Cookie, so the server cannot identify it.

Is it possible to use a Stateful bean over REST?

Sample 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<SimilarityResult> savedSimilarityResults = new ArrayList<SimilarityResult>();

    @POST
    @Path("/atom-count/")
    public List<SimilarityResult> atomCountSimilarity(JAXBElement<SimilarityRequestXML> sr) {

        try {
            if (this.savedSimilarityResults.isEmpty()) {

                List<SimilarityResult> 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());
        }
    }

}

      

What I expect, when I call this method /atom-count/

twice, it should respond with 404.

+3


source to share


1 answer


You must annotate your resource class with @SessionScoped

to tell JAX-RS to create request objects with a session lifetime, otherwise the default is @RequestScoped

.



+6


source







All Articles