可以从用RPC编写的GWT客户机模块访问EJB远程接口方法吗?gwt应用程序位于安装了Tomcat的服务器上,EJB部署在Jboss服务器中。如果可能,我在哪里可以找到示例代码?
发布于 2012-11-15 04:05:57
您提供的教程看起来不错,尽管它适用于命令行应用程序,但同样的概念也适用于部署在Tomcat上的应用程序。你发现它有什么问题吗?
这里有一个更简单的示例:假设您有一个部署在JBoss上的具有以下简单接口的EJB:
package ejb.example;
import javax.ejb.Remote;
@Remote
public interface Example {
public String hello (String nom);
}远程访问EJB的代码应该类似于:
// Simple EJB Client example
package ejbclient.example
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb.example.Example; // Need to import the remote interface of the bean
public class ClientEJB {
public static void main(String[] args) {
try {
// Set the properties to JBoss access
Properties environment = new Properties();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
environment.put(Context.PROVIDER_URL,"yourjboserver.com:1099" );
InitialContext context = new InitialContext(environment);
// Once the proper context is set, we can obtain the dynamic proxy
Example accessEJB = (Example)
context.lookup("ExampleBean/remote");
// And finally we're done! We can access the EJB as if it was a regular object
String result = accessEJB.hello("Kate"));
} catch (NamingException e) {
e.printStackTrace();
}
}
}要牢记的事情:
答:正如教程中所说,你可以在jndi.properties文件中定义上下文属性,而不是在源代码中硬编码上下文属性,如下所示:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=yourJBossServer.com:JBossJNPPort此文件应放在类路径中,因此,在代码中,您只需调用:
InitialContext context = new InitialContext();这种解决方案更可取,也更优雅(它允许您在不重新编译客户端的情况下更改值)
B.注意context.lookup("ExampleBean/remote")语句:默认情况下,JBoss将接口的JNDI指定为类Bean (实现)的名称,后缀为"/remote“或"/local”,具体取决于接口的类型。这适用于直接部署在jar文件中的EJB,如果将EJB放在EAR文件中,它会添加ear文件的名称作为前缀(例如,您的EJB-jar位于一个名为myapp.ear的ear中,您应该查找的名称是:"myapp/ExampleBean/remote")。当然,您可能已经在EJB中更改了JNDI名称(使用anotations或使用其部署描述符),在这种情况下,您必须使用这些名称。
另一方面,您还需要将本教程中列出的JBoss客户端库放在类路径中(您可以将它们放在war的wEB-INF/lib文件夹中)。
最后,您还需要在类路径中使用远程接口。
我希望它能有所帮助!
https://stackoverflow.com/questions/13380465
复制相似问题