4 Replies Latest reply on Oct 14, 2002 12:51 AM by icanoop

    Bean access across multiple J2EE apps

    icanoop

      Hi,

      I'm fairly new to JBoss and J2EE, so any help is appreciated.

      I am creating a set of J2EE apps that will be deployed in seperate jar files. In some cases they depend on eachother, but I would like to keep them as independent as possible.

      The best way I can come up with is to do the following...

      For each object that will be shared between apps, I define an interface that contains all the members used by multiple apps. I compile those interfaces and package them as a jar, and then use that jar to compile my apps with.

      For each shared object, only one app actually has a bean that implements it's interface, and that bean will offer it as a service through JNDI. Then the beans in other apps that need to use it, will get it from JNDI as an object of that interface I defined seperately. So they don't have any idea what bean is actually implementing the interface.

      That way, it seems like the dependent apps can operate with any other apps, so long as one of them is offering the service of the interfaces it needs. This way I can have different apps that offer a service implementing the defined interface and they can be switched on the fly, just like any JBoss app.

      So in conclusion, is this possible? Is this a good practice or a bad one? Are there better ways to accomplish what I want? Any comments at all?

      What I am wondering is if JBoss will let me use objects just based on the interface, when the actual implementation does not yet exist at compile time. I am also wondering if this idea will cause a performance penalty, even though the implementing beans would have local interfaces.

      Also, can a bean have a local and a remote interface? Local for other beans in that JVM, and remote for clients?

      Thanks a lot,
      Ryan

        • 1. Re: Bean access across multiple J2EE apps

          Yes this is all possible, it is the way EJB
          is intended to work.

          Your bean does not implement the interfaces directly,
          instead it implements the javax.ejb interfaces.

          The implementation is created by the EJB container
          which delegates to your bean as required.

          JBoss will optimize any use of a remote interface
          to local behaviour in the same JVM, you still need
          local interfaces for CMR.

          Regards,
          Adrian

          • 2. Re: Bean access across multiple J2EE apps
            icanoop

            Ok, good to hear I am atleast in the right ballpark.

            One of the goals I have is to be able to compile my different apps without having the other apps available.

            For example, I have an app called someapp.jar, and it contains an EJB called SomeEJB. It uses an entity bean called AnotherEJB, that is contained in a different app called anotherapp.jar. I would like to be able to compile and package someapp.jar without having anotherapp.jar on my machine.

            The solution I am thinking of would be to make a set of interfaces and put them in a package called common.jar. Common.jar would have an interface that describes all the shared functionality of AnotherEJB, and this interface is called Another. The remote interface of AnotherEJB would extend Another.

            Then I could compile someapp having only common.jar in the classpath, and not having anotherapp.jar in the classpath. When someapp needs to use the shared bean it would get the remote interface through JNDI, but cast it as an object of the Another interface. Since the actual remote interface of AnotherEJB extends Another, I think that would work. And then somapp only needs common.jar to compile, but when it's actually running it ends up using AnotherEJB, which is contained in anotherapp.jar.

            The reason I want to do this is so that if someone does not like my implementation of the Another interface (namely AnotherEJB), they can create a seperate implementation called YetAnotherEJB, whose remote interface also extends Another. Then using JBoss hot deploying, they could replace AnotherEJB with YetAnotherEJB, and SomeEJB would still work because it's only using the Another interface, and both remote interfaces extend Another.

            Is there anything wrong with this? Would you experienced JBoss developers consider this an elegant solution, or is there a better way?

            Ryan

            • 3. Re: Bean access across multiple J2EE apps

              You will be ok provided you have

              AnotherHome
              Another
              <ejb-class>YetAnotherBean</ejb-class>

              If you do this:

              YetAnotherHome
              YetAnother
              <ejb-class>YetAnotherBean</ejb-class>

              Where
              public interface YetAnother extends Another
              {
              ...
              }

              The client would require the interfaces
              YetAnotherHome.class and YetAnother.class

              It is possible to configure the client to download
              the classes from the server using the RMI codebase.

              Regards,
              Adrian

              • 4. Re: Bean access across multiple J2EE apps
                icanoop


                Ok, thanks for the example. It made me realize that I was misunderstanding something. I think I got it now.

                Thanks,
                Ryan