1

我在这里尝试以下操作:我有一个调用远程 EJB 的 Web 应用程序:

@WebServlet("/token")
@SuppressWarnings("serial")
public class TokenServlet extends HttpServlet {

@EJB
private TokenService tokenService;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    Token token = tokenService.getToken();

    tokenService.setToken(token);

}

}

服务ejb:

@Stateless
@Remote
public class TokenServiceImpl implements TokenService {

private Token token = new Token();

@Override
public Token getToken() {
    System.out.println("class: " + TokenServiceImpl.class
            + ", method: getToken() - " + "token value: '"
            + token.getToken() + "' reference: " + this.token);

    return this.token;
}

@Override
public void setToken(Token token) {
    boolean referenceEquality = this.token == token;
    boolean valueEquality = this.token.equals(token);
    System.out.println("class " + TokenServiceImpl.class
            + ", methos: setToken(Token token)");
    System.out.println("test for reference equality: " + " " + this.token
            + " == " + token + " --> " + referenceEquality);
    System.out.println("test for value equality: " + " " + this.token
            + " equals " + token + " --> " + valueEquality);

    this.token = token;
}

webproject(.war) 和 ejbproject(.ear) 分别使用 glassfish-application.xml 条目进行部署:

<!DOCTYPE glassfish-application PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Java EE Application 6.0//EN"
"http://glassfish.org/dtds/glassfish-application_6_0-1.dtd">
<glassfish-application>
   <pass-by-reference>true</pass-by-reference>
</glassfish-application>

class: class TokenServiceImpl, method: getToken() - token value: '     
'default token value' reference: com.ebcont.domain.impl.Token@7fef01a8|#]

class TokenServiceImpl, method: setToken(Token token)|#]
test for reference equality:  Token@7fef01a8 == Token@2f4d4e75 --> false|#]
test for value equality:  Token@7fef01a8 equals Token@2f4d4e75 --> false|#]

是我出了什么问题,还是不能对远程 EJB 使用引用调用?

4

1 回答 1

0

您的无状态会话 Bean 实现存在问题。这是变量的介绍和使用

private Token token = new Token();

根据这里的规范,

当客户端调用无状态 bean 的方法时,bean 的实例变量可能包含特定于该客户端的状态,但仅限于调用期间。当方法完成时,不应保留特定于客户端的状态。

简而言之,对 Session bean 的每次调用很可能会在单独且独立的线程或上下文中运行,这将相应地关联和初始化您的实例变量。

于 2013-07-05T23:24:49.443 回答