我想从另一个访问GlassFish Server 4.0中的@Remote
bean。我尝试了很多方法,但没有人工作。
//This is the remote Interface For server A and client B:
@Remote
public interface SayHelloBeanRemote extends Serializable{
public void test(String name);
}
//Impl for Client B:
@Stateless
public class SayHelloBean implements SayHelloBeanRemote {
@Override
public void test(String name) {
System.out.println("nihao"+name);
}
}
首先我在JSF托管bean中编写一个方法:
public void test() throws NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.104");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
SayHelloBeanRemote testRmi = (SayHelloBeanRemote) ctx.lookup("java:global/EJBRm/rmi/NewSessionBean");
testRmi.test("xx");
System.out.println("k开始连接.....................");
}
这是错误:
线程中的异常" main" javax.naming.NamingException:查找失败 for' java:global / EJBRmi / rmi / NewSessionBean'在 SerialContext [myEnv = {org.omg.CORBA.ORBInitialPort = 3700, java.naming.factory.initial的= com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHost = 192.168.1.104, java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming} [Root 异常是javax.naming.NameNotFoundException:rmi]
然后我尝试使用sun-web.xml
配置应用:
<glassfish-web-app error-url="">
<ejb-ref>
<ejb-ref-name>RemoteEJBName</ejb-ref-name>
<jndi-name>corbaname:iiop:<servername or IP>:3700#java:global/Rmi_ejb /FooClassImpl!com.test.foo.FooClass</jndi-name>
</ejb-ref>
</glassfish-web-app>
我使用标签:
@EJB( name="TheRef") SayHelloBeanRemote testRmi;
但是注射失败了。
所以我需要你的帮助,以正确的方式访问A和B之间的EJB。
答案 0 :(得分:0)
由于您使用的是Java EE 7,并且您的服务器和客户端都位于应用程序服务器中,因此无需进行手动查找 - 只需拥有需要访问EJB的独立Java客户端时(参见我在my answer to this post中的例子)。
如果从另一个托管组件访问一个EJB,只需使用注入机制。请参阅Java EE教程中的“Accessing Enterprise Beans”一章。