我有一个带有EJB的企业应用程序,该EJB实现了一个@Remote业务接口,我想从其他计算机上的JSF托管bean访问该接口。它们都是Netbeans 7和Glassfish 3.1的开发机器。我相信答案取决于CORBA,但我认为我做的不正确。
有没有比CORBA更好的选择了?
这是我找到如何使用corbaname的地方:iiop http://download.oracle.com/docs/cd/E19798-01/821-1752/beanv/index.html
这是我的EJB接口:
package remote.ejb;
import javax.ejb.Remote;
@Remote
public interface HelloRemote {
public String getHello();
}
企业应用程序:RemoteEJBTest
Java EE模块:RemoteEJBTest-ejb
EJB:
package remote.ejb;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
public class HelloBean implements HelloRemote {
@Override
public String getHello() {
return "Hello World!";
}
}
Web应用程序:RemoteWebTest
package hello.web;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import remote.ejb.HelloRemote;
@ManagedBean
@RequestScoped
public class Hello {
private HelloRemote helloBean;
private String hello;
public Hello() throws NamingException {
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup("corbaname:iiop:remote_ip:3700#RemoteEJBTest/RemoteEJBTest-egb/HelloBean");
helloBean = (HelloRemote) PortableRemoteObject.narrow(obj,HelloRemote.class);
}
public String getHello(){
return helloBean.getHello();
}
}
这是堆栈跟踪http://pastebin.com/PxNCKCg4
堆栈跟踪的相关部分:
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: hello.web.Hello.
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
Caused by: javax.naming.NameNotFoundException [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
at com.sun.jndi.cosnaming.ExceptionMapper.mapException(ExceptionMapper.java:44)
at com.sun.jndi.cosnaming.CNCtx.callResolve(CNCtx.java:485)
Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
at org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(NotFoundHelper.java:72)
at org.omg.CosNaming._NamingContextStub.resolve(_NamingContextStub.java:251)
at com.sun.jndi.cosnaming.CNCtx.callResolve(CNCtx.java:471)
... 59 more
在多个远程计算机上拆分EJB的最佳方法是什么?
最佳答案
嗯...我想您错过了这里的几个步骤。
首先,您需要在glassfish-web.xml中创建一个ejb-ref入口,如下所示:
<ejb-ref>
<ejb-ref-name>ejb/Foo</ejb-ref-name>
<jndi-name>corbaname:iiop:host:port#a/b/Foo</jndi-name>
<ejb-ref>
其次,您直接引用您的ejb名称。
Context ic = new InitialContext();
Object o = ic.lookup("java:comp/env/ejb/Foo");
并且由于您使用的是具有EJB 3.1支持的Java EE容器,所以为什么不使用@EJB将EJB直接注入到托管bean中(我认为它比JNDI查找干净得多):
@EJB(name="your-ref-name")
BeanRemoteInterface beanRemoteInterface;
在这里查看更多信息:http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#cross-appserverremoteref