我是EJB的新手,我为了演示目的编写了一个非常小的ejb组件。所有它应该做的是打印“你好”。目前正在努力正确配置远程客户端的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)名为HelloEJB的会话bean的JNDI绑定 在部署单元部署中,“stateless1.jar”如下:
Java的:全球/ stateless1 / HelloEJB hello.Hello Java的:应用程序/ stateless1 / HelloEJB hello.Hello Java的:模块/ HelloEJB hello.Hello
Java的:全球/ stateless1 / HelloEJB Java的:应用程序/ stateless1 / HelloEJB
的java:模块/ 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] at javax.naming.spi.NamingManager.getInitialContext(未知来源)at javax.naming.InitialContext.getDefaultInitCtx(未知来源)at javax.naming.InitialContext.init(未知来源)at javax.naming.InitialContext。(未知来源)at client.Test.lookupStatelessHello(Test.java:34)at client.Test.main(Test.java:18)引起: 抛出java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory at java.net.URLClassLoader.findClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at java.lang.ClassLoader.loadClass(未知来源)at java.lang.Class.forName0(本机方法)
我试图更改端口,尝试添加安全凭据但我无法理解如何添加用户,试图阅读指南但坦率地说因为我是新手这对我来说很难。另外,我很确定我查找我的bean的方式是错误的,但现在不是问题。
我希望你能帮助我找到我在这里做错的事情。如果您需要有关设置的更多信息,请询问。
答案 0 :(得分:1)
您的客户端的类路径中缺少类。
查看$ JBOSS_HOME / bin / client / README-EJB-JMS.txt文件,了解有关您需要在客户端中包含哪些jar的信息。
答案 1 :(得分:1)
如上所述,您需要将jboss-client.jar与您的代码一起使用才能生效。具体的罐子可以在
找到JBOSS_HOME/bin/client/jboss-client-7.1.0.Final.jar.
另外,请查看官方文档和wiki以及完整示例。请参阅here。
答案 2 :(得分:0)
client*.jar
在类路径中,gf-client.jar
是使用glassfish而不是JBoss进行的。 Context context = new InitialContext();
下面的工作代码粘贴并运行;)
import javax.ejb.Remote;
@Remote
public interface HelloI {
public String sayHello(String name);
}
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless(name = "HelloWorldEJB", mappedName="HelloEJB")
@Remote(HelloI.class)
public class Hello implements HelloI {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
public class HelloT {
public static void main(String[] args) {
HelloI statelessHelloI = null;
try {
statelessHelloI = lookupStatelessHello(); // the method that throws exception
} catch (NamingException e) {
System.out.println("Bean Loading Failed");
e.printStackTrace();
Thread.currentThread().stop();
}
HelloI stub=(HelloI) PortableRemoteObject.narrow(statelessHelloI, HelloI.class);
System.out.println("obtained a remote stateless hello for invocation");
System.out.println(stub.sayHello(args[0]));
}
private static HelloI 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*/
Context context = new InitialContext();
return (HelloI) context.lookup("HelloEJB");
}
}
输出2020年7月18日,下午5:58:07 获得了远程无状态 打招呼你好,阿妮
以退出代码0结束的过程
在上面的帖子中,我留下的大部分代码都没有涉及。上面的客户端代码可以简化为
import com.au.ejbs.HelloI;
import javax.naming.Context;
import javax.naming.InitialContext;
public class HelloT2 {
public static void main(String[] args)throws Exception {
Context context = new InitialContext();
HelloI remote = (HelloI) context.lookup("HelloEJB");//resolves to mappedName @Stateless(name = "HelloWorldEJB", mappedName="HelloEJB"), from what I read could be glassfish and weblogic centric.
System.out.println(remote.sayHello(args[0]));
remote = (HelloI) context.lookup("java:global/ejb3_2_ear_exploded/ejb/HelloWorldEJB");//Portable should work on jboss and any container resolves to Stateless(name = "HelloWorldEJB"
System.out.println(remote.sayHello(args[0]));
}
}