Java EE 6 - 有状态REST作为有状态会话bean [英] Java EE 6 - Stateful REST as Stateful session bean

查看:230
本文介绍了Java EE 6 - 有状态REST作为有状态会话bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建需要有状态的REST Web服务。请考虑以下情况:

I am creating REST web service that need to be stateful. Consider following situation:


  1. Web服务执行困难且耗时的计算并返回非常大的结果。因此,此服务仅返回结果数,并且整个结果将保存在有状态bean中的服务器上。

  2. 当结果存在时。客户可以要求提供现有结果的子集。

我试图通过 @Stateful 会话bean但它仍然像 @Stateless 。现在我想知道它是否可能,因为客户端不接受任何Cookie,因此服务器无法识别它。

I am trying to do this via @Stateful session bean but it still acts like @Stateless. Now I am wondering wether it is even possible, because Client is not accepting any Cookie so server cannot identify it.

是否可以通过REST接收有状态bean?

Is it possible to have Stateful bean accesed via REST?

代码示例:

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

}

我是什么期望是,当我将此 / atom-count / 方法调用两次时,它应该以404响应。

What I expect is, when I call this /atom-count/ method twice, it should response with 404.

推荐答案

您应该使用 @SessionScoped 注释您的资源类,以告诉JAX-RS创建具有会话生存期的请求对象,否则默认为 @RequestScoped

You should annotate your resource class with @SessionScoped in order to tell JAX-RS to create request objects with session lifetime, otherwise the default is @RequestScoped.

这篇关于Java EE 6 - 有状态REST作为有状态会话bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆