10 Replies Latest reply on Apr 5, 2007 7:58 AM by Adrian Brock

    Acessing Microcontainer Beans in Servlet

    Martin Stoewer Newbie

      I have defined a few beans in jboss-beans.xml and I want to use them from a servlet. How can I get an instance of a class defined in jboss-beans.xml in my java code ?

        • 1. Re: Acessing Microcontainer Beans in Servlet
          Ales Justin Master

          Get a reference to underlying Controller.
          Or you can register your bean into JNDI or JMX and then just grab it from there.

          • 2. Re: Acessing Microcontainer Beans in Servlet
            Martin Stoewer Newbie

            Unfortunately I'm completely new to the microcontainer and I wasn't able to find any reference during my google search to a code example how to get this controler you are talking about. I tried the following:

            bootstrap = new BasicBootstrap();
            bootstrap.run();
            this.tracker = (TrackerImp)bootstrap.getKernel().getRegistry().getEntry("trackerImplementation");

            Where "trackerImplemenation" is the name of my bean in jboss-beans.xml:


























            So if you could provide a code example how to get this controller, I'd appreciate it very much.

            • 3. Re: Acessing Microcontainer Beans in Servlet
              Ales Justin Master

               

              "obelix1998" wrote:

              So if you could provide a code example how to get this controller, I'd appreciate it very much.

               protected Kernel bootstrap() throws Throwable
               {
               BasicBootstrap bootstrap = new BasicBootstrap();
               bootstrap.run();
               return bootstrap.getKernel();
               }
              
               Kernel kernel = bootstrap();
               KernelController controller = kernel.getController();
              


              • 4. Re: Acessing Microcontainer Beans in Servlet
                Martin Stoewer Newbie

                Ok, but it seems that also from the controller I cannot get anywhere near my beans described in jboss-beans.xml :-(

                Whatever I do (and I also checked in the debugger), I cannot get hold of my bean that is defined like this:

                bean name="trackerImplementation" class="net.pelvit.tracker.jar.imp.TrackerImp"

                Could you please provide a complete code snippet how to get this bean ?

                • 5. Re: Acessing Microcontainer Beans in Servlet
                  Ales Justin Master

                   

                  "obelix1998" wrote:

                  Could you please provide a complete code snippet how to get this bean ?

                  KernelController controller = ...;
                  ControllerContext context = controller.getInstalledContext("<name>");
                  // ControllerContext context = controller.getContext("<name>", <state>);
                  Object target = context.getTarget();
                  


                  • 6. Re: Acessing Microcontainer Beans in Servlet
                    Martin Stoewer Newbie

                    This simply does not work :( There is not a single example to be found, how to access the beans defined in jboss-beans.xml from JAVA and I tried many things, none of which is working. It doesn't seem to me such an unusual idea to define beans in the xml and then use these instances within the java code (in my case a servlet). Could you please provide one WORKING example how this really works ?!!!

                    I defined this in jboss-beans.xml:

                    <bean name="surferCookieHandler" class="net.pelvit.tracker.jar.imp.SurferCookieHandler">
                    </bean>


                    I added this jboss-app.xml (tracker.beans is my "jar"):
                    <jboss-app>
                     <module>
                     <service>tracker.beans</service>
                     </module>
                    </jboss-app>


                    The in the code I used the following (as suggested by you):

                    BasicBootstrap bootstrap;
                    bootstrap = new BasicBootstrap();
                    bootstrap.run();
                    Object target = bootstrap.getKernel().getController().getInstalledContext("surferCookieHandler").getTarget();
                    

                    Please give an example, how to really make this work because as it is, it simply doesn't and I'm beginning to think, I'm wasting my time on this microcontainer :-(


                    • 7. Re: Acessing Microcontainer Beans in Servlet
                      Ales Justin Master

                       

                      "obelix1998" wrote:

                      The in the code I used the following (as suggested by you):

                      BasicBootstrap bootstrap;
                      bootstrap = new BasicBootstrap();
                      bootstrap.run();
                      Object target = bootstrap.getKernel().getController().getInstalledContext("surferCookieHandler").getTarget();
                      


                      You need to get a hold of underlying Kernel reference, not create a new one - your new one of course doesn't know anything about your beans, which were deployed by the core Kernel.

                      Since I see you are trying to bind MC with Servlet, there is a whole sub-project written that already does similar: http://wiki.jboss.org/wiki/Wiki.jsp?page=EmbeddedAndTomcat


                      Could you please provide one WORKING example how this really works ?!!!
                      Please give an example, how to really make this work because as it is, it simply doesn't and I'm beginning to think, I'm wasting my time on this microcontainer :-(

                      No need to get nasty and frustrated, nobody learned '2 year dev project' over night. ;-)

                      • 8. Re: Acessing Microcontainer Beans in Servlet
                        Martin Stoewer Newbie

                        We have one example:

                        we use example from microcontainer: example-simple.beans
                        we deployed it and we can see it also in jmx console:
                        jboss.beans: name='example-simple.beans',service=JBossBeanDeployment

                        please, give us an example how can we get an access to Simple bean in servlet. We run JBoss Application Server 4.0.5 with default configuration and we add one test web application with one servlet. Nothing what we read helps us.

                        • 9. Re: Acessing Microcontainer Beans in Servlet
                          Ales Justin Master

                          Like I already written - when a bean is installed, register it into mbeanserver or jndi, and then pull it out in servlet.

                          Or you can always do a singleton approach - with setting underyling Kernel to static field, and accessing it later.
                          This is how JBossWS is doing it - look for KernelLocator.

                          As you could see, there is no binding layer yet on top of MC that would make things easier - but this is what JBoss Seam is about. We are looking into providing Seam + Microcontainer integration.

                          • 10. Re: Acessing Microcontainer Beans in Servlet
                            Adrian Brock Master

                            I don't understand this thread, you guys are talking about completely different things!

                            If you have a *.beans deployment then that will create the kernel and also
                            but more importantly, it will parse the xml and populate that kernel with the beans.

                            Creating your own kernel (as Ales suggested) will just give you your own
                            empty kernel. Without a "Bean Deployer" nothing is going to populate and even
                            if it did, they would be different objects to what is constructed by the *.beans.

                            If you want to get access to the beans from the .beans file
                            in another context that doesn't have a reference to that kernel
                            and doesn't use IOC then you need to use something like the GOF Locator/Singleton
                            pattern.

                            Examples of different possible implementations of this are here:
                            [url]http://anonsvn.jboss.org/repos/jbossas/projects/microcontainer/trunk/kernel/examples/[url]
                            in the "locator" directory.