4 Replies Latest reply on Oct 6, 2006 1:30 AM by grdzeli_kaci

    Automatic Destroy State in Stateful Session Bean

      hi all,
      i'm using ejb3 for server side component model and jsf for client side user interfase.

      on server i want catch the event when client close connection (for example close internet browser)

      i know @Remove and @Predestroy annotations but they is not usefull for me.

      i want that when clien disconnected from stateful session bean .......
      automacially called some method.

      can anybody help me :(
      thanks.
      Paata.

        • 1. Re: Automatic Destroy State in Stateful Session Bean

          I think i had ideological mistake :(

          • 2. Re: Automatic Destroy State in Stateful Session Bean

            ok, I find another problem :
            1.i have interface

            package com.magti.businesslayer.ejb3entity.oracle;
            
            import javax.ejb.Remote;
            
            @Remote
            public interface MyState {
             public void initialize();
            }
            

            2. Implementation :
            package com.magti.businesslayer.ejb3entity.oracle;
            
            import javax.ejb.Remote;
            import javax.ejb.Stateless;
            
            @Stateless
            @Remote(MyState.class)
            public class StateBean implements MyState
            {
             String name = null;
            
             public void initialize()
             {
             if (name == null)
             {
             System.out.println("Null");
             name = "Initialize";
             }
             else
             {
             System.out.println("Not Null -> "+name);
             }
             }
            }
            



            3. Test Client

            package com.magti.businesslayer.ejb3entity.oracle;

            import java.util.Properties;
            import javax.naming.Context;
            import javax.naming.InitialContext;

            public class TestMain
            {
            public static void main(String[] args)
            {
            try
            {

            Properties jndiProps = new Properties();
            jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
            jndiProps.setProperty(Context.URL_PKG_PREFIXES,
            "org.jboss.naming:org.jnp.interface");
            jndiProps.setProperty(Context.PROVIDER_URL,
            "jnp://192.168.9.136:1099");
            InitialContext ctx = new InitialContext(jndiProps);

            MyState state = (MyState) ctx.lookup("StateBean/remote");

            state.initialize();
            }
            catch (Exception e)
            {
            e.printStackTrace();
            }
            }
            }
            
            when i run client fierst time i get this result :
            
            21:47:33,652 INFO [STDOUT] Null
            
            second time :
            
            21:47:34,652 INFO [STDOUT] Null
            
            third time :
            
            21:47:35,652 INFO [STDOUT] Initialize
            
            forth time
            
            21:47:36,652 INFO [STDOUT] Initialize
            
            fifth time :
            
            21:47:37,652 INFO [STDOUT] Initialize
            
            .....
            .....
            
            
            can anybody explain me why ?
            
            this session bean is stateless.
            
            
            


            • 3. Re: Automatic Destroy State in Stateful Session Bean
              alrubinger

              You have an instance variable in your bean. By "Stateless", this means the container will not guarantee that the same instance will be returned to the same client on each invocation. At the end of the invocation, it's just returned to the pool. It's up to the developer to ensure the bean has no instance variables within that instance, or that all are reset to a common state, such that all instances in the pool can be considered "equal" and handed out to any client without evidence of that instance's history.

              S,
              ALR

              • 4. Re: Automatic Destroy State in Stateful Session Bean

                ok, thanks ALRubinger

                i think than i need always returned new instance for the same client.

                i think that stateless session bean always must return new instance to the client when i lookup from from the client...

                i did some changes on my sample bean :

                public void initialize()
                 {
                 System.out.println(this);
                 if (name == null)
                 {
                 System.out.println("Null");
                 name = "Initialize";
                 }
                 else
                 {
                 System.out.println("Not Null -> "+name);
                 }
                 }
                



                and it fiert time prints

                09:10:21,759 INFO [STDOUT] com.magti.businesslayer.ejb3entity.oracle.StateBean@dd8904
                09:10:21,759 INFO [STDOUT] Null
                


                and then always printed :

                09:10:21,759 INFO [STDOUT] com.magti.businesslayer.ejb3entity.oracle.StateBean@dd8904
                09:10:21,759 INFO [STDOUT] Not Null -> Initialize