调用ejb遥控器 [英] call ejb remote

查看:146
本文介绍了调用ejb遥控器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用RPC从GWT客户端模块写入访问EJB远程接口方法吗? gwt应用程序位于具有Tomcat的服务器上,EJB部署在Jboss服务器中。如果是可能的,我可以在哪里找到示例代码?

解决方案

您提供的教程看起来不错,虽然它是一个命令行应用程序,相同的概念应该适用于部署在Tomcat上的应用程序。你发现了什么问题?



这里有一个更简单的例子:让我们假设你有一个EJB,这个简单的接口部署在JBoss上:

  package ejb.example; 
import javax.ejb.Remote;
@Remote
public interface Example {
public String hello(String nom);
}

远程访问EJB的代码应该类似于: p>

  //简单的EJB客户端示例
package ejbclient.example
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb.example.Example; //需要导入bean的远程接口
public class ClientEJB {
public static void main(String [] args){
try {
//将属性设置为JBoss访问
属性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);

//一旦正确的上下文设置,我们可以获得动态代理
示例accessEJB =(示例)
context.lookup(ExampleBean / remote);
//最后我们完成了!我们可以访问EJB,就像它是一个常规对象一样。
String result = accessEJB.hello(Kate));
} catch(NamingException e){
e.printStackTrace();
}
}
}

需要考虑的事项:



A。如本教程所述,而不是对源代码中的上下文属性进行硬编码,您可以在jndi.properties文件中定义它们,如下所示:

  java.naming.factory.initial = org.jnp.interfaces.NamingContextFactory 
java.naming.provider.url = yourJBossServer.com:JBossJNPPort

此文件应放置在类路径中,因此,只需要在代码中调用:

  InitialContext context = new InitialContext(); 

此解决方案是优先和更优雅的(它允许您更改值而不重新编译客户端) / p>

B。注意context.lookup(ExampleBean / remote)语句:默认情况下,JBoss将使用sufix/ remote或/ local分配接口的JNDI作为类Bean(实现)的名称,具体取决于接口的种类。这是为了将EJB直接部署在一个jar文件中,如果将EJB放置在EAR中,则会将耳机的名称添加为前缀(例如,您的EJB-jar在一个叫做myapp.ear的名字中)应该查找将是:myapp / ExampleBean / remote)。当然,您可能已经更改了EJB中的JNDI名称(带有引号或使用其部署描述符),在这种情况下,您必须使用这些名称。



℃。另一方面,您还需要在教程中列出JBoss客户端库(可以将它们放在战争的wEB-INF / lib文件夹中)。



D。最后,您还需要您的类路径中的远程接口。



我希望它有帮助!


Is it possible to access an EJB remote interface method from a GWT client module write with RPC? The gwt application is on an server with Tomcat and the EJB is deployed in a Jboss server. If is it possibile, where i can find example code?

解决方案

The tutorial you've provided looks fine, and though it's for a command line app, the same concept should work for an application deployed on Tomcat. What problems have you found with it?

Here you've a simpler example: let's suppose you've an EJB with this simple interface deployed on JBoss:

package ejb.example;
import javax.ejb.Remote;
@Remote
public interface Example {
    public String hello (String nom);
}

The code to access to the EJB remotely should be something similar to:

// 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();
      }
  }
}

Things to take in mind:

A. As it's said in the tutorial, instead of hardcode the the context properties in the source code, you could define them in a jndi.properties file, something like this:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=yourJBossServer.com:JBossJNPPort

this file should be placed in the classpath, and so, in the code you'd only have to call:

InitialContext context = new InitialContext();

this solution is prefered and more elegant (it allows you to change the values without recompiling the client)

B. Pay attention to the context.lookup("ExampleBean/remote") statement: by default JBoss assigns the JNDI of the interfaces as the name of the class Bean (the implementation) with the sufix "/remote" or "/local" depending on the kind of the interface. That's for EJB deployed directly in a jar file, in case the EJB it's placed inside an EAR it adds the name of the ear file as a prefix (for example you've your EJB-jar inside an ear called myapp.ear the name you should lookup for would be: "myapp/ExampleBean/remote"). And of course, you may have changed the JNDI name in the EJB (with anotations or using its deployment descriptors), in that case, you'll have to use these names.

C. On the other hand, you'll also need to have the JBoss client libraries, which are also listed in the tutorial, in the classpath (you could place them in the wEB-INF/lib folder of your war).

D. And finally, you'll also need the remote interface in your classpath.

I hope it helps!

这篇关于调用ejb遥控器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆