6 Replies Latest reply on Sep 17, 2007 9:33 AM by alrubinger

    handling ejb3 injection failure

    ajay662

      In my servlet I want to inject an EJB, only if its available.

      @EJB
      MyBeanClass myBean;


      My requirement is that 'myBean' gets initialized if corresponding EJB is available in the application, otherwise it stays NULL.

      Is there a way to achieve this? Is there a way to catch injection exceptions?

      I don't want to do a lookup since lookup requires knowledge of application name as well and I don't want to depend on that.

        • 1. Re: handling ejb3 injection failure
          alrubinger

          The @EJB Annotation does more than injection; it also establishes a dependency.

          For a JBoss-specific solution, you can also annotate your instance variable with @IgnoreDependency like:

          @EJB
          @IgnoreDependency
          MyBeanClass myBean;


          ...which I believe should work, though I've never tried this use case specifically. One thing it won't do is inject "myBean" if "MyBeanClass" is deployed *after* your other service - the container won't keep track of what injections it should perform if they're deployed at a later time.

          Also, your lookups don't necessarily have to point to a JNDI address that includes the application name. JBoss assigns these by default, but you can override with ejb-jar.xml or the @RemoteBinding/@LocalBinding annotations.

          S,
          ALR

          • 2. Re: handling ejb3 injection failure
            ajay662

            Thanks for your suggestions ALR.

            I have tried @IgnoreDependency in my other jboss-specific app and it does work fine.

            In this case however, I am actually looking for a portable solution and not something thats specific to a vendor.

            • 3. Re: handling ejb3 injection failure
              alrubinger

              In that case, would recommend using traditional Context.lookup() calls to get at your services, and would additionally bind each service to the container-non-specific local ENC in JNDI using container-specific metadata. This is actually discussed in another thread today:
              http://www.jboss.org/index.html?module=bb&op=viewtopic&t=117939

              S,
              ALR

              • 4. Re: handling ejb3 injection failure
                bensonfungjava

                Hi,

                I tried @IgnoreDepency, but it doesn't work for some reasons.
                The following are my code of servlet, please help

                package com.myejb3.servlet;

                import javax.ejb.EJB;
                import javax.naming.InitialContext;
                import javax.naming.NamingException;
                import javax.servlet.http.HttpServlet;

                import java.io.IOException;
                import javax.servlet.ServletException;
                import javax.servlet.http.HttpServletRequest;
                import javax.servlet.http.HttpServletResponse;

                import org.jboss.annotation.IgnoreDependency;

                import com.myejb3.intf.MyEJB3_1;

                /**
                * Servlet Class
                *
                * @web.servlet name="MyEJB3_1"
                * display-name="Name for MyEJB3_1"
                * description="Description for MyEJB3_1"
                * @web.servlet-mapping url-pattern="/MyEJB3_1"
                * @web.servlet-init-param name="A parameter"
                * value="A value"
                */
                public class MyEJB3_1Servlet extends HttpServlet {


                @EJB
                @IgnoreDependency
                MyEJB3_1 myejb3;
                public MyEJB3_1Servlet() {
                // TODO Auto-generated constructor stub
                }



                protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException,
                IOException {
                // TODO Auto-generated method stub
                super.doGet(req, resp);
                this.doPost(req, resp);
                }

                protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException,
                IOException {
                // TODO Auto-generated method stub
                String userName = (String)req.getParameter("username");
                String password = (String)req.getParameter("password");
                /* try {
                InitialContext ctx = new InitialContext();
                MyEJB3_1 myejb3 = (MyEJB3_1) ctx.lookup("demo_ejb/MyEJB3_1Bean/local");
                myejb3.setName(userName);
                myejb3.setPassword(password);
                } catch(NamingException e) {
                e.printStackTrace();
                }*/

                myejb3.setName(userName);
                myejb3.setPassword(password);

                getServletConfig().getServletContext().getRequestDispatcher("/result.jsp").forward(req, resp);
                }

                }


                Thanks
                Benson

                • 5. Re: handling ejb3 injection failure
                  bensonfungjava

                  Hi,

                  My JBOSS version is 4.2.1

                  Thanks
                  Benson

                  • 6. Re: handling ejb3 injection failure
                    alrubinger

                    Injection for Servlets isn't supported until the 5 Series, currently in its second Beta release..

                    http://www.jboss.com/index.html?module=bb&op=viewtopic&t=107353

                    S,
                    ALR