10 Replies Latest reply on Dec 1, 2003 5:36 PM by Klaus Kaal

    [newbie] References between EJB

    Klaus Kaal Newbie


      maybe a silly question, then someone will answer it straight away.

      I have 2 EJB: EJB1 and EJB2
      then I have 2 clients CL1 and CL2.

      CL1 looks up EJB1 and creates it. The same does CL2 with EJB2.

      Now I would like to have the EJBs talk to each other. Maybe, I have to access the other via JNDI and the local interface. If so, how do I have to store the EJBs in JNDI to find and reference it?

      Tanks for your help.


        • 1. Re: [newbie] References between EJB
          Klaus Kaal Newbie

          Just putting the question into another phrase:

          How does an EJB find and access an instance of another EJB which is being used anywhere in the container?


          • 2. Re: [newbie] References between EJB
            martin_kersten Newbie

            Best would be buying a copy of O'Reilly's Enterprise JavaBeans book. Helped me a lot to get behind the newbie state ;)

            The reference of other EJBs within the EJB differs. CMP and BMP (Conterainer/Bean Managed Persistence) - Entity Beans - referring each other using perstistent fields representing foreign key constraints within a database.

            If you are using session beans / message driven beans just take a look at the home/local home interfaces. Use the find and create methods like you would do if you use 'em in your client code.

            If you are using an naming context to get the local home interface you may not need to narrow the reference.


            javax.naming.Context jndiContext - the context you are using.

            now just:
            MyBeanHomeLocal home=(MyBeanHomeLocal)jndiContext.lookup("java:comp/env/ejb/MyBeanHomeLocal");
            MyBeanLocal myBean=home.findByPrimaryKey(primaryKey);

            The jndiName of the home interface maybe a static constant provided by the home local interface.

            If you are using remote home interface/remote invocation the code will look the same like in your client.

            After using an stateless session bean you may call remove, which will free the session bean. But calling remove on statefull session beans and entity beans may remove the bean instance from the bean pool or the database. So take care about it.

            Tip: Entity beans may also get instances of other entity beans using the home interface but it is not recommanded.

            I hope this helps,


            • 3. Re: [newbie] References between EJB
              Klaus Kaal Newbie

              Thanks for your hint. But it does not answer my question:

              I need to find an EXISTING instance of an EJB in the container. From a bean inside the container as well as from a client.

              This would, I believe, involve registering the created bean in the INDI thus making it visible for others.

              Thanks for any help.


              • 4. Re: [newbie] References between EJB
                Raja Master

                Not sure if i understand you right but as soon as a bean is deployed, it gets a name in the JNDI Space. So if u want EJB1 to talk to EJB2, you look up the JNDI name of EJB2 just as you do with Client2.

                If both EJBs are in the same ear, you can create a link and use a ejb-ref in the ejb-jar.xml to lookup one from another.


                • 5. Re: [newbie] References between EJB
                  Klaus Kaal Newbie

                  Sorry, I have to stick to it because I do not get on with my work.

                  I use JSP as client with a bean that keeps and tracks the connection to JBOSS EJB container.

                  One client-bean creats a session-EJB which reads all status-vars from the db over CMP and keeps it for further processing (I call it status-manager).

                  On the next page, I need to process some data - lets call it machine-data - from the db. So, I create with lookup - home.create() an session-EJB that cares for this processing.

                  At this spot, I need some data from the status-manager. The client could read it out and transfer it to machine-data. But in my mind, I should be able to contact the status-manager in the ejb-container directly. But: wherever I read about EJB, I only find the creation-process of EJB beans with looking up the home-interface and creating the remote. This would create a new instance. But in my case, I do not want to create a new bean, I like to reference the existing instance of the status-manager in the container. But, how do I find it? I thought, I could load a reference to JNDI under a unique name for this very session and call it from another bean.

                  Up to now, I have not been able to load my status-manager into JNDI and find it again. Is there a pattern to do so?

                  Am I understandable now?

                  Thanks for your patience.


                  • 6. Re: [newbie] References between EJB
                    Klaus Kaal Newbie

                    Hi Gurus,

                    is there anybody who could answer my question. Im am stuck. Maybe its just a little misunderstanding.

                    Presently, my client has to drag all status-vars out of one EJB to set it as a parameter to the other one. That cannot be the solution.

                    How do you do these things: The usermanager has all infos about the user. Another JSP creates a new EJB and this needs some user-data. Does your client also cares for the transfer. Or do you have another way to get to the userdata on the EJB side?

                    I tried to find this solution in several books. But the all go around that problem. They all use the database as a central info-interface.

                    So, pls help


                    • 7. Re: [newbie] References between EJB
                      jamesstrachan Newbie


                      You may have found an answer by now, but here goes.

                      It sounds as though your usermanager contains information on users. So the data on each user could be represented in an entity bean - with persistent storage in the underlying database.

                      You can look up a specific instance of an entity bean from a session bean through the primary key - in this case, username. In the home interface, the method is defined as findByPrimaryKey().

                      If your users are short lived (logically, not in real life) you could use a stateful session bean, which first records user details and is then available from the next JSP page to use the stored data on the user. So the data used across pages is stored in the session bean and not the client.

                      You are quite right to complain that many textbooks just treat the applcation server as another way to read and write the database.

                      Turn it round. All the data is in the application server, and the database is there only to keep a copy of the application server data on hard disk.


                      • 8. Re: [newbie] References between EJB
                        Klaus Kaal Newbie

                        Thank you very much James,

                        as -for the first time-, I have the feeling that you understood a part of my question, I read your post word-by-word. And still have another question:

                        I would - in this example - keep all the userdata (after catching them with a entity bean from the db) in a stateful sessionbean. Ok. Then I would construct a web of other session beans (call them ejb1 - ejbN), which I would reference in the usersession. Now, from another JSP, I would like to comunicate with ejb2. The trouble: I could ask usersession to give me the reference to ejb2, but that would mean that userbean had to serialize ejb2 totally. But I just want to ask a single question to ejb2.
                        Would it not be possible that userbean (in the container) would register a reference to ejb2 in JNDI and pass that JNDI-name to my jsp, so that my jsp could lookup the ejb2-instance there?

                        You understand: I do not want to create ejb2 from the jsp, as it is already created and stateful. I just want a reference to it.

                        How would I do something like that?

                        Thank you for your time and efford.


                        • 9. Re: [newbie] References between EJB
                          kerem argenc Newbie

                          hey klaus,

                          i used this method.

                          i have session bean and i was stored in httpsession and each required page read from httpsession this object. this is very usefully for me.

                          this solution may be is not solve your problem.

                          i try only help you .


                          • 10. Re: [newbie] References between EJB
                            Klaus Kaal Newbie


                            I appreciate your input and I am happy for any discussion contribution as I think that all techniques to store EJB-references in JNDI are circumvented in the literature, I know.

                            Storing references in HTTP-context is not the answer to me, as I try to construct a net of objects in the EJB container. It should be abstracted in that way, that ANY type of client (HTTP or not) could connect to it.

                            The way, I am doing it now, is that wherever I create an EJB (by looking up the HOME interface and creating the Local/Remote interface), I store the Local/Remote interface under a unique name in JNDI. Thus it can be accessed by any client which knows the name.

                            But thanks anyhow.