使用JNDI配置EJB:NameNotFoundException

时间:2014-10-06 12:34:55

标签: java java-ee jndi wildfly-8

我有以下结构

MyProject.ear  (module name) MyProjectEar
   |----- lib
   |----- META-INF
   |----- MyProject.war
   |----- MyProject.jar (module name) MyProject

在MyProject.jar,我有几个EJB,包括mypackages.myproject.ejb.impl.ConfigManager(在mypackages.myproject.ejb.local.ConfigManagerLocal中有一个本地接口)

ConfigManager

@Singleton
@Startup
public class GestorConfiguracion implements GestorConfiguracionLocal {

  @PostConstruct
  private void init() {
    Context ctx = null;
    try {
      ctx = new InitialContext();
      ctx = (Context) ctx.lookup("java:comp");
    } catch (Exception e) {
      return;
    }
    try {
      String jndiEnv = (String) ctx.lookup("myProject/srv/environment");
    } catch (Exception e) {
      log.log(Level.WARNING, "Exception in jndienv", e);
    }
    try {
      appVersion = (String) ctx.lookup("myProject/buildVersion"));
    } catch (Exception e) {
      log.log(Level.WARNING, "Exception in buildversion", e);
    }

myProject/srv/environment是服务器的本地(因此它不会在EAR构建之间发生变化),因此它在standalone.xml中设置,如下所示

<subsystem xmlns="urn:jboss:domain:naming:2.0">
  <bindings>
    <simple name="java:/env/myProject/srv/environment" value="DEV"/>
  </bindings>
  <remote-naming/>
</subsystem>
通过向myProject/buildVersion添加env-entry-value,将在每个建筑物中更改

env-entry

我尝试了两种方法:

1)在MyProject.ear / META-INF / application.xml中添加以下行:

<env-entry>
  <env-entry-name>myProject/buildVersion</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>0.0.1</env-entry-value>
</env-entry>

<resource-ref>
  <res-ref-name>myProject/srv/environment</res-ref-name>
  <res-type>java.lang.String</res-type>
  <lookup-name>java:/env/myProject/srv/environment</lookup-name>
</resource-ref>

我只收到javax.naming.NameNotFoundException的消息:

  • service jboss.naming.context.java.comp.MyProjectEar.MyProject.ConfigManager.myProject.buildVersion

  • service jboss.naming.context.java.comp.MyProjectEar.MyProject.ConfigManager.myProject.srv.environment

1 个答案:

答案 0 :(得分:0)

结果是我正在查看错误的背景。

  • 如果env-entryresource-ref标签位于EJB项目的ejb-jar.xml中,我会在java:comp/env/中找到它们(vg,java:comp/env/myProject/buildVersionjava:comp/env/myProject/srv/environment);我需要将第一次查找更改为ctx = (Context) ctx.lookup("java:comp/env");

  • 如果env-entryresource-ref标记位于EAR项目的application.xml中,我会在java:app/env/中找到它们(vg,java:app/env/myProject/buildVersion和{ {1}});我需要将第一次查找更改为java:app/env/myProject/srv/environment