6 Replies Latest reply on May 3, 2002 4:28 PM by sgturner

    What does a home and remote interface really represent?

    cs710

      Hi, I have done some reading on ejb. However I still have some confusion. It would be really helpful if someone can shed some light.


      Context ctx = new InitialContext();
      HelloHome home = (HelloHome)ctx.lookup("Hello");
      Hello bean = home.create();


      Assuming the above code is how we get a handle to the actual bean and is part of a jsp page. I guess HelloHome is an object that you can use it to talk to ejb container and ask it to get you a remote proxy for a actual bean instance (could be newly created). My following question is in the jsp page, can I cache any of ctx, home and bean so that I don't have to do a lookup everytime someone invokes the page?

      Another thing I am curious is that before JBoss 3.0 clustering, how do people deploy JBoss servers on many machines? For example, suppose you have installed servlet engines on machine A and B, and installed JBoss on machine C and D. In this case are you running 2 naming servers on C and D? If so, do you have to somehow hard code in JSP pages about the jndi info?

      Many thanks.

        • 1. Re: What does a home and remote interface really represent?
          jwkaltz

          Yes you can cache the home interfaces; but not the beans because the methods on the home interfaces may have different permissions depending on which user is making the call.
          home.create() is not very expensive anyway.

          On your second question jboss by default also starts a naming service. So if you don't change anything, in your example you would have 2 servers and you might configure their names in a properties file leaving near your servlets. In a more elaborate system you could of course imagine the server settings (I mean naming-hostname & port) coming from some dynamic source such as an ldap repository.

          • 2. Re: What does a home and remote interface really represent?
            cs710

            Thanks for your reply. I am not sure what you mean in the second paragraph. Are you saying that JSP pages get one of the naming service running on Machine C and D from either a property file or from a central ldap and then look up the beans it needs to access? If this is the case, you can't really cache home interface. In addition, if you deploy HelloBean on machine C and AnotherBean on machine D, do you have to put this info in the property file or ldap that you have to look up HelloBean from Machine C naming service? Is it possible to configure a central naming service which has all the beans' info in JBoss? If we have to use an external naming server, how do we register ejbs with it? Thanks.

            • 3. Re: What does a home and remote interface really represent?
              davidjencks

              Get and read the jboss 3 clustering docs. It has clustered jndi.

              • 4. Re: What does a home and remote interface really represent?
                sgturner

                A couple of points. Hello is not a proxy to your bean, but technically is a proxy to the EJBObject. You could write a neat class that caches the context and home objects for you. Then your code becomes something like:

                Hello bean = (Hello)MyCache.getBean ("Hello", Hello.class)

                If you are using JBoss, then such a class is very easy to write. But if you use some other app server, then you may have to write more code, to handle what happens if the connection to Jndi goes down.

                • 5. Re: What does a home and remote interface really represent?
                  cs710

                  Interesting. Are you saying that you don't need to handle the case for JBoss? Is it because bean and jndi lives in the same JVM? How do you know the connection with jndi is dropped anyway? Can you simply determine simply based on you catch some exception when doing any of the following:
                  ctx.lookup()
                  home.create()

                  What should you do in this case? Do another lookup?

                  • 6. Re: What does a home and remote interface really represent?
                    sgturner

                    First of all, follow David's advice and read docs on Jboss clustering and all will become clear. In JBoss, the reference to the context is a lot more than a simple reference to some remote object. In JBoss, there is a lot of intelligence built into that proxy that will reestablish connections. Read the docs !! Absent using a JBoss server, a simple procedure to follow would be:

                    ask home for EJBObject ref
                    if failure, then ask context for new ref to home and go to top
                    if failure, then get new context and recycle
                    Of course, build in logic to avoid infinite loop.
                    There is more you could do based on type of exceptions thrown, but hey, use the superior JBoss, and its all handled for you.