我有一个带有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的最佳方法是什么?
答案 0 :(得分:2)
首先,您需要在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直接在托管bean上注入EJB(我认为它比JNDI查找要简洁得多):
@EJB(name="your-ref-name")
BeanRemoteInterface beanRemoteInterface;
请点击此处了解更多信息:http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#cross-appserverremoteref
答案 1 :(得分:0)
这里的样品很好: 为Oracle WebLogic Server开发独立客户端 http://docs.oracle.com/middleware/1212/wls/SACLT/j2se.htm