0 Replies Latest reply on Oct 2, 2012 7:52 AM by kirkor-pl

    Connect to JBoss 7 AS from Tomcat 7 using JNDI, then inject bean with @EJB

    kirkor-pl

      Hello All,

       

      I have two machines, one is Tomcat with simple Web App and another is JBoss AS 7 with deployed remote EJB. When I use this code on my Tomcat:

       

      Properties jndiProps = new Properties();
      jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
      jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
      jndiProps.put(Context.SECURITY_PRINCIPAL, "kirkor");
      jndiProps.put(Context.SECURITY_CREDENTIALS, "xxxxx");
      jndiProps.put("jboss.naming.client.ejb.context", true);

      Context ctx = null;       
      HelloWorld ob = null;

      try {

           ctx = new InitialContext(jndiProps);

           ob = (HelloWorld) ctx.lookup("okm-ear/okm-services/HelloWorldRemote!pl.beriko.openkm.service.HelloWorld");
           System.out.println(ob.sayHello("KirkoR"));
      } catch (NamingException e) {
           e.printStackTrace();
      }

       

      Works very nice!

       

      But if I want use @Annotatios and inject my EJB into my servlet... well, dosn't work (even with jboss-naming-client.properties). Is this correct approach:

      @EJB(lookup = "okm-ear/okm-services/HelloWorldRemote!pl.beriko.openkm.service.HelloWorld")
      private HelloWorld hw;

      Other annotations like @Inject just works fine.

       

      I'm using maven, and have all needed dependencies, like:

       <dependencyManagement>

                          <dependencies>

                                    <!-- Import scope will provide versions for dependencies below. -->

                                    <dependency>

                                              <groupId>org.jboss.weld.servlet</groupId>

                                              <artifactId>weld-servlet-parent</artifactId>

                                              <version>${version.weld-servlet}</version>

                                              <type>pom</type>

                                              <scope>import</scope>

                                    </dependency>

       

       

                                    <dependency>

                                              <groupId>org.jboss.spec</groupId>

                                              <artifactId>jboss-javaee-6.0</artifactId>

                                              <version>3.0.0.Final</version>

                                              <type>pom</type>

                                              <scope>import</scope>

                                    </dependency>

       

       

                                    <dependency>

                                              <groupId>org.jboss.as</groupId>

                                              <artifactId>jboss-as-ejb-client-bom</artifactId>

                                              <version>7.1.1.Final</version>

                                              <type>pom</type>

                                              <scope>import</scope>

                                    </dependency>

                          </dependencies>

                </dependencyManagement>

       

       

                <dependencies>

                          <dependency>

                                    <groupId>com.vaadin</groupId>

                                    <artifactId>vaadin</artifactId>

                                    <version>${vaadin.version}</version>

                          </dependency>

       

       

                          <dependency>

                                    <groupId>org.jboss.weld.servlet</groupId>

                                    <artifactId>weld-servlet</artifactId>

                          </dependency>

       

       

                          <dependency>

                                    <groupId>org.jboss.spec.javax.transaction</groupId>

                                    <artifactId>jboss-transaction-api_1.1_spec</artifactId>

                          </dependency>

       

       

                          <dependency>

                                    <groupId>org.jboss.spec.javax.ejb</groupId>

                                    <artifactId>jboss-ejb-api_3.1_spec</artifactId>

                          </dependency>

       

       

                          <dependency>

                                    <groupId>org.jboss</groupId>

                                    <artifactId>jboss-ejb-client</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.xnio</groupId>

                                    <artifactId>xnio-api</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.xnio</groupId>

                                    <artifactId>xnio-nio</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.remoting3</groupId>

                                    <artifactId>jboss-remoting</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.remoting3</groupId>

                                    <artifactId>remoting-jmx</artifactId>

                                    <version>1.0.1.Final</version>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.sasl</groupId>

                                    <artifactId>jboss-sasl</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.marshalling</groupId>

                                    <artifactId>jboss-marshalling-river</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.as</groupId>

                                    <artifactId>jboss-as-naming</artifactId>

                          </dependency>

                          <dependency>

                                    <groupId>org.jboss.marshalling</groupId>

                                    <artifactId>jboss-marshalling</artifactId>

                          </dependency>

                </dependencies>

      Do you have some propositions?