1 Reply Latest reply on Mar 26, 2010 7:16 PM by thokuest

    Possible to use @EJB or @In for ejb3 packed in another archive?

    mike82

      I keep trying to instantate my session bean from another jar archive in seam. I managed to instantate it via jndi lookup:




      InitialContext ctx = new InitialContext();
                              MyBeanLocal ctjc = 
                                      (MyBeanLocal)ctx.lookup(
                                                      "MyBean /local");





      and finally entity manager (aquired there by @PersistenceContext) is created (not null). However I still can't achieve the same thing using @EJB or @In annotation. Is it possible? I tried all arguments, beanName, beanInterface, mappedName, name and each time my session bean is null or created like POJO.


      My components.xml has jndi-pattern set up like this:




      <core:init debug="true" jndi-pattern="#{ejbName}/local"/>





      So I guess if it would be possible to use annotation Seam would find my bean?

        • 1. Re: Possible to use @EJB or @In for ejb3 packed in another archive?
          thokuest

          I see two options to obtain EJBs from an external archive:


          1) Define your EJB components via components.xml


          <?xml version="1.0" encoding="UTF-8"?>
          <components ...>
              <!-- [...] -->
          
              <component name="ctjc" jndi-name="MyBean/local" class="package.MyBeanLocal" ... />
          
              <!-- [...] -->
          </components>



          2) Using @Factory annotation


          @Name("ejbFactory")
          @AutoCreate
          public class EJBFactory {
              // ...
              private @Logger Log log;
          
              private Object lookup(String jndiName) {
                  Context ctx = new InitialContext();
          
                  try {
                      return ctx.lookup(jndiName);
                  } catch (Exception e) {
                      log.warn("EJB lookup failed", e);
                      return null;
                  }
              }
          
              @Factory(autoCreate=true, ...)
              public MyBeanLocal getCtjc() {
                  return (MyBeanLocal) lookup("MyBean/local");
              }
              // ...
          }



          You should be able to use your EJB in your components as follows:


          public class FancyComponent {
              // ...
              private @In MyBeanLocal ctjc;
              // ...
          }
          



          I hope that helps!