9 Replies Latest reply on Dec 4, 2009 3:51 AM by Mirek Sz

    EJB 3.0 lookup from Pojo and Injection

    Mirek Sz Newbie

      Hello,
      I have a question, I have

      @Stateless
      public class AccountServiceBean implements AccountService{
      
      }
      
      @Stateless
      public class InvoiceServiceBean implements InvoiceService {
      
      @EJB
      AccountService accountService;
      
      public void doIt(){
      SimplePojo simple = new SimplePojo();
      simple.search();
      
      }
      
      }
      
      public class SimplePojo{
      
      
      public void search(){
      
       Context context = new InitialContext();
       AccountService accountService =(AccountService)context.lookup("AccountServiceBean /local");
      }
      
      }


      Now my question I injectiong AccountService into InvoiceServiceBean , and invoke doIt inside it I create pojo and lookup for AccountService. If I can be sure that injected object AccountService will be the same lookup object??? I think that is the same reguest context

      Best regards

        • 1. Re: EJB 3.0 lookup from Pojo and Injection
          jaikiran pai Master

           

          "MirekSz" wrote:


          If I can be sure that injected object AccountService will be the same lookup object???


          What is the AccountService? Is it a Stateless session bean or a stateful session bean? More importantly, why do you want it to be the same instance?

          • 2. Re: EJB 3.0 lookup from Pojo and Injection
            Mirek Sz Newbie

            Thanks for reply

            AccountService is Stateless.
            I need the same instance beacuse in InvoiceItemServiceBean a do some job which generates data and assign it to the AccountService field. After in POJO I want this data

            • 3. Re: EJB 3.0 lookup from Pojo and Injection
              Wolfgang Knauf Master

              Hi,

              by definition, the management of EJB class instances (created by "new MyBean()" calls) is a matter of the server and totally undefined for your own client code. So, you can never expect to get instance of a previous method call. This is also true for stateful session beans, but here the server will restore the instance variables state for you.

              Please provide us with more details of your architecture. Currently, I don't get the point what is happening in "AccountServiceBean". If it contains some business method, which stores it's results in a member variable, I would advice you to convert the bean to some POJO/helper class, and return the instance of this class from "SimplePojo.search".

              Hope this helps

              Wolfgang

              • 4. Re: EJB 3.0 lookup from Pojo and Injection
                Mirek Sz Newbie

                I try explain exacly what I doing, the prevoius example wasn't real(but describe problem)

                I have fasade

                @Stateful
                public class FasadeBean implements Fasade{
                
                @EJB
                private ChangedIdsCollectorService;
                
                
                
                public ServerResponse execute(String serviceName, String serviceMethod, Object[] arguments){
                
                 Object result = serviceExecutor.execute(serviceName,serviceMethod, arguments);
                
                 return new ServerResponse(result, ChangedIdsCollectorService.getChangedIds());
                
                
                }
                
                
                }
                

                and service which collect ids of changed object(ENTITY)
                @stateles
                public class ChangedIdsCollectorServiceBean inplements ChangedIdsCollectorService{
                
                private List<Long> changedIds;
                
                public void addChangedId(Long id){
                changedIds.add(id);
                }
                
                public List<Long> getChangedIds(){
                return changedIds;
                }
                
                }
                


                When i call fasade and execute for example ("OperatorService","update" , operatorEntity) this fasade invokes "OperatorService" and return updated enitity, but I also have in my Entity
                metod which lookup for ChangedIdsCollectorService and add yourself to his list:
                public class AbstractEntity {
                
                @javax.persistence.PostUpdate
                public void changeListener(){
                 Context context = new InitialContext();
                 ChangedIdsCollectorService ChangedIdsCollectorService = ChangedIdsCollectorServicecontext.lookup("ChangedIdsCollectorService/local");
                 ChangedIdsCollectorService.addChangedId(this.id);
                
                
                }
                }
                

                In this way I get information about changed objects....update of operator can invokes some businness rule which can updates another object and I also will be have this Ids.

                Now I don't now if in FasadeBean I will be have the same instance of ChangedIdsCollectorService (from injection) like in Entity(from lookup)




                • 5. Re: EJB 3.0 lookup from Pojo and Injection
                  Wolfgang Knauf Master

                  Hi,

                  I think I understand. But as I said above: a stateless bean must not contain member variables, because their values will be lost/undefined after a business method is finished.

                  I also think that it is not really possible to "store" data beetween two EJB calls, because the EJB layer does not know "sessions", only transactions.
                  I am also not sure whether a JNDI lookup for a stateful session bean in the same transaction will return the SAME instance as a previous call. Didn't find anything about this in the web.

                  What for do you need the "ChangedIdsCollectorService"? If you need it to update an ID after its autocreation in other entities: would relationships help?

                  Best regards

                  Wolfgang

                  • 6. Re: EJB 3.0 lookup from Pojo and Injection
                    Mirek Sz Newbie

                    Thanks for replay
                    I need this information (about changed entity) to inform my Swing client which records was updated.

                    "Wolfgang Knauf" wrote:

                    I am also not sure whether a JNDI lookup for a stateful session bean in the same transaction will return the SAME instance as a previous call.


                    And what with stateless : JNDI lookup for a STATELESS session bean i the same transaction will return the SAME instance as a previous call ??


                    Best regards

                    • 7. Re: EJB 3.0 lookup from Pojo and Injection
                      Peter Johnson Master

                       

                      JNDI lookup for a STATELESS session bean i the same transaction will return the SAME instance as a previous call ??

                      No. Stateless beans are kept in a pool; each time you ask for one via JNDI you could get a different instance. If you need a single bean, then look it up once and pass it to the methods that need to interact with it.

                      • 8. Re: EJB 3.0 lookup from Pojo and Injection
                        Wolfgang Knauf Master

                        Hi,

                        I need this information (about changed entity) to inform my Swing client which records was updated


                        This could be achieved be a Topic Queue. The ID changed message is posted by the EJB, and all swing clients register to it.

                        Hope this helps

                        Wolfgang

                        • 9. Re: EJB 3.0 lookup from Pojo and Injection
                          Mirek Sz Newbie

                          Thanks for help your answers realy help.

                          I understand that I can't be sure that I get the same instance (of Stateless) in the same transaction by lookup.

                          Is there exists other way to pass some data from one statelsss to other in the same transaction


                          In web application I used to it HttpSession and setAttribute.