4 Replies Latest reply on Apr 30, 2008 12:07 AM by jaikiran

    EJB Annotation question?

    jxcheng

      I deployed and tested a simple EJB 3.0 program successfully with JBOSS 4.05.

      The following code use JNDI lookup works fine.

      Hashtable<String,String> h= new Hashtable<String,String>();
      h.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      h.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      h.put(Context.PROVIDER_URL, "localhost");
      
      InitialContext ctx = new InitialContext(h);
      
       WordReverse wr = (WordReverse) ctx.lookup("/WordReverseBean/remote");
      
      wr.getReversedWorld("test")
      


      However, I have problems using EJB annotation. The following code compiles fine. but run into run-time Exception
      Exception in thread "main" java.lang.NullPointerException
      It complains that the instance variable wr is NULL. I tried different attribute values for the EJB annotation without sucsess.

      @EJB(mappedName="jnp://localhost:1099/WordReverseBean/remote")
      //@EJB(beanName="WordReverseBean")
      //@EJB
      private static WordReverse wr;
      
      public static void main(String[] args) throws NamingException{
      
      ...
      wr.getReversedWorld("test")
      ...
      


      Could anyone help here?

      Thanks!

        • 1. Re: EJB Annotation question?
          polesen

          You do not mention in which context you've put the @EJB annotation that fails, but judging from the "public static void main..." code in your example, it seems to be outside container code in a client application. @EJB annotations does not work there. In clients, you (still) need to do JNDI lookups.

          You can only inject ejbs in certain supported classes, like EJB implementations and servlets. Actually, you cannot in servlets but that is a JBoss issue (see: http://docs.jboss.org/ejb3/app-server/tutorial/ear/ear.html) even though you should be able to.

          • 2. Re: EJB Annotation question?
            jxcheng

            Yes, it is in a client application.

            Why @EJB annotation does not work there?

            Is it due to that JBOSS does not fully implement EJB 3.0?


            In Sun's JavaEE 5 tutorial and sample code,
            I see the following in a client program:

            package converter.client;
            
            import converter.ejb.Converter;
            import java.math.BigDecimal;
            import javax.ejb.EJB;
            
            
            /**
             *
             * @author ian
             */
            public class ConverterClient {
             @EJB
             private static Converter converter;
            
             /** Creates a new instance of Client */
             public ConverterClient(String[] args) {
             }
            
             /**
             * @param args the command line arguments
             */
             public static void main(String[] args) {
             ConverterClient client = new ConverterClient(args);
             client.doConversion();
             }
            
             public void doConversion() {
             try {
             BigDecimal param = new BigDecimal("100.00");
             BigDecimal yenAmount = converter.dollarToYen(param);
            
             System.out.println("$" + param + " is " + yenAmount + " Yen.");
            
             BigDecimal euroAmount = converter.yenToEuro(yenAmount);
             System.out.println(yenAmount + " Yen is " + euroAmount + " Euro.");
            
             System.exit(0);
             } catch (Exception ex) {
             System.err.println("Caught an unexpected exception!");
             ex.printStackTrace();
             }
             }
            }
            


            The client code above works well with Sun's Java System Application Server 9.0.


            Thanks




            • 3. Re: EJB Annotation question?
              afmarsal

              Hi, is it possible with JBoss 4.2.2 ?
              Is it planned for Jboss 5.x ? It's a great feature when developing a client...

              Thanks.

              • 4. Re: EJB Annotation question?
                jaikiran

                 

                "afmarsal" wrote:
                Hi, is it possible with JBoss 4.2.2 ?
                Is it planned for Jboss 5.x ? It's a great feature when developing a client...


                It's not possible with JBoss-4.2.2. However this is available in JBoss-5.0. There's a sticky in this forum http://www.jboss.com/index.html?module=bb&op=viewtopic&t=107353 which explains exactly this same thing:

                There are a lot of questions about injection not working in AS 4.0 and 4.2. Most of the problems boil down to injection not working the web layer.

                This is because AS 4.0 and 4.2 are J2EE 1.4 servers with an EJB3 plugin. This means that injection and annotations only work within the EJB3 plugin. To get to your EJBs you still have to perform a JNDI lookup from your servlet / application client.

                To get all the functionality of a JavaEE 5 server you must use JBoss Application Server 5 instead.