Alex Kartishev
Alex Kartishev

Reputation: 1856

Cannot get EJB from MBean in JBoss7

The problem is following. I am trying to migrate the project from JBoss 4.2 to JBoss 7.1. Previously ejb lookup was done in following way:

    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
    props.put(Context.SECURITY_PRINCIPAL, new SimplePrincipal(login));
    props.put(Context.SECURITY_CREDENTIALS, password);
    InitialContext ctx = new InitialContext(props);
    SomeClass someClass = (SomeClass) ctx.lookup("appName/SomeClass/local");

And everything worked fine. Now implementation is following:

InitialContext ctx = new InitialContext();
SomeClass someClass = (SomeClass) ctx.lookup("java:app/jarModuleName/SomeClass!com.example.SomeClass");

ejb interface:

@Local
public interface SomeClass(){
...
}

ejb class:

@Stateless(name = "SomeClass")
public class SomeClassImpl implements SomeClass() {
...
}

During boot jboss shows that binding is:

java:app/jarModuleName/SomeClass!com.example.SomeClass

But lookup() after causes NameNotFoundException.

The package structure is following:

appName.ear
|
+--jmx-services.sar (where lookup method is invoked)
|
+--jarModuleName.jar (where ejb is located.)

The content of ejb-jar.xml in jarModuleName:

<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
    <session>
        <ejb-name>SomeClass</ejb-name>
        <ejb-class>com.example.SomeClassImpl</ejb-class>
    </session>
</enterprise-beans>

Also can specify, that MBeans are created in "old-style", using jboss-service.xml and start()/stop() methods, not @Startup and @Singleton annotation.

Upvotes: 1

Views: 797

Answers (1)

Soosh
Soosh

Reputation: 812

Use both @Local and @Remote with two separate interfaces, and I highly recommend using @EJB.

Also put your EJBs in different jar file and deploy it.

This folder structure is working for me:

yourApp.ear
{
 -lib
 -META-INF
  {
   application.xml
  }
 -yourEJB.jar
 -yourWebContent.war
}

you should have an application.xml in your META-INF folder, an example of it is the following:

<?xml version="1.0" encoding="UTF-8"?>
<application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd">
  <module>
    <ejb>
      yourEJB.jar
    </ejb>
  </module>
  <module>
    <web>
      <web-uri>
        yourWar.war
      </web-uri>
      <context-root>
        yourAppRoot (use it as localhost://yourAppRoot)
      </context-root>
    </web>
  </module>
</application>


deploy yourApp.ear in JBoss.


I haven't use ejb-jar.xml in my apps.

Upvotes: 1

Related Questions