从请求范围的JAX-RS Web服务调用的无状态EJB3 Bean会累积状态 [英] stateless EJB3 bean invoked from request-scoped JAX-RS web service accumulates state

查看:89
本文介绍了从请求范围的JAX-RS Web服务调用的无状态EJB3 Bean会累积状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:请求范围内的 JAX-RS 服务调用无状态的 EJB3 bean,并且EJB3 bean保留了从客户端连续调用Web服务之间的状态.

I have the following situation: Request-scoped JAX-RS service invokes stateless EJB3 bean and the EJB3 bean retains state between successive invocation of the web service from a client.

代码如下:

@javax.enterprise.context.RequestScoped
@Path("/actions")
public class CounterFrontEnd {

    @EJB
    private ICounterService.ILocal counterService;

    @GET @Produces("application/text;") @Path("/counter")
    public String counter() {
        return String.format("%d ", counterService.incCounter());
    }

无状态EJB3 bean

@Stateless
@Local (ICounterService.ILocal.class)
@Remote(ICounterService.IRemote.class)
public class CounterService implements ICounterService.ILocal, ICounterService.IRemote {

    public int counter = 0;
    @Override
    public int incCounter() {
        return counter++;
    }

然后我使用以下python脚本调用服务:

I then invoke the service with the following python script:

for i in range(100):
  os.system( 'curl  http://somewhere:8080/counter-ws/rest/actions/counter' ) 

令人惊讶的是,输出为:

Surprisingly, the output is:

1 2 3 4 5 ...

推荐答案

这是因为每次都会为您提供相同Bean实例的容器(发生),而不是您应该依赖的容器.

This is because the container (happens) to give you the same bean-instance every time, not something you should rely on.

无状态会话bean不应像示例中那样依赖实例变量.

Stateless session-beans should not rely on instance-variables like in your example.

摘自java ee教程: http://docs.oracle.com /javaee/6/tutorial/doc/javaeetutorial6.pdf

From java ee tutorial: http://docs.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf

无状态会话Bean

无状态会话Bean不会与 客户端.当客户端调用无状态Bean的方法时, bean的实例变量可能包含特定于该客户端的状态 但仅在调用期间.当方法是 在这种情况下,不应保留客户特定的状态.客户可以 但是,请在无状态池中更改实例变量的状态 Bean,并且此状态将保留到下一次调用 合并的无状态bean.除方法调用期间外,所有实例 无状态Bean的等效项允许EJB容器执行以下操作: 将实例分配给任何客户端.也就是说,无状态的状态 会话Bean应该适用于所有客户端.

A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state speciic to that client but only for the duration of the invocation. When the method is inished, the client-speciic state should not be retained. Clients may, however, change the state of instance variables in pooled stateless beans, and this state is held over to the next invocation of the pooled stateless bean. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. That is, the state of a stateless session bean should apply across all clients.

这篇关于从请求范围的JAX-RS Web服务调用的无状态EJB3 Bean会累积状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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