无法使用JBoss AS 7.1实例化EJB

时间:2012-08-29 14:25:40

标签: java ejb jboss7.x

我定义了一个EJB,我试图在另一场战争中使用它:

ejb-api.jar

@Remote
public interface Example {
    // ...
}

ejb-impl.war中(使用ejb-api.jar):

@Stateless(mappedName = "ExampleEjb")
public class ExampleBean implements Example {
    // ...
}

ejb-user.war中(使用ejb-api.jar):

@WebServlet("/ejbuser")
public class UserServlet extends HttpServlet {

    @Override
    public void doGet(...) throws Exception {
        Example example = null;
        try {
            InitialContext context = new InitialContext();
            example = (Example)context.lookup("ExampleEjb");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

我将ejb-impl.warejb-user.war部署到JBoss AS 7.1的实例中。 如果我将所有类放在同一个war文件中,那么就找到了EJB。但是这样,它不是(我得到NamingException)。

此外,如果我添加一个包含这些内容的jndi.properties文件......

java.naming.factory.initial = org.jboss.naming.remote.client.InitialContextFactory
java.naming.provider.url = remote://localhost:4447
jboss.naming.client.ejb.context = true
java.naming.factory.url.pkgs = org.jboss.ejb.client.naming

...然后我收到了这样的警告:

16:55:46,268 WARNING [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-7) javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.example.war:main" from Service Module Loader: javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.example.war:main" from Service Module Loader
    at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:64)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:681) [rt.jar:1.7.0_03]
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307) [rt.jar:1.7.0_03]
    at javax.naming.InitialContext.init(InitialContext.java:242) [rt.jar:1.7.0_03]
    at javax.naming.InitialContext.<init>(InitialContext.java:192) [rt.jar:1.7.0_03]
    at com.sun.faces.config.WebConfiguration.processJndiEntries(WebConfiguration.java:687) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.config.WebConfiguration.<init>(WebConfiguration.java:134) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.config.WebConfiguration.getInstance(WebConfiguration.java:194) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:163) [jsf-impl-2.1.7-jbossorg-2.jar:]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_03]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_03]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_03]

你知道我做错了吗?

2 个答案:

答案 0 :(得分:4)

根据@Stateless' documentation,“应用服务器不需要支持任何特定形式或类型的映射名称,也不需要使用映射名称。”因此,使用它来更改bean的映射名称IMHO不是一个好主意。

我们试试

  1. 从@Stateless
  2. 中删除mappedName
  3. 已编辑:在java:global命名空间中使用ExampleBean的名称: java:global[/application name]/module name/enterprise bean name[/interface name]
  4. Further reading about portable JNDI syntax

    <强>编辑: 您将两个模块部署到同一个应用服务器,不是吗?然后,您不需要jndi.properties文件,让我们尝试不使用它。

答案 1 :(得分:0)

将“Dependencies:org.jboss.remote-naming”添加到.WAR文件的META-INF / manifest.mf中。这将加载org.jboss.remote命名模块并消除“Failed instantiate InitialContextFactory”异常。详情here

关于同样问题的答案see here