我是 EJB 新手,我写了一个非常小的 ejb 组件用于演示目的。它应该做的就是打印“hello”。目前正在努力正确配置远程客户端的 InitialContext。我使用的容器是 JBoss 7.0。我使用 JaveEE7.0 和 ejb3.2。
ejb的接口:
package hello;
public interface Hello {
public String sayHello(String name);
}
豆子本身:
package hello;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless(name="HelloEJB")
@Remote(Hello.class)
public class HelloBean implements Hello {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
以及我放置在 EJB 项目中但我作为 java 应用程序运行的远程客户端:
package client;
import hello.Hello;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class Test {
public static void main(String[] args) {
Hello statelessHello = null;
try {
statelessHello = lookupStatelessHello(); // the method that throws exception
} catch (NamingException e) {
System.out.println("Bean Loading Failed");
e.printStackTrace();
Thread.currentThread().stop();
}
Hello stub=(Hello)PortableRemoteObject.narrow(statelessHello, Hello.class);
System.out.println("obtained a remote stateless hello for invocation");
System.out.println(stub.sayHello(args[0]));
}
private static Hello lookupStatelessHello() throws NamingException {
// the problematic code:
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL,"remote://localhost:4447"); // tried to change ports.
jndiProperties.put("jboss.naming.client.ejb.context", true);
Context context = new InitialContext(jndiProperties); // exception happens here
return (Hello) context.lookup("stateless1/HelloEJB!hello.Hello");
}
}
当我运行 JBoss 时,我成功地部署了我的 ejb 项目:
13:37:20,141 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor](MSC 服务线程 1-6)部署单元部署“stateless1.jar”中名为 HelloEJB 的会话 bean 的 JNDI 绑定如下:
java:global/stateless1/HelloEJB!hello.Hello java:app/stateless1/HelloEJB!hello.Hello java:module/HelloEJB!hello.Hello
java:global/stateless1/HelloEJB java:app/stateless1/HelloEJB
java:module/HelloEJB13:37:20,255 INFO [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) 部署“stateless1.jar”
但是当我尝试从远程客户端调用 ejb 时,我得到:
Bean 加载失败 javax.naming.NoInitialContextException:无法实例化类:org.jboss.naming.remote.client.InitialContextFactory [根异常是 java.lang.ClassNotFoundException:org.jboss.naming.remote.client.InitialContextFactory] 在 javax.naming .spi.NamingManager.getInitialContext(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.init(Unknown Source) at javax.naming.InitialContext.(Unknown Source) at client.Test。 client.Test.main(Test.java:18) 处的lookupStatelessHello(Test.java:34) 原因:java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory at java.net.URLClassLoader.findClass(未知来源)在 sun.misc.Launcher$AppClassLoader 的 java.lang.ClassLoader.loadClass(未知来源)。loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method)
我尝试更改端口,尝试添加安全凭据,但我不明白如何添加用户,尝试阅读指南,但坦率地说,因为我是新手,这对我来说很难。另外,我很确定我查找 bean 的方式是错误的,但这不是现在的问题。
我希望你能帮助我找到我在这里做错的事情。如果您需要有关设置的更多信息,请询问。