javax.naming.NameNotFoundException WildFly上的EJB HelloWorld

时间:2018-07-27 13:03:14

标签: java ejb wildfly

我是EJB 3.1的初学者,试图运行我的第一个Hello World程序并获取NameNotFoundException。

Exception in thread "main" javax.naming.NameNotFoundException: WFNAM00004: Name "global/HelloWorld/HelloWorldBean!api.HelloWorldRemote" is not found
at org.wildfly.naming.client.util.NamingUtils$1.lookupNative(NamingUtils.java:95)
at org.wildfly.naming.client.AbstractContext.lookup(AbstractContext.java:84)
at org.wildfly.naming.client.WildFlyRootContext.lookup(WildFlyRootContext.java:150)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at driver.HelloWorld.main(HelloWorld.java:27)

这是我的课程

本地接口:

@Local
 public interface HelloWorldLocal {
public void getHello();
 }

远程接口:

@Remote
public interface HelloWorldRemote extends HelloWorldLocal {

 }

Bean类:

@Stateless(name="HelloWroldBean")
public class HelloWorldBean implements HelloWorldLocal, HelloWorldRemote{

@Override
public void getHello() {
    System.out.println("Hello Cheppura");
}

  }

客户:

public class HelloWorld {

public static void main(String[] args) throws NamingException {
    final Hashtable<String, String> jndiProperties = new Hashtable<>();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY,         
            "org.wildfly.naming.client.WildFlyInitialContextFactory");
    Context con = new InitialContext(jndiProperties);

    final String appName = "";
    final String moduleName = "HelloWorld";
    final String beanName = HelloWorldBean.class.getSimpleName();
    final String viewClassName = HelloWorldRemote.class.getName();

    Object ob = con.lookup("java:global" + appName + "/" + moduleName  + "/" + beanName + "!" + viewClassName);

    HelloWorldRemote hw = (HelloWorldRemote) PortableRemoteObject.narrow(ob, HelloWorldRemote.class);

    hw.getHello();
}

}

有人可以对此提出建议吗? 预先感谢

1 个答案:

答案 0 :(得分:0)

您违反了JEE规范,因为EJB接口中没有继承。

您的Remote接口扩展了Local,但这种方法违背了规范。我认为,无论运行时环境如何,此类EAR都永远不会正确部署。

如果您想以这种方式编写更少的代码,请使用public void getHello();方法创建第三个接口,并将其扩展到LocalRemote接口中。

public interface Secondary {
    public void getHello();
}

@Remote
public interface HelloWorldRemote extends Secondary {
 }

@Local
 public interface HelloWorldLocal extends Secondary{
 }