Stateless agindo como stateful

1 resposta
M

o bean staeless ta agindo como stateful, pq sera os arquivos relevantes seguem a seguir:

HELLOBEAN.JAVA
package examples;

import javax.ejb.SessionContext;

/**

  • Demonstration stateless session bean.
    */
    public class HelloBean implements javax.ejb.SessionBean {
    int contador;
    SessionContext sc;
    public void setSessionContext(SessionContext ctx) {
    sc = ctx;
    }
    public void ejbCreate() {
    System.out.println(“ejbCreate()”);
    contador = 0;
    }
public void ejbRemove() {

System.out.println(ejbRemove());

}
public void ejbActivate() {

System.out.println(ejbActivate());

}
public void ejbPassivate() {

System.out.println(ejbPassivate());

}
//

// Business methods

//

public int hello() {

return ++contador;

}

}
CONTADORBEAN.JAVA
/*

  • Created on 29/11/2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */
    package examples;

import java.rmi.RemoteException;

import javax.ejb.EJBException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

/**

  • @author monster
  • TODO To change the template for this generated type comment go to
  • Window - Preferences - Java - Code Style - Code Templates
    */
    public class ContadorBean implements SessionBean{
    int contador;
    SessionContext sc;

/** O método ejbCreate() será chamado quando for solicitado

  • o método create na interface Home.
    **/
public void ejbCreate() {

contador = 0;

}

/** O método ejbCreate(int) será chamado quando for solicitado

  • o método create(int) na interface Home.
    **/
public void ejbCreate(int i) {

contador = i;

}

/* (non-Javadoc)

  • @see javax.ejb.SessionBean#ejbActivate()
    */
    public void ejbActivate() throws EJBException, RemoteException {
    System.out.println(“Ativando componente”);

}

/* (non-Javadoc)

  • @see javax.ejb.SessionBean#ejbPassivate()
    */
    public void ejbPassivate() throws EJBException, RemoteException {
    System.out.println(“desAtivando componente”);

}

/* (non-Javadoc)

  • @see javax.ejb.SessionBean#ejbRemove()
    */
    public void ejbRemove() throws EJBException, RemoteException {
    System.out.println(“removendo componente”);

}

/* (non-Javadoc)

  • @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
    /
    public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException {
    System.out.println(“setando o contexto”);
    sc = sessionContext;
    }
    /
    Implementação do método da interface Remota */
    public int getCount() {

return ++contador;

}

}

EJB-JAR.XML

<!DOCTYPE ejb-jar PUBLIC

“-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN”

“<a href="http://java.sun.com/dtd/ejb-jar_2_0.dtd">http://java.sun.com/dtd/ejb-jar_2_0.dtd</a>”>

<ejb-jar>

<enterprise-beans>

<session>

<ejb-name>Hello</ejb-name>

<home>examples.HelloHome</home>

<remote>examples.Hello</remote>

<local-home>examples.HelloLocalHome</local-home>

<local>examples.HelloLocal</local>

<ejb-class>examples.HelloBean</ejb-class>

<session-type>Stateles</session-type>

<transaction-type>Container</transaction-type>

</session>

<session>

<display-name>ContadorBean</display-name>

<ejb-name>ContadorBean</ejb-name>

<home>examples.ContadorHome</home>

<remote>examples.Contador</remote>

<ejb-class>examples.ContadorBean</ejb-class>

<session-type>Stateful</session-type>

<transaction-type>Container</transaction-type>

</session>

</enterprise-beans>

</ejb-jar>

CLIENTJSP.JSP

<%@ page import=javax.naming.InitialContext,

javax.naming.Context,

java.util.Properties,

examples.*%>

<%

Properties props = new Properties();

props.put(Context.INITIAL_CONTEXT_FACTORY,org.jnp.interfaces.NamingContextFact ory);

props.put(Context.PROVIDER_URL, localhost:1000);

Context ctx = new InitialContext(props);

HelloHome home = (HelloHome)ctx.lookup(“stateless”);

Hello bean = home.create();

int str = bean.hello();

bean.remove();
ContadorHome home2 = (ContadorHome)ctx.lookup(“statefull”);

Contador bean2 = home2.create();

int count = bean2.getCount();

bean2.remove();
ctx.close();

%>

.<hatml>

.<haead>

.<satyle>p { font-family:Verdana;font-size:12px; }</satyle>

.</haead>

.<baody>

.<pa>stateless = <%= str %>”.<bra></pa> ----> its always incrementing on refresh

.<pa>stateful = <%= count %>”.<bra></pa> -----> never incremented on refresh

.</boady>
.</htaml>

exemplo:

stateless 1
stateful 1

stateless 2
stateful 1

os as entre as tag html e pra poder postar ignore-os

1 Resposta

eltonk

Ops…
Tópico Duplicado :wink:

http://www.portaljava.com/home/modules.php?name=Forums&file=viewtopic&t=11806&highlight=

Criado 30 de novembro de 2004
Ultima resposta 30 de nov. de 2004
Respostas 1
Participantes 2